-
Notifications
You must be signed in to change notification settings - Fork 69
221 response codes #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
221 response codes #321
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b7dcc0d
221 - added custom response code support for kafka response_from target
mmacai f1fc317
221 - reworked status code setting in response_from targets
mmacai 59b2317
Merge branch 'master' into 221-response-codes
mmacai fc9edab
221 - restructured response_from parsing
mmacai baeb145
221 - updated changelog, docs and response_from handling
mmacai bd80fea
221 - removed unnecessary encoding
mmacai 9bebad0
221 - restructured response_from handling
mmacai e7e78ec
221 - cleanup
mmacai d2fc685
221 - added new /v3 internal api to support response code
mmacai f235177
221 - updated to_int function
mmacai 1b2e98b
221 - added forwarding of extra headers for response_from
mmacai 63bece3
221 - fixed failing test and updated response code converting function
mmacai 8a8b0c4
Format the kafka response_from tests a bit
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,7 +103,7 @@ defmodule RigInboundGateway.ApiProxy.Handler.Http do | |
| end | ||
|
|
||
| receive do | ||
| {:response_received, response} -> | ||
| {:response_received, response, response_code, response_headers} -> | ||
| ProxyMetrics.count_proxy_request( | ||
| conn.method, | ||
| conn.request_path, | ||
|
|
@@ -116,7 +116,10 @@ defmodule RigInboundGateway.ApiProxy.Handler.Http do | |
| |> with_cors() | ||
| |> Tracing.Plug.put_resp_header(Tracing.context()) | ||
| |> Conn.put_resp_content_type("application/json") | ||
| |> Conn.send_resp(:ok, response) | ||
| |> Map.update!(:resp_headers, fn existing_headers -> | ||
| existing_headers ++ Map.to_list(response_headers) | ||
| end) | ||
|
mmacai marked this conversation as resolved.
|
||
| |> Conn.send_resp(response_code, response) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about the content type? Ideally, we'd forward the body as-is and also retain the content-type, then the backend can send whatever |
||
| after | ||
| response_timeout -> | ||
| ProxyMetrics.count_proxy_request( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
lib/rig_inbound_gateway/api_proxy/response_from_parser.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| defmodule RigInboundGateway.ApiProxy.ResponseFromParser do | ||
| @moduledoc """ | ||
| Handles parsing of messages coming from different sources (e.g. Kafka) | ||
|
|
||
| """ | ||
|
|
||
| require Logger | ||
|
|
||
| use Rig.Config, [:schema_registry_host] | ||
|
|
||
| alias Rig.Connection.Codec | ||
| alias RigKafka.Avro | ||
|
|
||
| @default_response_code 200 | ||
|
|
||
| # --- | ||
|
|
||
| @spec parse([{String.t(), String.t()}, ...], map()) :: | ||
| {pid, pos_integer(), any(), map()} | String.t() | ||
| # structured | ||
| def parse( | ||
| _headers, | ||
| %{ | ||
| "body" => body, | ||
| "rig" => rig_metadata | ||
| } = message | ||
| ) do | ||
|
mmacai marked this conversation as resolved.
Outdated
|
||
| with {:ok, correlation_id} <- Map.fetch(rig_metadata, "correlation"), | ||
| {:ok, deserialized_pid} <- Codec.deserialize(correlation_id), | ||
| raw_response_code <- Map.get(rig_metadata, "response_code", @default_response_code), | ||
|
kevinbader marked this conversation as resolved.
Outdated
|
||
| # convert status code to int if needed, HTTP headers can't have number as a value | ||
| {response_code, _} <- to_int(raw_response_code), | ||
|
kevinbader marked this conversation as resolved.
Outdated
|
||
| response_headers <- Map.get(message, "headers", %{}), | ||
| response_body <- try_encode(body) do | ||
| Logger.debug(fn -> | ||
| "Parsed structured HTTP response: body=#{inspect(response_body)}, headers=#{ | ||
| inspect(response_headers) | ||
| }, code=#{inspect(response_code)}" | ||
| end) | ||
|
|
||
| {deserialized_pid, response_code, response_body, response_headers} | ||
| else | ||
| err -> err | ||
| end | ||
| end | ||
|
|
||
| # binary | ||
| def parse( | ||
| headers, | ||
| message | ||
| ) do | ||
| with headers_map <- Enum.into(headers, %{}), | ||
| {:ok, correlation_id} <- Map.fetch(headers_map, "rig-correlation"), | ||
| {:ok, deserialized_pid} <- Codec.deserialize(correlation_id), | ||
| raw_response_code <- Map.get(headers_map, "rig-response-code", @default_response_code), | ||
| # convert status code to int if needed, HTTP headers can't have number as a value | ||
| {response_code, _} <- to_int(raw_response_code), | ||
| response_body <- try_encode(message) do | ||
|
kevinbader marked this conversation as resolved.
|
||
| Logger.debug(fn -> | ||
| "Parsed binary HTTP response: body=#{inspect(message)}, code=#{inspect(response_code)}" | ||
| end) | ||
|
|
||
| {deserialized_pid, response_code, response_body, %{}} | ||
| else | ||
| err -> err | ||
| end | ||
| end | ||
|
|
||
| # --- | ||
|
|
||
| @spec try_decode_message(<<_::3>> | String.t()) :: String.t() | map() | ||
| def try_decode_message(<<0::8, _id::32, _body::binary>> = message), | ||
| do: Avro.decode(message, config().schema_registry_host) | ||
|
|
||
| def try_decode_message(message) when is_binary(message) do | ||
| case Jason.decode(message) do | ||
| {:ok, val_map} -> | ||
| val_map | ||
|
|
||
| _ -> | ||
| message | ||
| end | ||
| end | ||
|
|
||
| # --- | ||
|
|
||
| defp try_encode(message) when is_binary(message), do: message | ||
|
|
||
| defp try_encode(message) do | ||
| case Jason.encode(message) do | ||
| {:ok, val_map} -> | ||
| val_map | ||
|
|
||
| _ -> | ||
| message | ||
| end | ||
| end | ||
|
|
||
| # --- | ||
|
|
||
| defp to_int(value) when is_integer(value), do: {value, nil} | ||
| defp to_int(value), do: Integer.parse(value) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.