Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'lhttpc_options'

  • Loading branch information...
commit 8e6865bb3027c182387f755659306a551ada2d98 2 parents 06b7191 + 05a6ea3
Martin Rehfeld authored
View
3  include/dinerl_types.hrl
@@ -1,6 +1,7 @@
-type access_key_id() :: string().
-type secret_access_key() :: string().
-type zone() :: string().
+-type options() :: list().
-type token() :: string().
-type rfcdate() :: string().
-type endpoint() :: string().
@@ -15,7 +16,7 @@
-type jsonf() :: any().
--type clientarguments() :: {access_key_id(), secret_access_key(), zone(), token(), rfcdate(), integer()}.
+-type clientarguments() :: {access_key_id(), secret_access_key(), zone(), options(), token(), rfcdate(), integer()}.
-type method() :: batch_get_item | get_item | put_item | delete_item |
update_item | create_table | list_tables | describe_table |
View
42 src/dinerl.erl
@@ -5,7 +5,7 @@
-include("dinerl_types.hrl").
--export([setup/3, api/1, api/2, api/3]).
+-export([setup/3, setup/4, api/1, api/2, api/3]).
-export([create_table/4, create_table/5, delete_table/1, delete_table/2]).
-export([describe_table/1, describe_table/2, update_table/3, update_table/4]).
@@ -13,15 +13,25 @@
-export([delete_item/3, delete_item/4, get_item/3, get_item/4]).
-export([update_item/3, update_item/4]).
--export([update_data/3]).
+-export([update_data/4]).
-spec setup(access_key_id(), secret_access_key(), zone()) ->
{ok, clientarguments()}.
setup(AccessKeyId, SecretAccessKey, Zone) ->
+ setup(AccessKeyId, SecretAccessKey, Zone, [{max_connections, 5000}]).
+
+-spec setup(access_key_id(), secret_access_key(), zone(), options()) ->
+ {ok, clientarguments()} | {error, any()}.
+setup(AccessKeyId, SecretAccessKey, Zone, Options) ->
ets:new(?DINERL_DATA, [named_table, public]),
- R = update_data(AccessKeyId, SecretAccessKey, Zone),
- timer:apply_interval(1000, ?MODULE, update_data, [AccessKeyId, SecretAccessKey, Zone]),
- R.
+ case update_data(AccessKeyId, SecretAccessKey, Zone, Options) of
+ {ok, ClientArgs} ->
+ case timer:apply_interval(1000, ?MODULE, update_data, [AccessKeyId, SecretAccessKey, Zone, Options]) of
+ {ok, _TRef} -> {ok, ClientArgs};
+ Error -> Error
+ end;
+ Error -> Error
+ end.
-spec api(method()) ->result().
@@ -37,9 +47,9 @@ api(Name, Body, Timeout) ->
case catch(ets:lookup_element(?DINERL_DATA, ?ARGS_KEY, 2)) of
{'EXIT', {badarg, _}} ->
{error, missing_credentials, ""};
- {ApiAccessKeyId, ApiSecretAccessKey, Zone, ApiToken, Date, _} ->
+ {ApiAccessKeyId, ApiSecretAccessKey, Zone, Options, ApiToken, Date, _} ->
dinerl_client:api(ApiAccessKeyId, ApiSecretAccessKey, Zone,
- ApiToken, Date, Name, Body, Timeout)
+ ApiToken, Date, Name, Body, Timeout, Options)
end.
@@ -180,14 +190,15 @@ q() ->
%%
%% Every second it updates the Date part of the arguments
%% When within 120 seconds of the expiration of the token instead it refreshes also the token
--spec update_data(access_key_id(), secret_access_key(), zone()) ->
+-spec update_data(access_key_id(), secret_access_key(), zone(), options()) ->
{ok, clientarguments()}.
-update_data(AccessKeyId, SecretAccessKey, Zone) ->
+update_data(AccessKeyId, SecretAccessKey, Zone, Options) ->
case catch(ets:lookup_element(?DINERL_DATA, ?ARGS_KEY, 2)) of
{'EXIT', {badarg, _}} ->
CurrentApiAccessKeyId = "123",
CurrentApiSecretAccessKey = "123",
Zone = Zone,
+ Options = Options,
CurrentApiToken = "123",
CurrentExpirationSeconds = calendar:datetime_to_gregorian_seconds(erlang:universaltime());
@@ -195,12 +206,13 @@ update_data(AccessKeyId, SecretAccessKey, Zone) ->
{CurrentApiAccessKeyId,
CurrentApiSecretAccessKey,
Zone,
+ Options,
CurrentApiToken,
_Date,
CurrentExpirationSeconds} = Result
end,
-
+
NewDate = httpd_util:rfc1123_date(),
NowSeconds = calendar:datetime_to_gregorian_seconds(erlang:universaltime()),
SecondsToExpire = CurrentExpirationSeconds - NowSeconds,
@@ -208,20 +220,20 @@ update_data(AccessKeyId, SecretAccessKey, Zone) ->
case SecondsToExpire < 120 of
true ->
NewToken = iam:get_session_token(AccessKeyId, SecretAccessKey),
-
+
ExpirationString = proplists:get_value(expiration, NewToken),
ApiAccessKeyId = proplists:get_value(access_key_id, NewToken),
ApiSecretAccessKey = proplists:get_value(secret_access_key, NewToken),
ApiToken = proplists:get_value(token, NewToken),
ExpirationSeconds = calendar:datetime_to_gregorian_seconds(iso8601:parse(ExpirationString)),
-
- NewArgs = {ApiAccessKeyId, ApiSecretAccessKey, Zone, ApiToken, NewDate, ExpirationSeconds};
+
+ NewArgs = {ApiAccessKeyId, ApiSecretAccessKey, Zone, Options, ApiToken, NewDate, ExpirationSeconds};
false ->
NewArgs = {CurrentApiAccessKeyId, CurrentApiSecretAccessKey,
- Zone, CurrentApiToken, NewDate, CurrentExpirationSeconds}
+ Zone, Options, CurrentApiToken, NewDate, CurrentExpirationSeconds}
end,
-
+
ets:insert(?DINERL_DATA, {?ARGS_KEY, NewArgs}),
{ok, NewArgs}.
View
13 src/dinerl_client.erl
@@ -3,11 +3,11 @@
-include("dinerl_types.hrl").
--export([api/7, api/8]).
+-export([api/7, api/8, api/9]).
%%
%% Item related operations
-%%
+%%
-spec method_name(method()) -> string().
method_name(batch_get_item) ->
"DynamoDBv20110924.BatchGetItem";
@@ -46,13 +46,18 @@ method_name(scan) ->
-spec api(access_key_id(), secret_access_key(), zone(),
token(), rfcdate(), method(), any()) -> result().
api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body) ->
- api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body, undefined).
+ api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body, undefined, []).
-spec api(access_key_id(), secret_access_key(), zone(),
token(), rfcdate(), method(), any(), integer()) -> result().
api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body, Timeout) ->
+ api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body, Timeout, []).
+
+-spec api(access_key_id(), secret_access_key(), zone(),
+ token(), rfcdate(), method(), any(), integer(), options()) -> result().
+api(AccessKeyId, SecretAccessKey, Zone, Token, RFCDate, Name, Body, Timeout, Options) ->
case dynamodb:call(AccessKeyId, SecretAccessKey, Zone, method_name(Name),
- Token, RFCDate, dmochijson2:encode(Body), Timeout) of
+ Token, RFCDate, dmochijson2:encode(Body), Timeout, Options) of
{ok, Response} ->
{ok, dmochijson2:decode(Response)};
{error, Code, Reason} ->
View
34 src/dynamodb.erl
@@ -3,7 +3,7 @@
-include("dinerl_types.hrl").
--export([endpoint/1, signature_header/7, call/7, call/8]).
+-export([endpoint/1, signature_header/7, call/7, call/8, call/9]).
-spec endpoint(zone()) -> endpoint().
@@ -18,8 +18,8 @@ endpoint("eu-west-1" ++ _R) -> "dynamodb.eu-west-1.amazonaws.com".
signature_header(AccessKeyId, SecretAccessKey, Target, Token, Date, EndPoint, Body) ->
SignString = ["POST", $\n,
- "/", $\n,
- $\n,
+ "/", $\n,
+ $\n,
"host:", EndPoint, $\n,
"x-amz-date:", Date, $\n,
"x-amz-security-token:", Token, $\n,
@@ -29,39 +29,45 @@ signature_header(AccessKeyId, SecretAccessKey, Target, Token, Date, EndPoint, Bo
StringToSign = crypto:sha(SignString),
Signature = base64:encode_to_string(crypto:sha_mac(SecretAccessKey, StringToSign)),
{ok,
- {"x-amzn-authorization",
+ {"x-amzn-authorization",
["AWS3 AWSAccessKeyId=",
AccessKeyId,
",Algorithm=HmacSHA1,SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-target,Signature=",
Signature]}}.
--spec call(access_key_id(), secret_access_key(),
+-spec call(access_key_id(), secret_access_key(),
zone(), string(), token(), rfcdate(),
any()) -> result().
call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body) ->
- call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, 1000).
+ call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, 1000, []).
--spec call(access_key_id(), secret_access_key(),
+-spec call(access_key_id(), secret_access_key(),
zone(), string(), token(), rfcdate(),
any(), integer()) -> result().
call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, undefined) ->
- call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, 1000);
-call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, Timeout) ->
+ call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, 1000, []).
+
+-spec call(access_key_id(), secret_access_key(),
+ zone(), string(), token(), rfcdate(),
+ any(), integer(), options()) -> result().
+call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, undefined, Options) ->
+ call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, 1000, Options);
+call(AccessKeyId, SecretAccessKey, Zone, Target, Token, RFCDate, Body, Timeout, Options) ->
EndPoint = endpoint(Zone),
{ok, SHeader} = signature_header(AccessKeyId, SecretAccessKey, Target,
Token, RFCDate, EndPoint, Body),
- submit("http://" ++ EndPoint ++ "/",
+ submit("http://" ++ EndPoint ++ "/",
[{"content-type", "application/x-amz-json-1.0"},
{"x-amz-date", RFCDate},
{"x-amz-security-token", Token},
- {"x-amz-target", Target}, SHeader], Body, Timeout).
+ {"x-amz-target", Target}, SHeader], Body, Timeout, Options).
--spec submit(endpoint(), headers(), any(), integer()) -> result().
-submit(Endpoint, Headers, Body, Timeout) ->
+-spec submit(endpoint(), headers(), any(), integer(), options()) -> result().
+submit(Endpoint, Headers, Body, Timeout, Options) ->
%io:format("Request:~nHeaders:~p~nBody:~n~p~n~n", [Headers, iolist_to_binary(Body)]),
- case lhttpc:request(Endpoint, "POST", Headers, Body, Timeout, [{max_connections, 5000}]) of
+ case lhttpc:request(Endpoint, "POST", Headers, Body, Timeout, Options) of
{ok, {{200, _}, _Headers, Response}} ->
%io:format("Response: ~p~n", [Response]),
{ok, Response};
Please sign in to comment.
Something went wrong with that request. Please try again.