Skip to content
Browse files

updating to 0.3.0, fixes #9

This release is not directly compatiable with the previous releases as the API
has been updated and extended. Namely the service wrapper request calls have
been made more generic to allow for various HTTP methods as well as flexibility
when indicating the name of provider credentials, such as access_token.

Specifically this addresses an issue where a service uses a non-standard naming
scheme, such as Foursquare. Previously there was no easy way to overide the
parameter name and internally the variable was required or the method would
throw an error. Now the method has been abstracted to allow for generic naming
as well as dynamic HTTP methods.

Also the API has been extended and modified to be more closely aligned with
Request's. For instance, all service wrappers now have get(), post(), put(),
and delete() methods and corrospond to their respective HTTP methods. Some
naming conventions were altered, such as the http_method parameter which is now
simply method. (This also mirrors Request's API.)
  • Loading branch information...
1 parent 811aea0 commit 7d1368a1e45527d941d7b072173cf4409c2dd591 @maxcountryman maxcountryman committed Apr 23, 2012
View
24 README.markdown
@@ -46,7 +46,7 @@ object:
Then get an OAuth 1.0 request token:
request_token, request_token_secret = \
- twitter.get_request_token(http_method='GET')
+ twitter.get_request_token(method='GET')
Go through the authentication flow. Since our example is a simple console
application, Twitter will give you a PIN to enter.
@@ -58,10 +58,10 @@ application, Twitter will give you a PIN to enter.
Exchange the authorized request token for an access token:
- response = twitter.get_access_token(request_token,
- request_token_secret,
- http_method='GET',
- oauth_verifier=pin)
+ response = twitter.get_access_token('GET',
+ request_token=request_token,
+ request_token_secret=request_token_secret,
+ params={'oauth_verifier': pin})
data = response.content
access_token = data['oauth_token']
@@ -72,13 +72,11 @@ And now we can fetch our Twitter timeline!
params = {'include_rts': 1, # Include retweets
'count': 10} # 10 tweets
- response = twitter.request(
- 'GET',
- 'https://api.twitter.com/1/statuses/home_timeline.json',
- access_token,
- access_token_secret,
- header_auth=True,
- params=params)
+ response = twitter.get('https://api.twitter.com/1/statuses/home_timeline.json',
+ params=params,
+ access_token=access_token,
+ access_token_secret=access_token_secret,
+ header_auth=True)
for i, tweet in enumerate(response.content, 1):
handle = tweet['user']['screen_name'].encode('utf-8')
@@ -96,4 +94,4 @@ The Sphinx-compiled documentation is available here: [http://packages.python.org
## Copyright and License
Rauth is Copyright (c) 2012 litl, LLC and licensed under the MIT license.
-See the LICENSE file for full details.
+See the LICENSE file for full details.
View
BIN docs/_build/doctrees/environment.pickle
Binary file not shown.
View
BIN docs/_build/doctrees/index.doctree
Binary file not shown.
View
2 docs/_build/html/.buildinfo
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
-config: 36e43cbb20df62d578e2c8553d26b9cd
+config: 61a190c5a63c87f3f4475d76b84e4bd5
tags: fbb0d17656682115ca4d033fb2f83ba1
View
6 docs/_build/html/genindex.html
@@ -9,15 +9,15 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Index &mdash; rauth 0.2.3 documentation</title>
+ <title>Index &mdash; rauth 0.3.0 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
- VERSION: '0.2.3',
+ VERSION: '0.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
@@ -26,7 +26,7 @@
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="top" title="rauth 0.2.3 documentation" href="index.html" />
+ <link rel="top" title="rauth 0.3.0 documentation" href="index.html" />
</head>
<body>
View
88 docs/_build/html/index.html
@@ -7,15 +7,15 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Rauth &mdash; rauth 0.2.3 documentation</title>
+ <title>Rauth &mdash; rauth 0.3.0 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
- VERSION: '0.2.3',
+ VERSION: '0.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
@@ -24,7 +24,7 @@
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="top" title="rauth 0.2.3 documentation" href="#" />
+ <link rel="top" title="rauth 0.3.0 documentation" href="#" />
</head>
<body>
@@ -149,9 +149,10 @@
<span class="c"># once the above URL is consumed by a client we can ask for an access</span>
<span class="c"># token. note that the code is retrieved from the redirect URL above,</span>
<span class="c"># as set by the provider</span>
-<span class="n">token</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">get_access_token</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="s">&#39;foobar&#39;</span><span class="p">,</span>
- <span class="n">grant_type</span><span class="o">=</span><span class="s">&#39;authorization_code&#39;</span><span class="p">,</span>
- <span class="n">redirect_uri</span><span class="o">=</span><span class="s">&#39;http://example.com/&#39;</span><span class="p">)</span>
+<span class="n">data</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="s">&#39;foobar&#39;</span><span class="p">,</span>
+ <span class="n">grant_type</span><span class="o">=</span><span class="s">&#39;authorization_code&#39;</span><span class="p">,</span>
+ <span class="n">redirect_uri</span><span class="o">=</span><span class="s">&#39;http://example.com/&#39;</span><span class="p">)</span>
+<span class="n">token</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">get_access_token</span><span class="p">(</span><span class="s">&#39;POST&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
@@ -172,16 +173,17 @@
</table>
<dl class="method">
<dt id="rauth.service.OAuth2Service.get_access_token">
-<tt class="descname">get_access_token</tt><big>(</big><em>grant_type='authorization_code'</em>, <em>**data</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth2Service.get_access_token" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">get_access_token</tt><big>(</big><em>method='POST'</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth2Service.get_access_token" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieves the access token.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be used.
+Defaults to &#8216;POST&#8217;.</li>
<li><strong>grant_type</strong> &#8211; The grant type. Deaults to &#8216;authorization_code&#8217;.</li>
-<li><strong>data</strong> &#8211; Keyworded arguments to be passed in the body of the
-request.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
@@ -199,7 +201,7 @@
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>reponse_type</strong> &#8211; The response type. Defaults to &#8216;code&#8217;.</li>
-<li><strong>params</strong> &#8211; Additional keyworded arguments to be added to the
+<li><strong>**params</strong> &#8211; Additional keyworded arguments to be added to the
request querystring.</li>
</ul>
</td>
@@ -210,24 +212,17 @@
<dl class="method">
<dt id="rauth.service.OAuth2Service.request">
-<tt class="descname">request</tt><big>(</big><em>http_method</em>, <em>url</em>, <em>access_token=None</em>, <em>params=None</em>, <em>data=None</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth2Service.request" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">request</tt><big>(</big><em>method</em>, <em>url</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth2Service.request" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a request to an OAuth 2.0 endpoint, properly wrapped around
requests.</p>
-<p>The first time an access token is provided it will be saved on the
-object for convenience.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
-<li><strong>http_method</strong> &#8211; A string representation of the HTTP method to be
-used.</li>
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be used.</li>
<li><strong>url</strong> &#8211; The resource to be requested.</li>
-<li><strong>access_token</strong> &#8211; The access token as returned by
-<tt class="xref py py-class docutils literal"><span class="pre">get_access_token</span></tt>.</li>
-<li><strong>params</strong> &#8211; Additional arguments to be added to the request
-querystring.</li>
-<li><strong>data</strong> &#8211; Additional data to be included in the request body.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
@@ -267,16 +262,15 @@
<div class="highlight-python"><div class="highlight"><pre><span class="n">authorize_url</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">get_authorize_url</span><span class="p">(</span><span class="n">request_token</span><span class="p">)</span>
</pre></div>
</div>
-<p>Once the client has authorized the request it is not possible to retrieve
+<p>Once the client has authorized the request it is now possible to retrieve
an access token. Do so as follows:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="n">response</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="n">get_access_token</span><span class="p">(</span><span class="n">request_token</span><span class="p">,</span>
- <span class="n">request_token_secret</span><span class="p">,</span>
- <span class="n">http_method</span><span class="o">=</span><span class="s">&#39;GET&#39;</span><span class="p">)</span>
+<div class="highlight-python"><pre>response = service.get_access_token(method='GET'
+ request_token=request_token,
+ request_token_secret=request_token_secret)
-<span class="c"># access tokens are returned in the response dictionary</span>
-<span class="n">response</span><span class="p">[</span><span class="s">&#39;oauth_token&#39;</span><span class="p">]</span>
-<span class="n">response</span><span class="p">[</span><span class="s">&#39;oauth_key&#39;</span><span class="p">]</span>
-</pre></div>
+# access tokens are returned in the response dictionary
+response['oauth_token']
+response['oauth_key']</pre>
</div>
<p>Finally the <tt class="xref py py-class docutils literal"><span class="pre">get_authenticated_session</span></tt> method returns a wrapped
session and can be used once the access token has been made available.
@@ -300,21 +294,20 @@
</table>
<dl class="method">
<dt id="rauth.service.OAuth1Service.get_access_token">
-<tt class="descname">get_access_token</tt><big>(</big><em>request_token</em>, <em>request_token_secret</em>, <em>http_method</em>, <em>**params</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.get_access_token" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">get_access_token</tt><big>(</big><em>method='GET'</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.get_access_token" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieves the access token.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be
+used.</li>
<li><strong>request_token</strong> &#8211; The request token as returned by
<tt class="xref py py-class docutils literal"><span class="pre">get_request_token</span></tt>.</li>
<li><strong>request_token_secret</strong> &#8211; The request token secret as returned by
<tt class="xref py py-class docutils literal"><span class="pre">get_request_token</span></tt>.</li>
-<li><strong>http_method</strong> &#8211; A string representation of the HTTP method to be
-used.</li>
-<li><strong>params</strong> &#8211; Additional keyworded arguments to be added to the
-request querystring.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
@@ -354,7 +347,7 @@
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>request_token</strong> &#8211; The request token as returned by
<tt class="xref py py-class docutils literal"><span class="pre">get_request_token</span></tt>.</li>
-<li><strong>params</strong> &#8211; Additional keyworded arguments to be added to the
+<li><strong>**params</strong> &#8211; Additional keyworded arguments to be added to the
request querystring.</li>
</ul>
</td>
@@ -365,17 +358,15 @@
<dl class="method">
<dt id="rauth.service.OAuth1Service.get_request_token">
-<tt class="descname">get_request_token</tt><big>(</big><em>http_method</em>, <em>**data</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.get_request_token" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">get_request_token</tt><big>(</big><em>method='GET'</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.get_request_token" title="Permalink to this definition">¶</a></dt>
<dd><p>Gets a request token from the request token endpoint.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
-<li><strong>http_method</strong> &#8211; A string representation of the HTTP method to be
-used.</li>
-<li><strong>data</strong> &#8211; Keyworded arguments to be passed in the body of the
-request.</li>
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be used.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
@@ -385,24 +376,23 @@
<dl class="method">
<dt id="rauth.service.OAuth1Service.request">
-<tt class="descname">request</tt><big>(</big><em>http_method</em>, <em>url</em>, <em>access_token</em>, <em>access_token_secret</em>, <em>header_auth=False</em>, <em>params=None</em>, <em>data=None</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.request" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">request</tt><big>(</big><em>method</em>, <em>url</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OAuth1Service.request" title="Permalink to this definition">¶</a></dt>
<dd><p>Makes a request using <tt class="xref py py-class docutils literal"><span class="pre">_construct_session</span></tt>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
-<li><strong>http_method</strong> &#8211; A string representation of the HTTP method to be
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be
used.</li>
<li><strong>url</strong> &#8211; The resource to be requested.</li>
<li><strong>access_token</strong> &#8211; The access token as returned by
<tt class="xref py py-class docutils literal"><span class="pre">get_access_token</span></tt>.</li>
<li><strong>access_token_secret</strong> &#8211; The access token secret as returned by
<tt class="xref py py-class docutils literal"><span class="pre">get_access_token</span></tt>.</li>
<li><strong>header_auth</strong> &#8211; Authenication via header, defaults to False.</li>
-<li><strong>params</strong> &#8211; Additional arguments to be added to the request
-querystring.</li>
-<li><strong>data</strong> &#8211; Additional data to be included in the request body.</li>
+<li><strong>allow_redirects</strong> &#8211; Allows a request to redirect, defaults to True.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
@@ -465,7 +455,7 @@
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>remote_user</strong> &#8211; This is the oflyRemoteUser param. Defaults to None.</li>
<li><strong>redirect_uri</strong> &#8211; This is the oflyCallbackUrl. Defaults to None.</li>
-<li><strong>params</strong> &#8211; Additional keyworded arguments to be added to the
+<li><strong>**params</strong> &#8211; Additional keyworded arguments to be added to the
request querystring.</li>
</ul>
</td>
@@ -476,21 +466,19 @@
<dl class="method">
<dt id="rauth.service.OflyService.request">
-<tt class="descname">request</tt><big>(</big><em>http_method</em>, <em>url</em>, <em>header_auth=False</em>, <em>params=None</em>, <em>data=None</em><big>)</big><a class="headerlink" href="#rauth.service.OflyService.request" title="Permalink to this definition">¶</a></dt>
+<tt class="descname">request</tt><big>(</big><em>method</em>, <em>url</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#rauth.service.OflyService.request" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a request to an Ofly endpoint, properly wrapped around
requests.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
-<li><strong>http_method</strong> &#8211; A string representation of the HTTP method to be
+<li><strong>method</strong> &#8211; A string representation of the HTTP method to be
used.</li>
<li><strong>url</strong> &#8211; The resource to be requested.</li>
<li><strong>header_auth</strong> &#8211; Authenication via header, defaults to False.</li>
-<li><strong>params</strong> &#8211; Additional arguments to be added to the request
-querystring.</li>
-<li><strong>data</strong> &#8211; Additional data to be included in the request body.</li>
+<li><strong>**kwargs</strong> &#8211; Optional arguments. Same as Requests.</li>
</ul>
</td>
</tr>
View
BIN docs/_build/html/objects.inv
Binary file not shown.
View
6 docs/_build/html/py-modindex.html
@@ -7,15 +7,15 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Python Module Index &mdash; rauth 0.2.3 documentation</title>
+ <title>Python Module Index &mdash; rauth 0.3.0 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
- VERSION: '0.2.3',
+ VERSION: '0.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
@@ -24,7 +24,7 @@
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="top" title="rauth 0.2.3 documentation" href="index.html" />
+ <link rel="top" title="rauth 0.3.0 documentation" href="index.html" />
<script type="text/javascript">
View
6 docs/_build/html/search.html
@@ -7,15 +7,15 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Search &mdash; rauth 0.2.3 documentation</title>
+ <title>Search &mdash; rauth 0.3.0 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
- VERSION: '0.2.3',
+ VERSION: '0.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
@@ -25,7 +25,7 @@
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/searchtools.js"></script>
- <link rel="top" title="rauth 0.2.3 documentation" href="index.html" />
+ <link rel="top" title="rauth 0.3.0 documentation" href="index.html" />
<script type="text/javascript">
jQuery(function() { Search.loadIndex("searchindex.js"); });
</script>
View
2 docs/_build/html/searchindex.js
@@ -1 +1 @@
-Search.setIndex({objects:{"":{rauth:[0,0,1,""]},"rauth.service.OAuth1Service":{get_authenticated_session:[0,1,1,""],get_request_token:[0,1,1,""],get_access_token:[0,1,1,""],request:[0,1,1,""],get_authorize_url:[0,1,1,""]},"rauth.service":{OflyService:[0,2,1,""],OAuth2Service:[0,2,1,""],OAuth1Service:[0,2,1,""]},"rauth.hook.OAuth1Hook":{auth_header:[0,1,1,""],oauth_params:[0,3,1,""]},"rauth.hook":{OAuth1Hook:[0,2,1,""]},"rauth.service.OflyService":{request:[0,1,1,""],get_authorize_url:[0,1,1,""]},"rauth.service.OAuth2Service":{get_authorize_url:[0,1,1,""],request:[0,1,1,""],get_access_token:[0,1,1,""]}},terms:{represent:0,all:[],code:0,authorization_cod:0,when:[],auth_head:0,random:[],obtain:0,session:0,through:0,follow:0,token_kei:0,paramet:0,redirect:0,whose:[],onli:0,acquir:0,also:0,flask:[],"_csrf_token":[],activ:[],param:0,should:0,reinit:0,response_typ:0,onc:0,input:[],hidden:[],main:[],might:0,modul:0,futur:0,applic:0,global:[],"return":0,oauth1hook:0,thei:[],get:0,python:0,than:0,initi:[],defaul:0,util:0,instanc:[],webauth:[],pre_request:0,csrf:[],request_token:0,resourc:0,now:0,presum:0,string:0,like:0,name:0,specif:0,header:0,possibl:0,"_construct_sess":0,secret:0,authent:0,fals:0,provid:0,achiev:[],exampl:0,necessari:0,contain:0,cooki:[],querystr:0,ofli:0,deault:0,view:[],oflyservic:0,www:0,set:0,primari:[],signatur:0,natur:0,interact:0,some:0,prehook:0,reponse_typ:0,consum:0,ensur:0,see:0,http_method:0,design:[],expect:0,pass:0,fail:[],hash:[],generate_authorization_head:[],servic:0,append:0,variabl:0,subsequ:0,oflycallsignatur:0,flaskext:[],influenc:[],assum:0,someth:0,token:0,superb:0,content:0,enough:0,render_templ:[],header_auth:0,django:[],"import":0,get_authorization_url:0,method:0,attribut:0,altern:0,intial:0,after:0,run:[],kei:0,quit:0,gener:0,usag:0,accordingli:0,here:0,standard:0,consumer_secret:0,extens:0,step:0,base:[],whereupon:0,dictionari:0,ask:0,post:[],valid:0,valu:[],jinja:[],csrftoken:[],both:[],seasurf:[],would:0,capabl:0,similar:0,could:0,current:0,http:0,oauth2servic:0,get_access_token:0,turn:0,redirect_uri:0,oauth_token:0,logic:[],setup:0,implement:0,com:0,assign:[],commonli:0,first:0,request_token_secret:0,pleas:0,rang:[],via:0,simpli:0,although:0,directli:0,point:0,foobar:0,app:[],rather:0,sign:0,easy_instal:0,oauth_request:0,hook:0,api:0,oauth_kei:0,send:0,wrap:0,instal:0,hmacsha1signatur:0,miss:[],your:[],middlewar:[],backend:[],rauth:0,given:0,doe:0,from:0,wai:0,start:0,addition:0,top:0,support:0,visit:0,two:0,been:0,avail:0,attach:0,reli:0,call:0,includ:0,handl:0,addit:0,type:0,"final":0,more:0,"function":0,head:[],wrapper:0,option:0,form:[],conveni:0,exempt:[],rsa:[],oauth1servic:0,oflycallbackurl:0,made:0,bodi:0,templat:[],oauth:0,request_token_url:0,get_authenticated_sess:0,"true":0,easiest:0,access_token_url:0,rsa1:0,oauth1:0,none:0,endpoint:0,retriev:0,look:[],keyword:0,some_view:[],"default":0,wish:[],secret_kei:[],access:0,spec:0,can:0,abov:0,oauth_param:0,other:[],access_token_secret:0,def:[],pre:0,oauth2:[],have:0,generate_oauth_param:[],process:0,request:0,csrf_token:[],respons:0,shutterfli:0,argument:0,indic:[],get_authorize_url:0,direct:0,packag:0,properli:0,want:0,deliv:0,pip:0,sha1:0,exclud:[],check:[],"__init__":0,around:0,access_token:0,readi:0,realm:0,variou:0,authen:0,author:0,receiv:0,oauthhook:0,make:0,format:0,excel:0,init_app:[],consumer_kei:0,note:0,field:[],need:0,html:[],pair:0,split:0,which:0,relav:0,before_request:[],instead:0,you:0,document:0,simpl:0,updat:0,oflyremoteus:0,simplifi:0,begin:0,sure:0,unless:[],thi:0,trace:[],normal:0,our:0,proper:0,object:0,upon:0,decor:[],sfly:0,rout:[],most:0,render:[],specifi:[],establish:0,streamlin:0,mai:0,oauth_sess:0,construct:0,data:0,"class":0,built:0,get_request_token:0,simplic:0,remote_us:0,off:[],save:0,url:0,bind:0,grant:0,well:0,uri:0,produc:0,inform:0,client:0,command:0,allow:0,time:0,grant_typ:0,salt:[],authorize_url:0,usual:0},objtypes:{"0":"py:module","1":"py:method","2":"py:class","3":"py:attribute"},titles:["Rauth"],objnames:{"0":["py","module","Python module"],"1":["py","method","Python method"],"2":["py","class","Python class"],"3":["py","attribute","Python attribute"]},filenames:["index"]})
+Search.setIndex({objects:{"":{rauth:[0,0,1,""]},"rauth.service.OAuth1Service":{get_authenticated_session:[0,1,1,""],get_request_token:[0,1,1,""],get_access_token:[0,1,1,""],request:[0,1,1,""],get_authorize_url:[0,1,1,""]},"rauth.service":{OflyService:[0,2,1,""],OAuth2Service:[0,2,1,""],OAuth1Service:[0,2,1,""]},"rauth.hook.OAuth1Hook":{auth_header:[0,1,1,""],oauth_params:[0,3,1,""]},"rauth.hook":{OAuth1Hook:[0,2,1,""]},"rauth.service.OflyService":{request:[0,1,1,""],get_authorize_url:[0,1,1,""]},"rauth.service.OAuth2Service":{get_authorize_url:[0,1,1,""],request:[0,1,1,""],get_access_token:[0,1,1,""]}},terms:{represent:0,all:[],code:0,authorization_cod:0,excel:0,auth_head:0,random:[],allow_redirect:0,obtain:0,top:0,session:0,through:0,init_app:[],follow:0,assign:[],ofli:0,token_kei:0,paramet:0,redirect:0,whose:[],onli:0,get_authenticated_sess:0,acquir:0,also:0,flask:[],endpoint:0,activ:[],param:0,should:0,reinit:0,response_typ:0,dict:0,mai:0,foobar:0,input:[],hidden:[],main:[],might:0,app:[],split:0,applic:0,global:[],"return":0,oauth1hook:0,thei:[],get:0,python:0,rout:[],initi:[],defaul:0,util:0,instanc:[],webauth:[],oauth_request:0,csrf:[],request_token:0,now:0,you:0,like:0,document:0,name:0,specif:0,hook:0,"_construct_sess":0,html:[],authent:0,"default":0,achiev:[],necessari:0,contain:0,cooki:[],querystr:0,setup:0,deault:0,view:[],oflyservic:0,www:0,set:0,intial:0,client:0,natur:0,some:0,prehook:0,around:0,reponse_typ:0,consum:0,wrap:0,see:0,http_method:[],design:[],expect:0,authorize_url:0,pass:0,fail:[],unless:[],servic:0,append:0,variabl:0,oflycallsignatur:0,flaskext:[],influenc:[],miss:[],someth:0,token:0,superb:0,content:0,enough:0,run:[],oauth1:0,header_auth:0,"import":0,method:0,attribut:0,altern:0,signatur:0,object:0,hash:[],kei:0,rauth:0,gener:0,usag:0,given:0,here:0,standard:0,consumer_secret:0,extens:0,step:0,base:[],whereupon:0,dictionari:0,pair:0,ask:0,post:0,valu:[],render_templ:[],both:[],seasurf:[],your:[],wai:0,most:0,access_token_secret:0,could:0,current:0,http:0,oauth2servic:0,get_access_token:0,turn:0,redirect_uri:0,oauth_token:0,logic:[],fals:0,implement:0,reli:0,com:0,produc:0,commonli:0,first:0,request_token_secret:0,pleas:0,rang:[],via:0,simpli:0,although:0,directli:0,point:0,onc:0,modul:0,easy_instal:0,pre_request:0,header:0,api:0,oauth_kei:0,send:0,exclud:[],ensur:0,instal:0,hmacsha1signatur:0,assum:0,primari:[],middlewar:[],backend:[],quit:0,accordingli:0,from:0,would:0,attach:0,addition:0,interact:0,support:0,visit:0,two:0,been:0,avail:0,start:0,secret:0,call:0,includ:[],easiest:0,handl:0,some_view:[],addit:0,type:0,"final":0,more:0,"function":0,head:[],wrapper:0,option:0,form:[],conveni:0,exempt:[],jinja:[],oauth1servic:0,oflycallbackurl:0,made:0,bodi:[],indic:[],oauth:0,request_token_url:0,sign:0,"true":0,than:0,access_token_url:0,rsa1:0,remote_us:0,none:0,subsequ:0,retriev:0,look:[],keyword:0,possibl:0,provid:0,wish:[],secret_kei:[],access:0,spec:0,can:0,abov:0,oauth_param:0,inform:0,other:[],similar:0,def:[],pre:0,oauth2:[],want:0,generate_oauth_param:[],process:0,well:0,csrf_token:[],respons:0,shutterfli:0,argument:0,templat:[],get_authorize_url:0,direct:0,packag:0,kwarg:0,have:0,deliv:0,pip:0,properli:0,doe:0,check:[],"__init__":0,string:0,access_token:0,readi:0,realm:0,variou:0,authen:0,author:0,receiv:0,oauthhook:0,make:0,format:0,when:[],same:0,consumer_kei:0,note:0,field:[],need:0,valid:0,"_csrf_token":[],futur:0,which:0,relav:0,before_request:[],instead:0,presum:0,generate_authorization_head:[],simpl:0,updat:0,oflyremoteus:0,simplifi:0,begin:0,sure:0,resourc:0,allow:0,trace:[],normal:0,our:0,proper:0,after:0,upon:0,decor:[],sfly:0,rsa:[],capabl:0,render:[],specifi:[],establish:0,streamlin:0,csrftoken:[],oauth_sess:0,construct:0,data:0,"class":0,built:0,get_request_token:0,simplic:0,sha1:0,off:[],save:[],url:0,bind:0,grant:0,request:0,uri:0,django:[],rather:0,exampl:0,command:0,thi:0,time:[],grant_typ:0,salt:[],get_authorization_url:0,usual:0},objtypes:{"0":"py:module","1":"py:method","2":"py:class","3":"py:attribute"},titles:["Rauth"],objnames:{"0":["py","module","Python module"],"1":["py","method","Python method"],"2":["py","class","Python class"],"3":["py","attribute","Python attribute"]},filenames:["index"]})
View
13 examples/github.py
@@ -15,14 +15,15 @@
# URL that's redirected to AFTER you login and authorize the demo application
code = raw_input('Enter code parameter (code=something) from URL: ')
+# create a dictionary for the data we'll post on the get_access_token request
+data = dict(code=code, redirect_uri='https://github.com/litl/rauth/')
+
# retrieve the access token
-access_token = github.get_access_token(
- code=code,
- redirect_uri='https://github.com/litl/rauth/').content['access_token']
+access_token = \
+ github.get_access_token('POST', data=data).content['access_token']
# make a request using the access token
-user = github.request('GET',
- 'https://github.com/api/v2/json/user/show',
- access_token=access_token).content['user']
+user = github.get('https://github.com/api/v2/json/user/show',
+ params=dict(access_token=access_token)).content['user']
print 'currently logged in as: ' + user['login']
View
23 examples/twitter-timeline.py
@@ -11,18 +11,17 @@
header_auth=True)
request_token, request_token_secret = \
- twitter.get_request_token(http_method='GET')
-
+ twitter.get_request_token(method='GET')
authorize_url = twitter.get_authorize_url(request_token)
print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')
-response = twitter.get_access_token(request_token,
- request_token_secret,
- http_method='GET',
- oauth_verifier=pin)
+response = twitter.get_access_token('GET',
+ request_token=request_token,
+ request_token_secret=request_token_secret,
+ params={'oauth_verifier': pin})
data = response.content
access_token = data['oauth_token']
@@ -31,13 +30,11 @@
params = {'include_rts': 1, # Include retweets
'count': 10} # 10 tweets
-response = twitter.request(
- 'GET',
- 'https://api.twitter.com/1/statuses/home_timeline.json',
- access_token,
- access_token_secret,
- header_auth=True,
- params=params)
+response = twitter.get('https://api.twitter.com/1/statuses/home_timeline.json',
+ params=params,
+ access_token=access_token,
+ access_token_secret=access_token_secret,
+ header_auth=True)
for i, tweet in enumerate(response.content, 1):
handle = tweet['user']['screen_name'].encode('utf-8')
View
4 rauth/__init__.py
@@ -1,9 +1,9 @@
-''''
+'''
rauth
-----
OAuth 1.0/a and 2.0 wrapped around Python Requests.
'''
-__version__ = '0.2.4'
+__version__ = '0.3.0'
View
195 rauth/service.py
@@ -16,6 +16,41 @@
from datetime import datetime
+class Request(object):
+ '''A container for common HTTP request methods.'''
+ def get(self, url, **kwargs):
+ '''Sends a GET request. Returns :class:`Response` object.
+
+ :param url: The resource to be requested.
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
+ '''
+ return self.request('GET', url, **kwargs)
+
+ def post(self, url, **kwargs):
+ '''Sends a POST request. Returns :class:`Response` object.
+
+ :param url: The resource to be requested.
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
+ '''
+ return self.request('POST', url, **kwargs)
+
+ def put(self, url, **kwargs):
+ '''Sends a PUT request. Returns :class:`Response` object.
+
+ :param url: The resource to be requested.
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
+ '''
+ return self.request('PUT', url, **kwargs)
+
+ def delete(self, url, **kwargs):
+ '''Sends a DELETE request. Returns :class:`Response` object.
+
+ :param url: The resource to be requested.
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
+ '''
+ return self.request('DELETE', url, **kwargs)
+
+
class Response(object):
'''A service response container.
@@ -41,7 +76,7 @@ def content(self):
return content
-class OflyService(object):
+class OflyService(Request):
'''An Ofly Service container.
This class wraps an Ofly service. Most commonly, Shutterfly. The process
@@ -126,7 +161,7 @@ def get_authorize_url(self, remote_user=None, redirect_uri=None, **params):
:param remote_user: This is the oflyRemoteUser param. Defaults to None.
:param redirect_uri: This is the oflyCallbackUrl. Defaults to None.
- :param params: Additional keyworded arguments to be added to the
+ :param \*\*params: Additional keyworded arguments to be added to the
request querystring.
'''
if remote_user is not None:
@@ -138,41 +173,43 @@ def get_authorize_url(self, remote_user=None, redirect_uri=None, **params):
params = '?' + self._sha1_sign_params(self.authorize_url, **params)
return self.authorize_url + params
- def request(self, http_method, url, header_auth=False, params=None,
- data=None):
+ def request(self, method, url, **kwargs):
'''Sends a request to an Ofly endpoint, properly wrapped around
requests.
- :param http_method: A string representation of the HTTP method to be
+ :param method: A string representation of the HTTP method to be
used.
:param url: The resource to be requested.
:param header_auth: Authenication via header, defaults to False.
- :param params: Additional arguments to be added to the request
- querystring.
- :param data: Additional data to be included in the request body.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
+ params = kwargs.get('params')
+ data = kwargs.get('data')
+
if params is None:
params = {}
+ header_auth = kwargs.get('header_auth', False)
if header_auth:
- params, headers = self._sha1_sign_params(url,
- header_auth=True,
- **params)
+ params, headers = \
+ self._sha1_sign_params(url,
+ header_auth,
+ **params)
- response = requests.request(http_method,
+ response = requests.request(method,
url + '?' + params,
headers=headers)
else:
params = self._sha1_sign_params(url, **params)
- response = requests.request(http_method,
+ response = requests.request(method,
url + '?' + params,
data=data)
return Response(response)
-class OAuth2Service(object):
+class OAuth2Service(Request):
'''An OAuth 2.0 Service container.
This class is similar in nature to the OAuth1Service container but does
@@ -198,9 +235,10 @@ class OAuth2Service(object):
# once the above URL is consumed by a client we can ask for an access
# token. note that the code is retrieved from the redirect URL above,
# as set by the provider
- token = service.get_access_token(code='foobar',
- grant_type='authorization_code',
- redirect_uri='http://example.com/')
+ data = dict(code='foobar',
+ grant_type='authorization_code',
+ redirect_uri='http://example.com/')
+ token = service.get_access_token('POST', data=data)
:param name: The service name.
:param consumer_key: Client consumer key.
@@ -227,64 +265,55 @@ def get_authorize_url(self, response_type='code', **params):
'''Returns a proper authorize URL.
:param reponse_type: The response type. Defaults to 'code'.
- :param params: Additional keyworded arguments to be added to the
+ :param \*\*params: Additional keyworded arguments to be added to the
request querystring.
'''
params.update({'client_id': self.consumer_key,
'response_type': response_type})
params = '?' + urlencode(params)
return self.authorize_url + params
- def get_access_token(self, grant_type='authorization_code', **data):
+ def get_access_token(self, method='POST', **kwargs):
'''Retrieves the access token.
+ :param method: A string representation of the HTTP method to be used.
+ Defaults to 'POST'.
:param grant_type: The grant type. Deaults to 'authorization_code'.
- :param data: Keyworded arguments to be passed in the body of the
- request.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
- data.update({'grant_type': grant_type})
+ # populate either data or params with our credentials
+ key = None
+ if 'data' in kwargs:
+ key = 'data'
+ elif 'params' in kwargs:
+ key = 'params'
+ else:
+ # raise an error because credentials must be sent in this method
+ raise ValueError('Either params or data dict missing.')
- data.update(dict(client_id=self.consumer_key,
- client_secret=self.consumer_secret))
+ if key is not None:
+ grant_type = kwargs[key].get('grant_type', 'authorization_code')
+ kwargs[key].update(client_id=self.consumer_key,
+ client_secret=self.consumer_secret,
+ grant_type=grant_type)
- response = requests.post(self.access_token_url,
- data=data)
+ response = requests.request(method, self.access_token_url, **kwargs)
return Response(response)
- def request(self, http_method, url, access_token=None, params=None,
- data=None):
+ def request(self, method, url, **kwargs):
'''Sends a request to an OAuth 2.0 endpoint, properly wrapped around
requests.
- The first time an access token is provided it will be saved on the
- object for convenience.
-
- :param http_method: A string representation of the HTTP method to be
- used.
+ :param method: A string representation of the HTTP method to be used.
:param url: The resource to be requested.
- :param access_token: The access token as returned by
- :class:`get_access_token`.
- :param params: Additional arguments to be added to the request
- querystring.
- :param data: Additional data to be included in the request body.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
- if access_token is None and self.access_token is None:
- raise ValueError('Access token must be set!')
- elif access_token is not None:
- self.access_token = access_token
-
- if params is None:
- params = {}
-
- params.update({'access_token': self.access_token})
-
- response = requests.request(http_method, url, params=params, data=data)
-
+ response = requests.request(method, url, **kwargs)
return Response(response)
-class OAuth1Service(object):
+class OAuth1Service(Request):
'''An OAuth 1.0/a Service container.
This class provides a container for an OAuth Service provider. It utilizes
@@ -311,12 +340,13 @@ class OAuth1Service(object):
authorize_url = service.get_authorize_url(request_token)
- Once the client has authorized the request it is not possible to retrieve
+ Once the client has authorized the request it is now possible to retrieve
an access token. Do so as follows::
- response = service.get_access_token(request_token,
- request_token_secret,
- http_method='GET')
+ response = \
+ service.get_access_token(method='GET'
+ request_token=request_token,
+ request_token_secret=request_token_secret)
# access tokens are returned in the response dictionary
response['oauth_token']
@@ -353,28 +383,26 @@ def _construct_session(self, **kwargs):
'''Construct the request session, supplying the consumer key and
secret.
- :param kwargs: Extra keyworded arguments to be passed to the
+ :param \*\*kwargs: Extra keyworded arguments to be passed to the
OAuth1Hook constructor.
'''
hook = OAuth1Hook(consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret,
**kwargs)
return requests.session(hooks={'pre_request': hook})
- def get_request_token(self, http_method, **data):
+ def get_request_token(self, method='GET', **kwargs):
'''Gets a request token from the request token endpoint.
- :param http_method: A string representation of the HTTP method to be
- used.
- :param data: Keyworded arguments to be passed in the body of the
- request.
+ :param method: A string representation of the HTTP method to be used.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
auth_session = \
self._construct_session(header_auth=self.header_auth)
- response = auth_session.request(http_method,
+ response = auth_session.request(method,
self.request_token_url,
- data=data)
+ **kwargs)
response.raise_for_status()
@@ -386,34 +414,36 @@ def get_authorize_url(self, request_token, **params):
:param request_token: The request token as returned by
:class:`get_request_token`.
- :param params: Additional keyworded arguments to be added to the
+ :param \*\*params: Additional keyworded arguments to be added to the
request querystring.
'''
params.update({'oauth_token': quote(request_token)})
params = '?' + urlencode(params)
return self.authorize_url + params
- def get_access_token(self, request_token, request_token_secret,
- http_method, **params):
+ def get_access_token(self, method='GET', **kwargs):
'''Retrieves the access token.
+ :param method: A string representation of the HTTP method to be
+ used.
:param request_token: The request token as returned by
:class:`get_request_token`.
:param request_token_secret: The request token secret as returned by
:class:`get_request_token`.
- :param http_method: A string representation of the HTTP method to be
- used.
- :param params: Additional keyworded arguments to be added to the
- request querystring.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
+
+ request_token = kwargs.pop('request_token')
+ request_token_secret = kwargs.pop('request_token_secret')
+
auth_session = self._construct_session(
access_token=request_token,
access_token_secret=request_token_secret,
header_auth=self.header_auth)
- response = auth_session.request(http_method,
+ response = auth_session.request(method,
self.access_token_url,
- params=params)
+ **kwargs)
return Response(response)
@@ -431,31 +461,32 @@ def get_authenticated_session(self, access_token, access_token_secret,
access_token_secret=access_token_secret,
header_auth=header_auth)
- def request(self, http_method, url, access_token, access_token_secret,
- header_auth=False, params=None, data=None):
+ def request(self, method, url, **kwargs):
'''Makes a request using :class:`_construct_session`.
- :param http_method: A string representation of the HTTP method to be
+ :param method: A string representation of the HTTP method to be
used.
:param url: The resource to be requested.
:param access_token: The access token as returned by
:class:`get_access_token`.
:param access_token_secret: The access token secret as returned by
:class:`get_access_token`.
:param header_auth: Authenication via header, defaults to False.
- :param params: Additional arguments to be added to the request
- querystring.
- :param data: Additional data to be included in the request body.
+ :param allow_redirects: Allows a request to redirect, defaults to True.
+ :param \*\*kwargs: Optional arguments. Same as Requests.
'''
+ access_token = kwargs.pop('access_token')
+ access_token_secret = kwargs.pop('access_token_secret')
+ header_auth = kwargs.pop('header_auth', False)
+ allow_redirects = kwargs.pop('allow_redirects', True)
auth_session = \
self._construct_session(access_token=access_token,
access_token_secret=access_token_secret,
header_auth=header_auth)
- response = auth_session.request(http_method,
+ response = auth_session.request(method,
url,
- params=params,
- data=data,
- allow_redirects=True)
+ allow_redirects=allow_redirects,
+ **kwargs)
return Response(response)
View
197 tests/test_service.py
@@ -31,12 +31,12 @@ def test_get_authorize_url(self):
url = self.service.get_authorize_url(
remote_user='foobar',
redirect_uri='http://example.com/redirect')
- self.assertTrue('ApiSig=' in url)
- self.assertTrue('oflyAppId=123' in url)
- self.assertTrue('oflyCallbackUrl=http://example.com/redirect' in url)
- self.assertTrue('oflyHashMeth=SHA1' in url)
- self.assertTrue('oflyRemoteUser=foobar' in url)
- self.assertTrue('oflyTimestamp=' in url)
+ self.assertIn('ApiSig=', url)
+ self.assertIn('oflyAppId=123', url)
+ self.assertIn('oflyCallbackUrl=http://example.com/redirect', url)
+ self.assertIn('oflyHashMeth=SHA1', url)
+ self.assertIn('oflyRemoteUser=foobar', url)
+ self.assertIn('oflyTimestamp=', url)
@patch.object(requests.Session, 'request')
def test_request(self, mock_request):
@@ -48,25 +48,55 @@ def test_request(self, mock_request):
self.assertEqual(response['status'], 'ok')
@patch.object(requests.Session, 'request')
+ def test_get(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.get('http://example.com/endpoint').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_post(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.post('http://example.com/endpoint').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_put(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.put('http://example.com/endpoint').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_delete(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.delete('http://example.com/endpoint').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
def test_request_header_auth(self, mock_request):
self.response.content = json.dumps({'status': 'ok'})
self.response.headers['content-type'] = 'json'
mock_request.return_value = self.response
response = self.service.request('GET',
'http://example.com/endpoint',
header_auth=True).content
- print response
self.assertEqual(response['status'], 'ok')
@patch.object(requests.Session, 'request')
def test_request_bad_response(self, mock_request):
self.response.ok = False
self.response.raise_for_status = self.raise_for_status
mock_request.return_value = self.response
- try:
- self.service.request('GET',
- 'http://example.com/endpoint')
- except Exception, e:
+
+ with self.assertRaises(Exception) as e:
+ self.service.get('http://example.com/endpoint')
self.assertEqual('Response not OK!', str(e))
def test_micro_to_milliseconds(self):
@@ -113,25 +143,34 @@ def test_get_authorize_url_response_type(self):
@patch.object(requests.Session, 'request')
def test_get_access_token(self, mock_request):
mock_request.return_value = self.response
- response = self.service.get_access_token(code='4242').content
+ response = \
+ self.service.get_access_token(data=dict(code='4242')).content
+ self.assertEqual(response['access_token'], '321')
+
+ @patch.object(requests.Session, 'request')
+ def test_get_access_token_params(self, mock_request):
+ mock_request.return_value = self.response
+ response = \
+ self.service.get_access_token('GET',
+ params=dict(code='4242')).content
self.assertEqual(response['access_token'], '321')
@patch.object(requests.Session, 'request')
def test_get_access_token_bad_response(self, mock_request):
self.response.ok = False
self.response.raise_for_status = self.raise_for_status
mock_request.return_value = self.response
- try:
+
+ with self.assertRaises(Exception) as e:
self.service.get_access_token(code='4242')
- except Exception, e:
self.assertEqual('Response not OK!', str(e))
@patch.object(requests.Session, 'request')
def test_get_access_token_grant_type(self, mock_request):
mock_request.return_value = self.response
+ data = dict(code='4242', grant_type='refresh_token')
response = \
- self.service.get_access_token(code='4242',
- grant_type='refresh_token').content
+ self.service.get_access_token(data=data).content
self.assertEqual(response['access_token'], '321')
@patch.object(requests.Session, 'request')
@@ -144,32 +183,54 @@ def test_request(self, mock_request):
access_token='321').content
self.assertEqual(response['status'], 'ok')
- # test here again to make sure the access token was set
- response = self.service.request('GET',
- 'http://example.com/endpoint').content
+ @patch.object(requests.Session, 'request')
+ def test_get(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.get('http://example.com/endpoint',
+ access_token='321').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_post(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.post('http://example.com/endpoint',
+ access_token='321').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_put(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.put('http://example.com/endpoint',
+ access_token='321').content
+ self.assertEqual(response['status'], 'ok')
+
+ @patch.object(requests.Session, 'request')
+ def test_delete(self, mock_request):
+ self.response.content = json.dumps({'status': 'ok'})
+ self.response.headers['content-type'] = 'json'
+ mock_request.return_value = self.response
+ response = self.service.delete('http://example.com/endpoint',
+ access_token='321').content
self.assertEqual(response['status'], 'ok')
@patch.object(requests.Session, 'request')
def test_request_bad_response(self, mock_request):
self.response.ok = False
self.response.raise_for_status = self.raise_for_status
mock_request.return_value = self.response
- try:
+
+ with self.assertRaises(Exception) as e:
self.service.request('GET',
'http://example.com/endpoint',
access_token='321')
- except Exception, e:
self.assertEqual('Response not OK!', str(e))
- @patch.object(requests.Session, 'request')
- def test_request_missing_acccess_token(self, mock_request):
- self.response.content = json.dumps({'status': 'ok'})
- mock_request.return_value = self.response
- try:
- self.service.request('GET', 'http://example.com/endpoint')
- except Exception, e:
- self.assertEqual('Access token must be set!', str(e))
-
class OAuth1ServiceTestCase(RauthTestCase):
def setUp(self):
@@ -222,9 +283,8 @@ def test_get_request_token_bad_response(self, mock_request):
self.response.raise_for_status = self.raise_for_status
mock_request.return_value = self.response
- try:
+ with self.assertRaises(Exception) as e:
self.service.get_request_token('GET')
- except Exception, e:
self.assertEqual('Response not OK!', str(e))
self.assertRaises(Exception, self.service.get_request_token, ('GET'))
@@ -248,7 +308,7 @@ def test_get_access_token(self, mock_request):
access_resp = self.service.get_access_token(request_token='123',
request_token_secret='456',
- http_method='GET').content
+ method='GET').content
self.assertEqual(access_resp['oauth_token'], '123')
self.assertEqual(access_resp['oauth_token_secret'], '456')
@@ -258,9 +318,8 @@ def test_get_access_token_bad_response(self, mock_request):
self.response.raise_for_status = self.raise_for_status
mock_request.return_value = self.response
- try:
+ with self.assertRaises(Exception) as e:
self.service.get_access_token('123', '456', 'GET')
- except Exception, e:
self.assertEqual('Response not OK!', str(e))
self.assertRaises(Exception,
@@ -271,7 +330,7 @@ def test_get_authenticated_session(self):
auth_session = \
self.service.get_authenticated_session(access_token='123',
access_token_secret='456')
- self.assertTrue(auth_session is not None)
+ self.assertIsNotNone(auth_session)
@patch.object(requests.Session, 'request')
def test_use_authenticated_session(self, mock_request):
@@ -282,19 +341,67 @@ def test_use_authenticated_session(self, mock_request):
access_token_secret='456')
response = auth_session.get('http://example.com/foobar').content
- self.assertTrue(response is not None)
+ self.assertIsNotNone(response)
self.assertEqual('oauth_token=123&oauth_token_secret=456', response)
@patch.object(requests.Session, 'request')
- def test_request_get(self, mock_request):
+ def test_request(self, mock_request):
mock_request.return_value = self.response
response = \
self.service.request('GET',
'http://example.com/some/method',
access_token='123',
access_token_secret='456').content
- self.assertTrue(response is not None)
+ self.assertIsNotNone(response)
+ self.assertEqual('123', response['oauth_token'])
+ self.assertEqual('456', response['oauth_token_secret'])
+
+ @patch.object(requests.Session, 'request')
+ def test_get(self, mock_request):
+ mock_request.return_value = self.response
+
+ response = \
+ self.service.get('http://example.com/some/method',
+ access_token='123',
+ access_token_secret='456').content
+ self.assertIsNotNone(response)
+ self.assertEqual('123', response['oauth_token'])
+ self.assertEqual('456', response['oauth_token_secret'])
+
+ @patch.object(requests.Session, 'request')
+ def test_post(self, mock_request):
+ mock_request.return_value = self.response
+
+ response = \
+ self.service.post('http://example.com/some/method',
+ access_token='123',
+ access_token_secret='456').content
+ self.assertIsNotNone(response)
+ self.assertEqual('123', response['oauth_token'])
+ self.assertEqual('456', response['oauth_token_secret'])
+
+ @patch.object(requests.Session, 'request')
+ def test_put(self, mock_request):
+ mock_request.return_value = self.response
+
+ response = \
+ self.service.put('http://example.com/some/method',
+ access_token='123',
+ access_token_secret='456').content
+ self.assertIsNotNone(response)
+ self.assertEqual('123', response['oauth_token'])
+ self.assertEqual('456', response['oauth_token_secret'])
+
+ @patch.object(requests.Session, 'request')
+ def test_delete(self, mock_request):
+ mock_request.return_value = self.response
+
+ response = \
+ self.service.delete('http://example.com/some/method',
+ access_token='123',
+ access_token_secret='456').content
+ self.assertIsNotNone(response)
self.assertEqual('123', response['oauth_token'])
self.assertEqual('456', response['oauth_token_secret'])
@@ -304,18 +411,24 @@ def test_json_response(self, mock_request):
mock_request.return_value = self.response
self.response.content = json.dumps({'a': 'b'})
- access_resp = self.service.get_access_token('123', '456', 'GET')
+ access_resp = self.service.get_access_token(method='GET',
+ request_token='123',
+ request_token_secret='456')
self.assertEqual({'a': 'b'}, access_resp.content)
# test the case of a non-list, non-dict
self.response.content = json.dumps(42)
- access_resp = self.service.get_access_token('123', '456', 'GET')
+ access_resp = self.service.get_access_token(method='GET',
+ request_token='123',
+ request_token_secret='456')
self.assertEqual(42, access_resp.content)
@patch.object(requests.Session, 'request')
def test_other_response(self, mock_request):
mock_request.return_value = self.response
self.response.content = {'a': 'b'}
- access_resp = self.service.get_access_token('123', '456', 'GET')
+ access_resp = self.service.get_access_token(method='GET',
+ request_token='123',
+ request_token_secret='456')
self.assertEqual({'a': 'b'}, access_resp.content)

0 comments on commit 7d1368a

Please sign in to comment.
Something went wrong with that request. Please try again.