PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_oauth/deps/erlang-oauth/src/oauth_client.erl

https://code.google.com/p/zotonic/
Erlang | 149 lines | 111 code | 29 blank | 9 comment | 0 complexity | 56cc4a4079630de9f1fc044c6f0de3a8 MD5 | raw file
  1-module(oauth_client).
  2
  3-behaviour(gen_server).
  4
  5-export([access_token_params/1, deauthorize/1, get/2, get/3, get/4, get_access_token/2,
  6  get_access_token/3, get_access_token/4, get_request_token/2, get_request_token/3,
  7  get_request_token/4, start/1, start/2, start_link/1, start_link/2, stop/1]).
  8
  9-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
 10
 11%%============================================================================
 12%% API functions
 13%%============================================================================
 14
 15start(Consumer) ->
 16  gen_server:start(?MODULE, Consumer, []).
 17
 18start(ServerName, Consumer) ->
 19  gen_server:start(ServerName, ?MODULE, Consumer, []).
 20
 21start_link(Consumer) ->
 22  gen_server:start_link(?MODULE, Consumer, []).
 23
 24start_link(ServerName, Consumer) ->
 25  gen_server:start_link(ServerName, ?MODULE, Consumer, []).
 26
 27get_request_token(Client, URL) ->
 28  get_request_token(Client, URL, [], header).
 29
 30get_request_token(Client, URL, Params) ->
 31  gen_server:call(Client, {get_request_token, URL, Params, header}).
 32
 33get_request_token(Client, URL, Params, ParamsMethod) ->
 34  gen_server:call(Client, {get_request_token, URL, Params, ParamsMethod}).
 35
 36get_access_token(Client, URL) ->
 37  get_access_token(Client, URL, [], header).
 38
 39get_access_token(Client, URL, Params) ->
 40  gen_server:call(Client, {get_access_token, URL, Params, header}).
 41
 42get_access_token(Client, URL, Params, ParamsMethod) ->
 43  gen_server:call(Client, {get_access_token, URL, Params, ParamsMethod}).
 44
 45get(Client, URL) ->
 46  get(Client, URL, [], header).
 47
 48get(Client, URL, Params) ->
 49  gen_server:call(Client, {get, URL, Params, header}).
 50
 51get(Client, URL, Params, ParamsMethod) ->
 52  gen_server:call(Client, {get, URL, Params, ParamsMethod}).
 53
 54access_token_params(Client) ->
 55  gen_server:call(Client, {access_token_params}).
 56
 57deauthorize(Client) ->
 58  gen_server:cast(Client, deauthorize).
 59
 60stop(Client) ->
 61  gen_server:cast(Client, stop).
 62
 63%%============================================================================
 64%% Helper functions
 65%%============================================================================
 66
 67oauth_get(header, URL, Params, Consumer, Token, TokenSecret) ->
 68  Signed = oauth:signed_params("GET", URL, Params, Consumer, Token, TokenSecret),
 69  {AuthorizationParams, QueryParams} = lists:partition(fun({K, _}) -> lists:prefix("oauth_", K) end, Signed),
 70  Request = {oauth:uri(URL, QueryParams), [oauth:header(AuthorizationParams)]},
 71  http:request(get, Request, [{autoredirect, false}], []);
 72oauth_get(querystring, URL, Params, Consumer, Token, TokenSecret) ->
 73  oauth:get(URL, Params, Consumer, Token, TokenSecret).
 74
 75%%============================================================================
 76%% gen_server callbacks
 77%%============================================================================
 78
 79init(Consumer) ->
 80  {ok, {Consumer}}.
 81
 82handle_call({get_request_token, URL, Params, ParamsMethod}, _From, State={Consumer}) ->
 83  case oauth_get(ParamsMethod, URL, Params, Consumer, "", "") of
 84    {ok, Response} ->
 85      case oauth_http:response_code(Response) of
 86        200 ->
 87          RParams = oauth_http:response_params(Response),
 88          {reply, {ok, oauth:token(RParams)}, {Consumer, RParams}};
 89        _ ->
 90          {reply, Response, State}
 91      end;
 92    Error ->
 93      {reply, Error, State}
 94  end;
 95handle_call({get_access_token, URL, Params, ParamsMethod}, _From, State={Consumer, RParams}) ->
 96  case oauth_get(ParamsMethod, URL, Params, Consumer, oauth:token(RParams), oauth:token_secret(RParams)) of
 97    {ok, Response} ->
 98      case oauth_http:response_code(Response) of
 99        200 ->
100          AParams = oauth_http:response_params(Response),
101          {reply, ok, {Consumer, RParams, AParams}};
102        _ ->
103          {reply, Response, State}
104      end;
105    Error ->
106      {reply, Error, State}
107  end;
108handle_call({get, URL, Params, ParamsMethod}, _From, State={Consumer, _RParams, AParams}) ->
109  case oauth_get(ParamsMethod, URL, Params, Consumer, oauth:token(AParams), oauth:token_secret(AParams)) of
110    {ok, Response={{_, Status, _}, Headers, Body}} ->
111      case Status of
112        200 ->
113          case proplists:get_value("content-type", Headers) of
114            undefined ->
115              {reply, {ok, Headers, Body}, State};
116            ContentType ->
117              MediaType = hd(string:tokens(ContentType, ";")),
118              case lists:suffix("/xml", MediaType) orelse lists:suffix("+xml", MediaType) of
119                true ->
120                  {XML, []} = xmerl_scan:string(Body),
121                  {reply, {ok, Headers, XML}, State};
122                false ->
123                  {reply, {ok, Headers, Body}, State}
124              end
125          end;
126        _ ->
127          {reply, Response, State}
128      end;
129    Error ->
130      {reply, Error, State}
131  end;
132handle_call({access_token_params}, _From, State={_Consumer, _RParams, AParams}) ->
133  {reply, AParams, State}.
134
135handle_cast(deauthorize, {Consumer, _RParams}) ->
136  {noreply, {Consumer}};
137handle_cast(deauthorize, {Consumer, _RParams, _AParams}) ->
138  {noreply, {Consumer}};
139handle_cast(stop, State) ->
140  {stop, normal, State}.
141
142handle_info(_Msg, State) ->
143  {noreply, State}.
144
145code_change(_OldVsn, State, _Extra) ->
146  {ok, State}.
147
148terminate(normal, _State) ->
149  ok.