Skip to content

Commit

Permalink
Merge pull request #117 from thoroai/feat/add-custom-metadata-support
Browse files Browse the repository at this point in the history
Adds support for custom metadata for aws-elixir.
  • Loading branch information
onno-vos-dev authored Nov 20, 2024
2 parents 879a665 + 205ae17 commit feec7b3
Show file tree
Hide file tree
Showing 5 changed files with 11,263 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/aws_codegen/rest_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ defmodule AWS.CodeGen.RestService do
function_name = AWS.CodeGen.Name.to_snake_case(operation)
request_header_parameters = collect_request_header_parameters(language, api_spec, operation)

request_headers_parameters =
collect_request_headers_parameters(language, api_spec, operation)

is_required = fn param -> param.required end
required_query_parameters = Enum.filter(query_parameters, is_required)
required_request_header_parameters = Enum.filter(request_header_parameters, is_required)
Expand All @@ -266,7 +269,8 @@ defmodule AWS.CodeGen.RestService do
"GET" ->
case language do
:elixir ->
2 + length(request_header_parameters) + length(query_parameters)
2 + length(request_header_parameters) + length(request_headers_parameters) +
length(query_parameters)

:erlang ->
4 + length(required_request_header_parameters) + length(required_query_parameters)
Expand Down Expand Up @@ -295,6 +299,7 @@ defmodule AWS.CodeGen.RestService do
query_parameters: query_parameters,
required_query_parameters: required_query_parameters,
request_header_parameters: request_header_parameters,
request_headers_parameters: request_headers_parameters,
required_request_header_parameters: required_request_header_parameters,
response_header_parameters:
collect_response_header_parameters(language, api_spec, operation),
Expand Down Expand Up @@ -338,6 +343,10 @@ defmodule AWS.CodeGen.RestService do
collect_parameters(language, api_spec, operation, "input", "smithy.api#httpHeader")
end

defp collect_request_headers_parameters(language, api_spec, operation) do
collect_parameters(language, api_spec, operation, "input", "smithy.api#httpPrefixHeaders")
end

defp collect_response_header_parameters(language, api_spec, operation) do
collect_parameters(language, api_spec, operation, "output", "smithy.api#httpHeader")
end
Expand Down
10 changes: 8 additions & 2 deletions priv/rest.ex.eex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ def <%= action.function_name %>(%Client{} = client<%= AWS.CodeGen.RestService.fu
{"<%= parameter.name %>", "<%= parameter.location_name %>"},<% end %>
]
|> Request.build_params(input)<% else %>
headers = []<% end %><%= if length(action.query_parameters) > 0 do %>
headers = []<% end %><%= if length(action.request_headers_parameters) > 0 do %>
{custom_headers, input} =
[<%= for parameter <- action.request_headers_parameters do %>
{"<%= parameter.name %>", "<%= parameter.location_name %>"},<% end %>
]
|> Request.build_params(input)<% else %>
custom_headers = []<% end %><%= if length(action.query_parameters) > 0 do %>
{query_params, input} =
[<%= for parameter <- action.query_parameters do %>
{"<%= parameter.name %>", "<%= parameter.location_name %>"},<% end %>
Expand Down Expand Up @@ -166,6 +172,6 @@ def <%= action.function_name %>(%Client{} = client<%= AWS.CodeGen.RestService.fu
meta = metadata()
<% end %>

Request.request_rest(client, meta, <%= AWS.CodeGen.RestService.Action.method(action) %>, url_path, query_params, headers, input, options, <%= inspect(action.success_status_code) %>)<% end %>
Request.request_rest(client, meta, <%= AWS.CodeGen.RestService.Action.method(action) %>, url_path, query_params, custom_headers ++ headers, input, options, <%= inspect(action.success_status_code) %>)<% end %>
end<% end %>
end
41 changes: 38 additions & 3 deletions test/aws_codegen_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule AWS.CodeGenTest do
}
}

@service_spec_custom_headers_file "test/fixtures/apis_specs/dataexchange.json"
@service_spec_custom_headers_generated "test/fixtures/generated/data_exchange.ex"

setup do
service_specs =
@service_spec_file
Expand All @@ -24,7 +27,7 @@ defmodule AWS.CodeGenTest do
end

describe "render/2" do
defp setup_context(language, specs, docs \\ nil) do
defp setup_context(language, specs, _docs \\ nil) do
spec = %AWS.CodeGen.Spec{
api: specs,
module_name: "AWS.CloudTrailData",
Expand All @@ -37,6 +40,19 @@ defmodule AWS.CodeGenTest do
RestService.load_context(language, spec, @endpoints_spec)
end

defp setup_dataexchange_context(language, specs, _docs \\ nil) do
spec = %AWS.CodeGen.Spec{
api: specs,
module_name: "AWS.DataExchange",
filename: "data_exchange.ex",
protocol: :rest_json,
language: :elixir,
shape_name: "com.amazonaws.dataexchange#DataExchange"
}

RestService.load_context(language, spec, @endpoints_spec)
end

test "renders the Elixir module with docs", %{specs: specs} do
context = setup_context(:elixir, specs)

Expand Down Expand Up @@ -238,6 +254,7 @@ defmodule AWS.CodeGenTest do
def put_audit_events(%Client{} = client, input, options \\\\ []) do
url_path = \"/PutAuditEvents\"
headers = []
custom_headers = []
{query_params, input} =
[
Expand All @@ -254,7 +271,7 @@ defmodule AWS.CodeGenTest do
:post,
url_path,
query_params,
headers,
custom_headers ++ headers,
input,
options,
200
Expand Down Expand Up @@ -717,6 +734,7 @@ defmodule AWS.CodeGenTest do
def put_audit_events(%Client{} = client, input, options \\\\ []) do
url_path = \"/PutAuditEvents\"
headers = []
custom_headers = []
{query_params, input} =
[
Expand All @@ -733,7 +751,7 @@ defmodule AWS.CodeGenTest do
:post,
url_path,
query_params,
headers,
custom_headers ++ headers,
input,
options,
200
Expand All @@ -742,5 +760,22 @@ defmodule AWS.CodeGenTest do
end
""")
end

test "renders the Elixir module with custom headers" do
specs_file =
@service_spec_custom_headers_file
|> File.read!()
|> Poison.decode!()

context = setup_dataexchange_context(:elixir, specs_file)

result =
context
|> AWS.CodeGen.render("priv/rest.ex.eex")
|> IO.iodata_to_binary()

generated = File.read!(@service_spec_custom_headers_generated)
assert result == generated
end
end
end
Loading

0 comments on commit feec7b3

Please sign in to comment.