-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from fghaas/requests
Add REST upload capability
- Loading branch information
Showing
11 changed files
with
645 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import unicode_literals | ||
|
||
import logging | ||
|
||
import requests | ||
|
||
|
||
class TokenHelperException(Exception): | ||
pass | ||
|
||
|
||
class TokenHelper(object): | ||
|
||
TOKEN_URL_FORMAT = '%s/oauth2/access_token' | ||
|
||
def __init__(self, url, client_id, client_secret): | ||
self.token_url = self.TOKEN_URL_FORMAT % url | ||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
|
||
def fetch_token(self): | ||
if not self.client_id: | ||
raise TokenHelperException('No client ID specified.') | ||
if not self.client_secret: | ||
raise TokenHelperException('No client secret specified.') | ||
request_data = { | ||
'grant_type': 'client_credentials', | ||
'token_type': 'jwt', | ||
'client_id': self.client_id, | ||
'client_secret': self.client_secret, | ||
} | ||
r = requests.post(self.token_url, | ||
data=request_data) | ||
# Raise an HTTPError if we didn't get an OK response | ||
r.raise_for_status() | ||
logging.debug("Request took %s to complete" % r.elapsed) | ||
|
||
json_result = r.json() | ||
logging.debug("Request returned JSON result %s" % json_result) | ||
|
||
self.token = json_result['access_token'] | ||
return self.token |
Oops, something went wrong.