Skip to content

Commit

Permalink
Merge pull request #19 from surfmappers/accessToken
Browse files Browse the repository at this point in the history
Access token
  • Loading branch information
YuriAlessandro authored Dec 18, 2018
2 parents a4b1b43 + 03a2a61 commit 81d45dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
### 1.0.0
* Add Order payment support
* Add Account feature
* Add validations to request's response
* Request errors handler
* Validations to requests response
* Feature requests with access token when necessary
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='wirecardpy',
version='0.4.1',
version='0.4.2',
description='Python Library to Wirecard (old MOIP in Brazil) API routes.',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
4 changes: 2 additions & 2 deletions wirecardpy/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def exists(data):
return util.request_get(url, data)

@staticmethod
def create(data):
def create(access_token, data):
url = util.get_base_url() + constants.ACCOUNT_URL
return util.request_post(url, data)
return util.request_post(url, data, access_token=access_token)

def get(account_id):
url = util.get_base_url() + constants.ACCOUNT_GET_URL.format(account_id)
Expand Down
20 changes: 15 additions & 5 deletions wirecardpy/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ def headers():
return _headers


def oauth2_headers(access_token):
_headers = {
'Authorization': 'Bearer ' + access_token,
}
return _headers


def validate_response(response):
if response.status_code == 200:
if response.status_code == 200 or response.status_code == 201:
return response.json()
else:
status_code = response.status_code
response_json = response.json()
response_json['status_code'] = status_code
error_message = 'WIRECARD REQUEST ERROR: Status ' + str(status_code) + \
'Request not sent. May contain errors as missing required parameters or transcription error. '
raise RequestException(error_message, response_json)


def set_api_authorization(api_token, api_key, sandbox=False):
global TOKEN
TOKEN['API_TOKEN'] = base64.b64encode('{}:{}'.format(api_token, api_key))
TOKEN['API_TOKEN'] = base64.b64encode('{}:{}'.format(api_token, api_key)).encode('utf-8')
TOKEN['base_url'] = constants.BASE_URL_SANDBOX if sandbox else constants.BASE_URL_LIVE


Expand All @@ -44,5 +50,9 @@ def request_get(url, data={}):
return validate_response(requests.get(url, params=data, headers=headers()))


def request_post(url, data):
return validate_response(requests.post(url, json=data, headers=headers()))
def request_post(url, data, access_token=None):
if access_token:
headers = oauth2_headers(access_token)
else:
headers = headers()
return validate_response(requests.post(url, json=data, headers=headers))

0 comments on commit 81d45dc

Please sign in to comment.