2019-03-12 16:18:08 +00:00
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
from requests.exceptions import RequestException
|
|
|
|
|
|
|
|
from sentry import http
|
|
|
|
from sentry.utils import json
|
|
|
|
|
|
|
|
from .constants import BASE_DOMAIN
|
|
|
|
|
|
|
|
|
2019-03-13 13:27:34 +00:00
|
|
|
class PassbookApiError(Exception):
|
2019-03-12 16:18:08 +00:00
|
|
|
def __init__(self, message='', status=0):
|
2019-03-13 13:27:34 +00:00
|
|
|
super(PassbookApiError, self).__init__(message)
|
2019-03-12 16:18:08 +00:00
|
|
|
self.status = status
|
|
|
|
|
|
|
|
|
2019-03-13 13:27:34 +00:00
|
|
|
class PassbookClient(object):
|
2019-03-12 16:18:08 +00:00
|
|
|
def __init__(self, client_id, client_secret):
|
|
|
|
self.client_id = client_id
|
|
|
|
self.client_secret = client_secret
|
|
|
|
self.http = http.build_session()
|
|
|
|
|
|
|
|
def _request(self, path, access_token):
|
|
|
|
params = {
|
|
|
|
'client_id': self.client_id,
|
|
|
|
'client_secret': self.client_secret,
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Authorization': 'Bearer {0}'.format(access_token),
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
req = self.http.get('https://{0}/{1}'.format(BASE_DOMAIN, path.lstrip('/')),
|
|
|
|
params=params,
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
except RequestException as e:
|
2019-03-13 13:27:34 +00:00
|
|
|
raise PassbookApiError(unicode(e), status=getattr(e, 'status_code', 0))
|
2019-03-12 16:18:08 +00:00
|
|
|
if req.status_code < 200 or req.status_code >= 300:
|
2019-03-13 13:27:34 +00:00
|
|
|
raise PassbookApiError(req.content, status=req.status_code)
|
2019-03-12 16:18:08 +00:00
|
|
|
return json.loads(req.content)
|
|
|
|
|
|
|
|
def get_user(self, access_token):
|
2019-03-13 13:27:34 +00:00
|
|
|
return self._request('/api/v1/openid/', access_token)
|