Skip to content

Commit da092ea

Browse files
authored
Updated sphinx syntax (dropbox#231)
* Formatted OAuth2FlowNoRedirectResult * Changed singel args to : param * Deleted duplicate parameters * Updated OAuth2FlowResult * Add link to oauth examples * Formatted line length and added sphinx tags * Added missing classes * Fixed linter errors
1 parent c41c2c8 commit da092ea

File tree

1 file changed

+96
-122
lines changed

1 file changed

+96
-122
lines changed

dropbox/oauth.py

+96-122
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,15 @@ class OAuth2FlowNoRedirectResult(object):
4444

4545
def __init__(self, access_token, account_id, user_id, refresh_token, expiration, scope):
4646
"""
47-
Args:
48-
access_token (str): Token to be used to authenticate later
49-
requests.
50-
refresh_token (str): Token to be used to acquire new access token
51-
when existing one expires
52-
expiration (int, datetime): Either the number of seconds from now that the token expires
53-
in or the datetime at which the token expires
54-
account_id (str): The Dropbox user's account ID.
55-
user_id (str): Deprecated (use account_id instead).
56-
refresh_token (str): Token to be used to acquire new access token
57-
when existing one expires
58-
expiration (int, datetime): Either the number of seconds from now that the token expires
59-
in or the datetime at which the token expires
60-
scope (list): list of scopes to request in base oauth flow.
47+
:param str access_token: Token to be used to authenticate later requests.
48+
:param str account_id: The Dropbox user's account ID.
49+
:param str user_id: Deprecated (use :attr:`account_id` instead).
50+
:param str refresh_token: Token to be used to acquire new access token when existing one
51+
expires.
52+
:param expiration: Either the number of seconds from now that the token expires in or the
53+
datetime at which the token expires.
54+
:type expiration: int or datetime
55+
:param list scope: List of scopes to request in base oauth flow.
6156
"""
6257
self.access_token = access_token
6358
if not expiration:
@@ -84,17 +79,15 @@ def __repr__(self):
8479

8580
class OAuth2FlowResult(OAuth2FlowNoRedirectResult):
8681
"""
87-
Authorization information for an OAuth2Flow with redirect.
82+
Authorization information for an :class:`OAuth2Flow` with redirect.
8883
"""
8984

9085
def __init__(self, access_token, account_id, user_id, url_state, refresh_token,
9186
expires_in, scope):
9287
"""
93-
Same as OAuth2FlowNoRedirectResult but with url_state.
88+
Same as :class:`OAuth2FlowNoRedirectResult` but with url_state.
9489
95-
Args:
96-
url_state (str): The url state that was set by
97-
:meth:`DropboxOAuth2Flow.start`.
90+
:param str url_state: The url state that was set by :meth:`DropboxOAuth2Flow.start`.
9891
"""
9992
super(OAuth2FlowResult, self).__init__(
10093
access_token=access_token,
@@ -234,9 +227,8 @@ def _finish(self, code, redirect_uri, code_verifier):
234227
def build_path(self, target, params=None):
235228
"""Build the path component for an API URL.
236229
237-
This method urlencodes the parameters, adds them
238-
to the end of the target url, and puts a marker for the API
239-
version in front.
230+
This method urlencodes the parameters, adds them to the end of the target url, and puts a
231+
marker for the API version in front.
240232
241233
:param str target: A target url (e.g. '/files') to build upon.
242234
:param dict params: Optional dictionary of parameters (name to value).
@@ -263,8 +255,7 @@ def build_path(self, target, params=None):
263255
def build_url(self, target, params=None, host=API_HOST):
264256
"""Build an API URL.
265257
266-
This method adds scheme and hostname to the path
267-
returned from build_path.
258+
This method adds scheme and hostname to the path returned from build_path.
268259
269260
:param str target: A target url (e.g. '/files') to build upon.
270261
:param dict params: Optional dictionary of parameters (name to value).
@@ -279,7 +270,8 @@ class DropboxOAuth2FlowNoRedirect(DropboxOAuth2FlowBase):
279270
OAuth 2 authorization helper for apps that can't provide a redirect URI
280271
(such as the command-line example apps).
281272
282-
See examples under example/oauth
273+
See examples under `example/oauth <https://github.com/dropbox/dropbox-sdk-python/tree/master/
274+
example/oauth>`_
283275
284276
"""
285277

@@ -290,33 +282,31 @@ def __init__(self, consumer_key, consumer_secret=None, locale=None, token_access
290282
291283
:param str consumer_key: Your API app's "app key".
292284
:param str consumer_secret: Your API app's "app secret".
293-
:param str locale: The locale of the user of your application. For
294-
example "en" or "en_US". Some API calls return localized data and
295-
error messages; this setting tells the server which locale to use.
296-
By default, the server uses "en_US".
285+
:param str locale: The locale of the user of your application. For example "en" or "en_US".
286+
Some API calls return localized data and error messages; this setting tells the server
287+
which locale to use. By default, the server uses "en_US".
297288
:param str token_access_type: the type of token to be requested.
298289
From the following enum:
299290
300291
* legacy - creates one long-lived token with no expiration
301292
* online - create one short-lived token with an expiration
302293
* offline - create one short-lived token with an expiration with a refresh token
303294
304-
:param list scope: list of scopes to request in base oauth flow. If left blank,
305-
will default to all scopes for app.
306-
:param str include_granted_scopes: which scopes to include from previous grants
295+
:param list scope: list of scopes to request in base oauth flow.
296+
If left blank, will default to all scopes for app.
297+
:param str include_granted_scopes: which scopes to include from previous grants.
307298
From the following enum:
308299
309300
* user - include user scopes in the grant
310301
* team - include team scopes in the grant
311-
* Note: if this user has never linked the app, include_granted_scopes must be None
302+
* *Note*: if this user has never linked the app, include_granted_scopes must be None
312303
313304
:param bool use_pkce: Whether or not to use Sha256 based PKCE. PKCE should be only use on
314305
client apps which doesn't call your server. It is less secure than non-PKCE flow but
315306
can be used if you are unable to safely retrieve your app secret.
316307
:param Optional[float] timeout: Maximum duration in seconds that
317-
client will wait for any single packet from the
318-
server. After the timeout the client will give up on
319-
connection. If `None`, client will wait forever. Defaults
308+
client will wait for any single packet from the server. After the timeout the client
309+
will give up on connection. If `None`, client will wait forever. Defaults
320310
to 100 seconds.
321311
"""
322312
super(DropboxOAuth2FlowNoRedirect, self).__init__(
@@ -334,10 +324,9 @@ def start(self):
334324
"""
335325
Starts the OAuth 2 authorization process.
336326
337-
:return: The URL for a page on Dropbox's website. This page will let
338-
the user "approve" your app, which gives your app permission to
339-
access the user's Dropbox account. Tell the user to visit this URL
340-
and approve your app.
327+
:return: The URL for a page on Dropbox's website. This page will let the user "approve"
328+
your app, which gives your app permission to access the user's Dropbox account.
329+
Tell the user to visit this URL and approve your app.
341330
"""
342331
return self._get_authorize_url(None, None, self.token_access_type,
343332
scope=self.scope,
@@ -346,27 +335,27 @@ def start(self):
346335

347336
def finish(self, code):
348337
"""
349-
If the user approves your app, they will be presented with an
350-
"authorization code". Have the user copy/paste that authorization code
351-
into your app and then call this method to get an access token.
338+
If the user approves your app, they will be presented with an "authorization code".
339+
Have the user copy/paste that authorization code into your app and then call this method to
340+
get an access token.
352341
353342
:param str code: The authorization code shown to the user when they
354343
approved your app.
355-
:rtype: OAuth2FlowNoRedirectResult
344+
:rtype: :class:`OAuth2FlowNoRedirectResult`
356345
:raises: The same exceptions as :meth:`DropboxOAuth2Flow.finish()`.
357346
"""
358347
return self._finish(code, None, self.code_verifier)
359348

360349

361350
class DropboxOAuth2Flow(DropboxOAuth2FlowBase):
362351
"""
363-
OAuth 2 authorization helper. Use this for web apps.
352+
OAuth 2 authorization helper. Use this for web apps.
364353
365-
OAuth 2 has a two-step authorization process. The first step is having the
366-
user authorize your app. The second involves getting an OAuth 2 access
367-
token from Dropbox.
354+
OAuth 2 has a two-step authorization process. The first step is having the user authorize your
355+
app. The second involves getting an OAuth 2 access token from Dropbox.
368356
369-
See examples under example/oauth
357+
See examples under `example/oauth <https://github.com/dropbox/dropbox-sdk-python/tree/master/
358+
example/oauth>`_
370359
371360
"""
372361

@@ -379,41 +368,38 @@ def __init__(self, consumer_key, redirect_uri, session,
379368
380369
:param str consumer_key: Your API app's "app key".
381370
:param str consumer_secret: Your API app's "app secret".
382-
:param str redirect_uri: The URI that the Dropbox server will redirect
383-
the user to after the user finishes authorizing your app. This URI
384-
must be HTTPS-based and pre-registered with the Dropbox servers,
385-
though localhost URIs are allowed without pre-registration and can
386-
be either HTTP or HTTPS.
387-
:param dict session: A dict-like object that represents the current
388-
user's web session (will be used to save the CSRF token).
389-
:param str csrf_token_session_key: The key to use when storing the CSRF
390-
token in the session (for example: "dropbox-auth-csrf-token").
391-
:param str locale: The locale of the user of your application. For
392-
example "en" or "en_US". Some API calls return localized data and
393-
error messages; this setting tells the server which locale to use.
394-
By default, the server uses "en_US".
395-
:param str token_access_type: the type of token to be requested.
371+
:param str redirect_uri: The URI that the Dropbox server will redirect the user to after the
372+
user finishes authorizing your app. This URI must be HTTPS-based and pre-registered
373+
with the Dropbox servers, though localhost URIs are allowed without pre-registration and
374+
can be either HTTP or HTTPS.
375+
:param dict session: A dict-like object that represents the current user's web session
376+
(Will be used to save the CSRF token).
377+
:param str csrf_token_session_key: The key to use when storing the CSRF token in the session
378+
(For example: "dropbox-auth-csrf-token").
379+
:param str locale: The locale of the user of your application. For example "en" or "en_US".
380+
Some API calls return localized data and error messages; this setting tells the server
381+
which locale to use. By default, the server uses "en_US".
382+
:param str token_access_type: The type of token to be requested.
396383
From the following enum:
397384
398385
* legacy - creates one long-lived token with no expiration
399386
* online - create one short-lived token with an expiration
400387
* offline - create one short-lived token with an expiration with a refresh token
401388
402-
:param list scope: list of scopes to request in base oauth flow. If left blank,
389+
:param list scope: List of scopes to request in base oauth flow. If left blank,
403390
will default to all scopes for app.
404-
:param str include_granted_scopes: which scopes to include from previous grants
391+
:param str include_granted_scopes: Which scopes to include from previous grants.
405392
From the following enum:
406393
407394
* user - include user scopes in the grant
408395
* team - include team scopes in the grant
409-
* Note: if this user has never linked the app, include_granted_scopes must be None
396+
* *Note*: If this user has never linked the app, :attr:`include_granted_scopes` must \
397+
be `None`
410398
411399
:param bool use_pkce: Whether or not to use Sha256 based PKCE
412-
:param Optional[float] timeout: Maximum duration in seconds that
413-
client will wait for any single packet from the
414-
server. After the timeout the client will give up on
415-
connection. If `None`, client will wait forever. Defaults
416-
to 100 seconds.
400+
:param Optional[float] timeout: Maximum duration in seconds that client will wait for any
401+
single packet from the server. After the timeout the client will give up on connection.
402+
If `None`, client will wait forever. Defaults to 100 seconds.
417403
"""
418404

419405
super(DropboxOAuth2Flow, self).__init__(
@@ -434,24 +420,19 @@ def start(self, url_state=None):
434420
"""
435421
Starts the OAuth 2 authorization process.
436422
437-
This function builds an "authorization URL". You should redirect your
438-
user's browser to this URL, which will give them an opportunity to
439-
grant your app access to their Dropbox account. When the user
440-
completes this process, they will be automatically redirected to the
441-
``redirect_uri`` you passed in to the constructor.
442-
443-
This function will also save a CSRF token to
444-
``session[csrf_token_session_key]`` (as provided to the constructor).
445-
This CSRF token will be checked on :meth:`finish()` to prevent request
446-
forgery.
447-
448-
:param str url_state: Any data that you would like to keep in the URL
449-
through the authorization process. This exact value will be
450-
returned to you by :meth:`finish()`.
451-
:return: The URL for a page on Dropbox's website. This page will let
452-
the user "approve" your app, which gives your app permission to
453-
access the user's Dropbox account. Tell the user to visit this URL
454-
and approve your app.
423+
This function builds an "authorization URL". You should redirect your user's browser to this
424+
URL, which will give them an opportunity to grant your app access to their Dropbox
425+
account. When the user completes this process, they will be automatically redirected to
426+
the :attr:`redirect_uri` you passed in to the constructor. This function will also save
427+
a CSRF token to :attr:`session[csrf_token_session_key]`
428+
(as provided to the constructor). This CSRF token will be checked on :meth:`finish()`
429+
to prevent request forgery.
430+
431+
:param str url_state: Any data that you would like to keep in the URL through the
432+
authorization process. This exact value will be returned to you by :meth:`finish()`.
433+
:return: The URL for a page on Dropbox's website. This page will let the user "approve" your
434+
app, which gives your app permission to access the user's Dropbox account. Tell the user
435+
to visit this URL and approve your app.
455436
"""
456437
csrf_token = base64.urlsafe_b64encode(os.urandom(16)).decode('ascii')
457438
state = csrf_token
@@ -466,23 +447,19 @@ def start(self, url_state=None):
466447

467448
def finish(self, query_params):
468449
"""
469-
Call this after the user has visited the authorize URL (see
470-
:meth:`start()`), approved your app and was redirected to your redirect
471-
URI.
472-
473-
:param dict query_params: The query parameters on the GET request to
474-
your redirect URI.
475-
:rtype: OAuth2FlowResult
476-
:raises: :class:`BadRequestException` If the redirect URL was missing
477-
parameters or if the given parameters were not valid.
478-
:raises: :class:`BadStateException` If there's no CSRF token in the
479-
session.
480-
:raises: :class:`CsrfException` If the ``state`` query parameter
481-
doesn't contain the CSRF token from the user's session.
482-
:raises: :class:`NotApprovedException` If the user chose not to
483-
approve your app.
484-
:raises: :class:`ProviderException` If Dropbox redirected to your
485-
redirect URI with some unexpected error identifier and error message.
450+
Call this after the user has visited the authorize URL (see :meth:`start()`), approved your
451+
app and was redirected to your redirect URI.
452+
453+
:param dict query_params: The query parameters on the GET request to your redirect URI.
454+
:rtype: class:`OAuth2FlowResult`
455+
:raises: :class:`BadRequestException` If the redirect URL was missing parameters or if the
456+
given parameters were not valid.
457+
:raises: :class:`BadStateException` If there's no CSRF token in the session.
458+
:raises: :class:`CsrfException` If the :attr:`state` query parameter doesn't contain the
459+
CSRF token from the user's session.
460+
:raises: :class:`NotApprovedException` If the user chose not to approve your app.
461+
:raises: :class:`ProviderException` If Dropbox redirected to your redirect URI with some
462+
unexpected error identifier and error message.
486463
"""
487464
# Check well-formedness of request.
488465

@@ -553,8 +530,7 @@ def finish(self, query_params):
553530

554531
class BadRequestException(Exception):
555532
"""
556-
Thrown if the redirect URL was missing parameters or if the
557-
given parameters were not valid.
533+
Thrown if the redirect URL was missing parameters or if the given parameters were not valid.
558534
559535
The recommended action is to show an HTTP 400 error page.
560536
"""
@@ -563,19 +539,18 @@ class BadRequestException(Exception):
563539

564540
class BadStateException(Exception):
565541
"""
566-
Thrown if all the parameters are correct, but there's no CSRF token in the
567-
session. This probably means that the session expired.
542+
Thrown if all the parameters are correct, but there's no CSRF token in the session. This
543+
probably means that the session expired.
568544
569-
The recommended action is to redirect the user's browser to try the
570-
approval process again.
545+
The recommended action is to redirect the user's browser to try the approval process again.
571546
"""
572547
pass
573548

574549

575550
class CsrfException(Exception):
576551
"""
577-
Thrown if the given 'state' parameter doesn't contain the CSRF token from
578-
the user's session. This is blocked to prevent CSRF attacks.
552+
Thrown if the given 'state' parameter doesn't contain the CSRF token from the user's session.
553+
This is blocked to prevent CSRF attacks.
579554
580555
The recommended action is to respond with an HTTP 403 error page.
581556
"""
@@ -591,11 +566,10 @@ class NotApprovedException(Exception):
591566

592567
class ProviderException(Exception):
593568
"""
594-
Dropbox redirected to your redirect URI with some unexpected error
595-
identifier and error message.
569+
Dropbox redirected to your redirect URI with some unexpected error identifier and error message.
596570
597-
The recommended action is to log the error, tell the user something went
598-
wrong, and let them try again.
571+
The recommended action is to log the error, tell the user something went wrong, and let them try
572+
again.
599573
"""
600574
pass
601575

@@ -621,11 +595,11 @@ def _safe_equals(a, b):
621595

622596
def _params_to_urlencoded(params):
623597
"""
624-
Returns a application/x-www-form-urlencoded ``str`` representing the
625-
key/value pairs in ``params``.
598+
Returns a application/x-www-form-urlencoded :class:`str` representing the key/value pairs in
599+
:attr:`params`.
626600
627-
Keys are values are ``str()``'d before calling ``urllib.urlencode``, with
628-
the exception of unicode objects which are utf8-encoded.
601+
Keys are values are ``str()``'d before calling :meth:`urllib.urlencode`, with the exception of
602+
unicode objects which are utf8-encoded.
629603
"""
630604
def encode(o):
631605
if isinstance(o, six.binary_type):

0 commit comments

Comments
 (0)