Skip to content

Commit dbc1aa9

Browse files
committed
Add basic formatter and format regular files
The formatter file is copied from aws-codegen and this commit only formats the regular files (not the generated by that tool). We should start to generate formatted files soon.
1 parent dd39fb7 commit dbc1aa9

File tree

6 files changed

+137
-75
lines changed

6 files changed

+137
-75
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

lib/aws/request.ex

+86-44
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ defmodule AWS.Request do
117117

118118
case response_header_parameters do
119119
[_ | _] ->
120-
merge_body_with_response_headers(response_body, response, response_header_parameters)
120+
merge_body_with_response_headers(
121+
response_body,
122+
response,
123+
response_header_parameters
124+
)
121125

122126
_ ->
123127
response_body
@@ -191,7 +195,8 @@ defmodule AWS.Request do
191195
end
192196
end
193197

194-
defp encode!(%Client{} = client, protocol, payload) when protocol in @valid_protocols and is_map(payload) do
198+
defp encode!(%Client{} = client, protocol, payload)
199+
when protocol in @valid_protocols and is_map(payload) do
195200
encode_format =
196201
case protocol do
197202
"query" -> :query
@@ -244,17 +249,20 @@ defmodule AWS.Request do
244249

245250
canonical_request = Internal.canonical_request(method, url, headers, body)
246251
hashed_canonical_request = Util.sha256_hexdigest(canonical_request)
247-
credential_scope = Internal.credential_scope(short_date, client.region,
248-
client.service)
249-
signing_key = Internal.signing_key(client.secret_access_key, short_date,
250-
client.region, client.service)
251-
string_to_sign = Internal.string_to_sign(long_date, credential_scope,
252-
hashed_canonical_request)
252+
credential_scope = Internal.credential_scope(short_date, client.region, client.service)
253+
254+
signing_key =
255+
Internal.signing_key(client.secret_access_key, short_date, client.region, client.service)
256+
257+
string_to_sign =
258+
Internal.string_to_sign(long_date, credential_scope, hashed_canonical_request)
259+
253260
signature = Util.hmac_sha256_hexdigest(signing_key, string_to_sign)
254261
signed_headers = Internal.signed_headers(headers)
255-
authorization = Internal.authorization(client.access_key_id,
256-
credential_scope, signed_headers,
257-
signature)
262+
263+
authorization =
264+
Internal.authorization(client.access_key_id, credential_scope, signed_headers, signature)
265+
258266
Internal.add_authorization_header(headers, authorization)
259267
end
260268

