Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update oauth_termie to new oauth_client usage.

  • Loading branch information...
commit f6ad26167a3dd8fb748befe8033f91049e5f2624 1 parent b986b11
@tim tim authored
Showing with 45 additions and 51 deletions.
  1. +45 −51 src/oauth_termie.erl
View
96 src/oauth_termie.erl
@@ -1,60 +1,54 @@
%%
%% This is an example client for the test server at http://term.ie/oauth/example/.
%%
-%% Use echo/0 to call the server with the default params and the default
-%% signature method (HMAC-SHA1). Use echo/1 to specify *either* different
-%% params, *or* a different signature method. Use echo/2 if you want to
-%% specify *both* the params and the signature method.
+%% Example usage:
+%%
+%% $ make
+%% ...
+%% $ erl -pa ebin -pa path/to/erlang-oauth/ebin -s crypto -s inets
+%% ...
+%% 1> {ok, Client} = oauth_termie:start().
+%% ...
+%% 2> {ok, _Token} = oauth_termie:get_request_token(Client).
+%% ...
+%% 3> ok = oauth_termie:get_access_token(Client).
+%% ...
+%% 4> oauth_termie:echo(Client).
+%% ...
%%
-
-module(oauth_termie).
-compile(export_all).
--export([echo/0, echo/1, echo/2]).
-
-
-echo() ->
- echo(hmac_sha1).
-
-echo(Params) when is_list(Params) ->
- echo(Params, hmac_sha1);
-echo(SigMethod) when is_atom(SigMethod) ->
- echo([{"bar", "baz"}, {"method", "foo"}], SigMethod).
-
-echo(Params, SigMethod) when is_atom(SigMethod) ->
- echo(Params, {"key", consumer_secret(SigMethod), SigMethod});
-echo(Params, Consumer) ->
- oauth_client:get(url(oauth_termie_request_token_url), [], Consumer, "", "", fun(Response) ->
- echo(Params, Consumer, oauth_http:response_params(Response))
- end).
-
-echo(Params, Consumer, RParams) ->
- oauth_client:get(url(oauth_termie_access_token_url), [], Consumer, RParams, fun(Response) ->
- echo(Params, Consumer, RParams, oauth_http:response_params(Response))
- end).
-
-echo(Params, Consumer, _, AParams) ->
- oauth_client:get(url(oauth_termie_echo_url), Params, Consumer, AParams, fun(Response) ->
- {ok, lists:keysort(1, oauth_http:response_params(Response))}
- end).
-
-consumer_secret(plaintext) ->
- "secret";
-consumer_secret(hmac_sha1) ->
- "secret";
-consumer_secret(rsa_sha1) ->
- "data/rsa_pkey.pem".
-
-default(oauth_termie_request_token_url) ->
- "http://term.ie/oauth/example/request_token.php";
-default(oauth_termie_access_token_url) ->
- "http://term.ie/oauth/example/access_token.php";
-default(oauth_termie_echo_url) ->
- "http://term.ie/oauth/example/echo_api.php".
-
-url(Name) ->
- case get(Name) of
- undefined -> default(Name);
- Other -> Other
+start() ->
+ start(hmac_sha1).
+
+start(SigMethod) when is_atom(SigMethod) ->
+ start(consumer(SigMethod));
+start(Consumer) ->
+ oauth_client:start(Consumer).
+
+consumer(rsa_sha1) ->
+ {"key", "data/rsa_pkey.pem", rsa_sha1};
+consumer(SigMethod) ->
+ {"key", "secret", SigMethod}.
+
+get_request_token(Client) ->
+ URL = "http://term.ie/oauth/example/request_token.php",
+ oauth_client:get_request_token(Client, URL, [], querystring).
+
+get_access_token(Client) ->
+ URL = "http://term.ie/oauth/example/access_token.php",
+ oauth_client:get_access_token(Client, URL, [], querystring).
+
+echo(Client) ->
+ echo(Client, [{"bar", "baz"}, {"method", "foo"}]).
+
+echo(Client, Params) ->
+ URL = "http://term.ie/oauth/example/echo_api.php",
+ case oauth_client:get(Client, URL, Params, querystring) of
+ {ok, _Headers, Body} ->
+ {ok, lists:keysort(1, oauth_uri:params_from_string(Body))};
+ Error ->
+ Error
end.
Please sign in to comment.
Something went wrong with that request. Please try again.