Skip to content
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

Add getters for aws_client() and remove all usages of maps:get(<something>, Client) #135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/aws_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
, make_local_client/4
]).

-export([ access_key_id/1
, secret_access_key/1
, token/1
, region/1
, endpoint/1
, proto/1
, port/1
, service/1
]).

-type access_key_id() :: binary().
-type secret_access_key() :: binary().
-type region() :: binary().
-type token() :: binary().
-type http_port() :: binary().
-type aws_client() :: map().
-type proto() :: binary().
-type service() :: binary().
-type endpoint() :: binary().
-opaque aws_client() :: map().

-export_type([access_key_id/0, secret_access_key/0, region/0,
token/0, aws_client/0]).
onno-vos-dev marked this conversation as resolved.
Show resolved Hide resolved
andreashasse marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -97,6 +109,40 @@ make_local_client(AccessKeyID, SecretAccessKey, Port, Endpoint)
port => Port,
service => undefined}.

-spec access_key_id(aws_client()) -> access_key_id().
access_key_id(#{access_key_id := AccessKeyId} = _Client) ->
AccessKeyId.

-spec secret_access_key(aws_client()) -> secret_access_key().
secret_access_key(#{secret_access_key := SecretAccessKey} = _Client) ->
SecretAccessKey.

-spec token(aws_client()) -> token().
token(#{token := Token} = _Client) ->
Token;
token(Client) when is_map(Client) ->
undefined.

-spec region(aws_client()) -> region().
region(#{region := Region} = _Client) ->
Region.

-spec endpoint(aws_client()) -> endpoint().
endpoint(#{endpoint := Endpoint} = _Client) ->
Endpoint.

-spec proto(aws_client()) -> proto().
proto(#{proto := Proto} = _Client) ->
Proto.

-spec port(aws_client()) -> port().
port(#{port := Port} = _Client) ->
Port.

-spec service(aws_client()) -> service().
service(#{service := Service} = _Client) ->
Service.

%%====================================================================
%% Helper functions
%%====================================================================
Expand Down
10 changes: 5 additions & 5 deletions src/aws_request.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ request(RequestFun, Options) ->
%% Generate headers with an AWS signature version 4 for the specified
%% request.
sign_request(Client, Method, URL, Headers0, Body) ->
AccessKeyID = maps:get(access_key_id, Client),
SecretAccessKey = maps:get(secret_access_key, Client),
Region = maps:get(region, Client),
Service = maps:get(service, Client),
Token = maps:get(token, Client, undefined),
AccessKeyID = aws_client:access_key_id(Client),
SecretAccessKey = aws_client:secret_access_key(Client),
Region = aws_client:region(Client),
Service = aws_client:service(Client),
Token = aws_client:token(Client),
Headers = case Token of
undefined -> Headers0;
_ -> [{<<"X-Amz-Security-Token">>, Token}|Headers0]
Expand Down
16 changes: 8 additions & 8 deletions src/aws_s3_presigned_url.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ make_presigned_v4_url(Client0, Method, ExpireSeconds, Bucket, Key) ->
MethodBin = aws_request:method_to_binary(Method),
Path = ["/", aws_util:encode_uri(Bucket), "/", aws_util:encode_multi_segment_uri(Key), ""],
Client = Client0#{service => <<"s3">>},
SecurityToken = maps:get(token, Client, undefined),
AccessKeyID = maps:get(access_key_id, Client),
SecretAccessKey = maps:get(secret_access_key, Client),
Region = maps:get(region, Client),
Service = maps:get(service, Client),
SecurityToken = aws_client:token(Client),
AccessKeyID = aws_client:access_key_id(Client),
SecretAccessKey = aws_client:secret_access_key(Client),
Region = aws_client:region(Client),
Service = aws_client:service(Client),
Host = build_host(<<"s3">>, Client, Bucket),
URL = build_url(Host, Path, Client, Bucket),
Now = calendar:universal_time(),
Expand All @@ -51,19 +51,19 @@ build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}, Bucket) ->
aws_util:binary_join([Bucket, EndpointPrefix, Region, Endpoint], <<".">>).

build_url(Host0, Path0, Client, Bucket) ->
Proto = maps:get(proto, Client),
Proto = aws_client:proto(Client),
%% Mocks are notoriously bad with host-style requests, just skip it and use path-style for anything local
%% At some points once the mocks catch up, we should remove this ugly hack...
Host1 = erlang:iolist_to_binary(Host0),
IsLocalHost = maps:get(region, Client) =:= <<"local">>,
IsLocalHost = aws_client:region(Client) =:= <<"local">>,
Path = erlang:iolist_to_binary(Path0),
Host = case Bucket of
_ when not IsLocalHost andalso Bucket =/= undefined ->
erlang:iolist_to_binary(string:replace(Host1, <<Bucket/binary, ".">>, <<"">>, all));
_ ->
Host1
end,
Port = maps:get(port, Client),
Port = aws_client:port(Client),
aws_util:binary_join([Proto, <<"://">>, Host, <<":">>, Port, Path], <<"">>).

%%====================================================================
Expand Down