@@ -274,23 +282,33 @@ defmodule AWS.Request do
274282
headers = Internal.add_date_header(headers, long_date)
275283
canonical_request = Internal.canonical_request(method, url, headers, body)
276284
hashed_canonical_request = Util.sha256_hexdigest(canonical_request)
277-
credential_scope = Internal.credential_scope(short_date, client.region,
278-
client.service)
279-
signing_key = Internal.signing_key(client.secret_access_key, short_date,
280-
client.region, client.service)
281-
string_to_sign = Internal.string_to_sign(long_date, credential_scope,
282-
hashed_canonical_request)
285+
credential_scope = Internal.credential_scope(short_date, client.region, client.service)
286+
287+
signing_key =
288+
Internal.signing_key(client.secret_access_key, short_date, client.region, client.service)
289+
290+
string_to_sign =
291+
Internal.string_to_sign(long_date, credential_scope, hashed_canonical_request)
292+
283293
signature = Util.hmac_sha256_hexdigest(signing_key, string_to_sign)
284294
signed_headers = Internal.signed_headers(headers)
285-
credential = Enum.join([client.access_key_id, short_date, client.region,
286-
client.service, "aws4_request"], "/")
287-
result = [{"X-Amz-Algorithm", "AWS4-HMAC-SHA256"},
288-
{"X-Amz-Credential", credential},
289-
{"X-Amz-Date", long_date},
290-
{"X-Amz-SignedHeaders", signed_headers},
291-
{"X-Amz-Signature", signature}]
295+
296+
credential =
297+
Enum.join(
298+
[client.access_key_id, short_date, client.region, client.service, "aws4_request"],
299+
"/"
300+
)
301+
302+
result = [
303+
{"X-Amz-Algorithm", "AWS4-HMAC-SHA256"},
304+
{"X-Amz-Credential", credential},
305+
{"X-Amz-Date", long_date},
306+
{"X-Amz-SignedHeaders", signed_headers},
307+
{"X-Amz-Signature", signature}
308+
]
309+
292310
if expiry = :proplists.get_value("X-Amz-Expires", headers, nil) do
293-
[{"X-Amz-Expires", expiry}|result]
311+
[{"X-Amz-Expires", expiry} | result]
294312
else
295313
result
296314
end
@@ -323,6 +341,7 @@ defmodule AWS.Request do
323341
def add_headers([], headers) do
324342
headers
325343
end
344+
326345
def add_headers([{name, _} = header | additions], headers) do
327346
case List.keyfind(headers, name, 0) do
328347
nil -> add_headers(additions, [header | headers])
@@ -343,23 +362,24 @@ defmodule AWS.Request.Internal do
343362
of headers.
344363
"""
345364
def add_authorization_header(headers, authorization) do
346-
[{"Authorization", authorization}|headers]
365+
[{"Authorization", authorization} | headers]
347366
end
348367

349368
@doc """
350369
Add an `X-Amz-Date` header with a long date value in `YYMMDDTHHMMSSZ` format
351370
to a list of headers.
352371
"""
353372
def add_date_header(headers, date) do
354-
[{"X-Amz-Date", date}|headers]
373+
[{"X-Amz-Date", date} | headers]
355374
end
356375

357376
@doc """
358377
Add an `X-Amz-Security-Token` if credentials configurations are configured for it
359378
"""
360379
def add_security_token(headers, %AWS.Client{session_token: nil}), do: headers
380+
361381
def add_security_token(headers, %AWS.Client{session_token: session_token}),
362-
do: [{"X-Amz-Security-Token", session_token}|headers]
382+
do: [{"X-Amz-Security-Token", session_token} | headers]
363383

364384
@doc """
365385
Add an X-Amz-Content-SHA256 header which is the hash of the payload.
@@ -374,11 +394,22 @@ defmodule AWS.Request.Internal do
374394
Generate an AWS4-HMAC-SHA256 authorization signature.
375395
"""
376396
def authorization(access_key_id, credential_scope, signed_headers, signature) do
377-
Enum.join(["AWS4-HMAC-SHA256 ",
378-
"Credential=", access_key_id, "/", credential_scope, ", ",
379-
"SignedHeaders=", signed_headers, ", ",
380-
"Signature=", signature],
381-
"")
397+
Enum.join(
398+
[
399+
"AWS4-HMAC-SHA256 ",
400+
"Credential=",
401+
access_key_id,
402+
"/",
403+
credential_scope,
404+
", ",
405+
"SignedHeaders=",
406+
signed_headers,
407+
", ",
408+
"Signature=",
409+
signature
410+
],
411+
""
412+
)
382413
end
383414

384415
@doc """
@@ -405,7 +436,7 @@ defmodule AWS.Request.Internal do
405436
"""
406437
def canonical_request(method, url, headers, body) when is_atom(method) do
407438
Atom.to_string(method)
408-
|> String.upcase
439+
|> String.upcase()
409440
|> canonical_request(url, headers, body)
410441
end
411442

@@ -414,8 +445,18 @@ defmodule AWS.Request.Internal do
414445
canonical_headers = canonical_headers(headers)
415446
signed_headers = signed_headers(headers)
416447
payload_hash = AWS.Util.sha256_hexdigest(body)
417-
Enum.join([method, canonical_url, canonical_query_string,
418-
canonical_headers, signed_headers, payload_hash], "\n")
448+
449+
Enum.join(
450+
[
451+
method,
452+
canonical_url,
453+
canonical_query_string,
454+
canonical_headers,
455+
signed_headers,
456+
payload_hash
457+
],
458+
"\n"
459+
)
419460
end
420461

421462
@doc """
@@ -440,15 +481,15 @@ defmodule AWS.Request.Internal do
440481
and header names are semicolon-joined in alphabetical order.
441482
"""
442483
def signed_headers(headers) do
443-
Enum.map(headers, &signed_header/1) |> Enum.sort |> Enum.join(";")
484+
Enum.map(headers, &signed_header/1) |> Enum.sort() |> Enum.join(";")
444485
end
445486

446487
@doc """
447488
Generate a signing key from a secret access key, a short date in `YYMMDD`
448489
format, a region identifier and a service identifier.
449490
"""
450491
def signing_key(secret_access_key, short_date, region, service) do
451-
"AWS4" <> secret_access_key
492+
("AWS4" <> secret_access_key)
452493
|> AWS.Util.hmac_sha256(short_date)
453494
|> AWS.Util.hmac_sha256(region)
454495
|> AWS.Util.hmac_sha256(service)
@@ -479,18 +520,19 @@ defmodule AWS.Request.Internal do
479520
|> Enum.map(&String.split(&1, "="))
480521
|> Enum.sort()
481522
|> Enum.map_join("&", fn
482-
[key, value] -> key <> "=" <> value
483-
[key] -> key <> "="
484-
end)
523+
[key, value] -> key <> "=" <> value
524+
[key] -> key <> "="
525+
end)
485526
end
486527

487528
@doc """
488529
Generate the text to sign from a long date in `YYMMDDTHHMMSSZ` format, a
489530
credential scope and a hashed canonical request.
490531
"""
491532
def string_to_sign(long_date, credential_scope, hashed_canonical_request) do
492-
Enum.join(["AWS4-HMAC-SHA256", long_date,
493-
credential_scope, hashed_canonical_request], "\n")
533+
Enum.join(
534+
["AWS4-HMAC-SHA256", long_date, credential_scope, hashed_canonical_request],
535+
"\n"
536+
)
494537
end
495-
496538
end

lib/aws/util.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ defmodule AWS.Util do
3737
def encode_query(value) do
3838
value
3939
|> Enum.map(fn {k, v} -> {k, to_string(v)} end)
40-
|> :uri_string.compose_query
40+
|> :uri_string.compose_query()
4141
end
42-
4342
end

lib/aws/xml.ex

+34-15
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ defmodule AWS.XML do
3030

3131
def encode_to_iodata!(map, _opts \\ []) do
3232
map
33-
|> Map.to_list
33+
|> Map.to_list()
3434
|> Enum.map(&encode_xml_key_value/1)
35-
|> :erlang.iolist_to_binary
35+
|> :erlang.iolist_to_binary()
3636
end
3737

3838
def decode!(xml, _opts \\ []) do
@@ -45,40 +45,55 @@ defmodule AWS.XML do
4545
defp encode_xml_key_value({k, v}) when is_binary(k) and is_binary(v) do
4646
["<", k, ">", v, "</", k, ">"]
4747
end
48+
4849
defp encode_xml_key_value({k, values}) when is_binary(k) and is_list(values) do
4950
for v <- values do
5051
encode_xml_key_value({k, v})
5152
end
5253
end
54+
5355
defp encode_xml_key_value({k, v}) when is_binary(k) and is_integer(v) do
5456
["<", k, ">", Integer.to_charlist(v), "</", k, ">"]
5557
end
58+
5659
defp encode_xml_key_value({k, v}) when is_binary(k) and is_float(v) do
5760
["<", k, ">", Float.to_charlist(v), "</", k, ">"]
5861
end
62+
5963
defp encode_xml_key_value({k, v}) when is_binary(k) and is_map(v) do
60-
[ "<", k, ">",
64+
[
65+
"<",
66+
k,
67+
">",
6168
v
62-
|> Map.to_list
69+
|> Map.to_list()
6370
|> Enum.map(&encode_xml_key_value/1),
64-
"</", k, ">"
71+
"</",
72+
k,
73+
">"
6574
]
6675
end
6776

6877
_ = """
6978
Callback hook_fun for xmerl parser
7079
"""
80+
7181
defp hook_fun(element, global_state) when Record.is_record(element, :xmlElement) do
7282
tag = xmlElement(element, :name)
7383
content = xmlElement(element, :content)
74-
value = case List.foldr(content, :none, &content_to_map/2) do
75-
v = %{@text => text} ->
76-
case String.trim(text) do
77-
"" -> Map.delete(v, @text)
78-
trimmed -> Map.put(v, @text, trimmed)
79-
end
80-
v -> v
81-
end
84+
85+
value =
86+
case List.foldr(content, :none, &content_to_map/2) do
87+
v = %{@text => text} ->
88+
case String.trim(text) do
89+
"" -> Map.delete(v, @text)
90+
trimmed -> Map.put(v, @text, trimmed)
91+
end
92+
93+
v ->
94+
v
95+
end
96+
8297
{%{Atom.to_string(tag) => value}, global_state}
8398
end
8499

@@ -99,20 +114,25 @@ defmodule AWS.XML do
99114
elements are processed as described above and all the text parts
100115
are merged under the `__text' key.
101116
"""
117+
102118
defp content_to_map(x, :none) do
103119
x
104120
end
105121

