Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add option for sending protocol parameters in either the header or qu…
…erystring.
  • Loading branch information
Tim Fletcher committed Sep 26, 2009
1 parent 1bcd44f commit 9d00161bcd47498d753feae1b81e48f72a595a09
Showing 1 changed file with 28 additions and 17 deletions.
@@ -2,9 +2,9 @@

-behaviour(gen_server).

-export([access_token_params/1, deauthorize/1, get/2, get/3, get_access_token/2,
get_access_token/3, get_request_token/2, get_request_token/3, start/1, start/2,
start_link/1, start_link/2, stop/1]).
-export([access_token_params/1, deauthorize/1, get/2, get/3, get/4, get_access_token/2,
get_access_token/3, get_access_token/4, get_request_token/2, get_request_token/3,
get_request_token/4, start/1, start/2, start_link/1, start_link/2, stop/1]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).

@@ -25,22 +25,31 @@ start_link(ServerName, Consumer) ->
gen_server:start_link(ServerName, ?MODULE, Consumer, []).

get_request_token(Client, URL) ->
get_request_token(Client, URL, []).
get_request_token(Client, URL, [], header).

get_request_token(Client, URL, Params) ->
gen_server:call(Client, {get_request_token, URL, Params}).
gen_server:call(Client, {get_request_token, URL, Params, header}).

get_request_token(Client, URL, Params, ParamsMethod) ->
gen_server:call(Client, {get_request_token, URL, Params, ParamsMethod}).

get_access_token(Client, URL) ->
get_access_token(Client, URL, []).
get_access_token(Client, URL, [], header).

get_access_token(Client, URL, Params) ->
gen_server:call(Client, {get_access_token, URL, Params}).
gen_server:call(Client, {get_access_token, URL, Params, header}).

get_access_token(Client, URL, Params, ParamsMethod) ->
gen_server:call(Client, {get_access_token, URL, Params, ParamsMethod}).

get(Client, URL) ->
get(Client, URL, []).
get(Client, URL, [], header).

get(Client, URL, Params) ->
gen_server:call(Client, {get, URL, Params}).
gen_server:call(Client, {get, URL, Params, header}).

get(Client, URL, Params, ParamsMethod) ->
gen_server:call(Client, {get, URL, Params, ParamsMethod}).

access_token_params(Client) ->
gen_server:call(Client, {access_token_params}).
@@ -55,11 +64,13 @@ stop(Client) ->
%% Helper functions
%%============================================================================

oauth_get(URL, Params, Consumer, Token, TokenSecret) ->
oauth_get(header, URL, Params, Consumer, Token, TokenSecret) ->
Signed = oauth:signed_params("GET", URL, Params, Consumer, Token, TokenSecret),
{AuthorizationParams, QueryParams} = lists:partition(fun({K, _}) -> lists:prefix("oauth_", K) end, Signed),
Request = {oauth:uri(URL, QueryParams), [oauth:header(AuthorizationParams)]},
http:request(get, Request, [{autoredirect, false}], []).
http:request(get, Request, [{autoredirect, false}], []);
oauth_get(querystring, URL, Params, Consumer, Token, TokenSecret) ->
oauth:get(URL, Params, Consumer, Token, TokenSecret).

%%============================================================================
%% gen_server callbacks
@@ -68,8 +79,8 @@ oauth_get(URL, Params, Consumer, Token, TokenSecret) ->
init(Consumer) ->
{ok, {Consumer}}.

handle_call({get_request_token, URL, Params}, _From, State={Consumer}) ->
case oauth_get(URL, Params, Consumer, "", "") of
handle_call({get_request_token, URL, Params, ParamsMethod}, _From, State={Consumer}) ->
case oauth_get(ParamsMethod, URL, Params, Consumer, "", "") of
{ok, Response} ->
case oauth_http:response_code(Response) of
200 ->
@@ -81,8 +92,8 @@ handle_call({get_request_token, URL, Params}, _From, State={Consumer}) ->
Error ->
{reply, Error, State}
end;
handle_call({get_access_token, URL, Params}, _From, State={Consumer, RParams}) ->
case oauth_get(URL, Params, Consumer, oauth:token(RParams), oauth:token_secret(RParams)) of
handle_call({get_access_token, URL, Params, ParamsMethod}, _From, State={Consumer, RParams}) ->
case oauth_get(ParamsMethod, URL, Params, Consumer, oauth:token(RParams), oauth:token_secret(RParams)) of
{ok, Response} ->
case oauth_http:response_code(Response) of
200 ->
@@ -94,8 +105,8 @@ handle_call({get_access_token, URL, Params}, _From, State={Consumer, RParams}) -
Error ->
{reply, Error, State}
end;
handle_call({get, URL, Params}, _From, State={Consumer, _RParams, AParams}) ->
case oauth_get(URL, Params, Consumer, oauth:token(AParams), oauth:token_secret(AParams)) of
handle_call({get, URL, Params, ParamsMethod}, _From, State={Consumer, _RParams, AParams}) ->
case oauth_get(ParamsMethod, URL, Params, Consumer, oauth:token(AParams), oauth:token_secret(AParams)) of
{ok, Response={{_, Status, _}, Headers, Body}} ->
case Status of
200 ->

0 comments on commit 9d00161

Please sign in to comment.