106122
defp content_to_map(x, acc) when is_map(x) and is_map(acc) do
107123
[{tag, value}] = Map.to_list(x)
124+
108125
case Map.has_key?(acc, tag) do
109126
true ->
110127
update_fun = fn
111128
l when is_list(l) -> [value | l]
112129
v -> [value, v]
113130
end
131+
114132
Map.update!(acc, tag, update_fun)
115-
false -> Map.put(acc, tag, value)
133+
134+
false ->
135+
Map.put(acc, tag, value)
116136
end
117137
end
118138

@@ -131,5 +151,4 @@ defmodule AWS.XML do
131151
defp content_to_map(x, acc) when is_map(x) and is_binary(acc) do
132152
Map.put(x, @text, acc)
133153
end
134-
135154
end

test/aws/util_test.exs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ defmodule AWS.UtilTest do
33
alias AWS.Util
44

55
test "hmac_sha256/2 returns a SHA256 HMAC for a message" do
6-
expected = <<110, 158, 242, 155, 117, 255, 252, 91,
7-
122, 186, 229, 39, 213, 143, 218, 219,
8-
47, 228, 46, 114, 25, 1, 25, 118,
9-
145, 115, 67, 6, 95, 88, 237, 74>>
6+
expected =
7+
<<110, 158, 242, 155, 117, 255, 252, 91, 122, 186, 229, 39, 213, 143, 218, 219, 47, 228, 46,
8+
114, 25, 1, 25, 118, 145, 115, 67, 6, 95, 88, 237, 74>>
9+
1010
assert ^expected = Util.hmac_sha256("key", "message")
1111
end
1212

0 commit comments

Comments
 (0)