Skip to content

Commit

Permalink
Merge pull request #5 from CashStar/US53062/belk-updates
Browse files Browse the repository at this point in the history
US53062 belk updates to allow headers and remove username/password
  • Loading branch information
parabatzis authored Jun 21, 2021
2 parents 0ff170e + d0b5f17 commit c121244
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
29 changes: 16 additions & 13 deletions opp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ def delete(self, **kwargs):


class API(object):
def __init__(self, **auth_params):
def __init__(self, headers=None, **auth_params):
"""
:rtype : object
:type auth_params: object
"""
url = opp.config.config.current_url
self.http_client = HTTPClient(url, auth_params)
self.http_client = HTTPClient(url, auth_params, headers)

def payments(self, payment_id=None):
return Payments(http_client=self.http_client, payment_id=payment_id)
Expand All @@ -97,13 +97,14 @@ def registrations(self, registration_id=None):


class HTTPClient(object):
def __init__(self, base_url, auth_params):
def __init__(self, base_url, auth_params, headers=None):
"""Initialize a new opp connection. Requires user name and password."""
logger.debug("START: OPP API connection with BASE_URL: {0}".format(base_url))
self.base_url = base_url
self.session = Session()
self.session.verify = opp.config.config.ssl_verify
self.auth_params = auth_params
self.headers = headers
self.operations = dict(GET=self.get, POST=self.post, PUT=self.put, DELETE=self.delete)
# for internal usage
self.response = None
Expand All @@ -126,7 +127,7 @@ def __call__(self, request_type, params, url, return_type):
if self.auth_params:
params.update(self.auth_params)
try:
result = self.operations[request_type](params, url, return_type)
result = self.operations[request_type](params, url, return_type, headers=self.headers)
logger.debug("SUCCESS: {0} {1}".format(request_type, url))
return result
except ValueError as v:
Expand All @@ -138,7 +139,7 @@ def __call__(self, request_type, params, url, return_type):
else:
raise ValueError()

def put(self, params, url, return_type):
def put(self, params, url, return_type, headers=None):
"""
:param params:
Expand All @@ -148,9 +149,10 @@ def put(self, params, url, return_type):
"""
return self._check_response(self.session.put(self.base_url + url, params,
timeout=opp.config.config.request_timeout,
hooks=dict(response=self._request_callback), ).json(), return_type)
hooks=dict(response=self._request_callback), headers=headers).json(),
return_type)

def post(self, params, url, return_type):
def post(self, params, url, return_type, headers=None):
"""
:param params:
Expand All @@ -159,10 +161,10 @@ def post(self, params, url, return_type):
:return:
"""
json = self.session.post(self.base_url + url, params, timeout=opp.config.config.request_timeout,
hooks=dict(response=self._request_callback)).json()
hooks=dict(response=self._request_callback), headers=headers).json()
return self._check_response(json, return_type)

def delete(self, params, url, return_type):
def delete(self, params, url, return_type, headers=None):
"""
:param params:
Expand All @@ -172,10 +174,10 @@ def delete(self, params, url, return_type):
"""
return self._check_response(self.session.delete(self.base_url + url, params=params,
timeout=opp.config.config.request_timeout,
hooks=dict(response=self._request_callback)).json(),
return_type)
hooks=dict(response=self._request_callback),
headers=headers).json(), return_type)

def get(self, params, url, return_type):
def get(self, params, url, return_type, headers=None):
"""
:param params:
Expand All @@ -185,7 +187,8 @@ def get(self, params, url, return_type):
"""
return self._check_response(self.session.get(self.base_url + url, params=params,
timeout=opp.config.config.request_timeout,
hooks=dict(response=self._request_callback)).json(), return_type)
hooks=dict(response=self._request_callback),
headers=headers).json(), return_type)

def _request_callback(self, r, *args, **kwargs):
"""
Expand Down
30 changes: 20 additions & 10 deletions opp/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@


class Authentication(object):
def __init__(self, user_id=None, password=None, entity_id=None):
def __init__(self, entity_id=None):
"""
:rtype : object
:param user_id:
:param password:
:param entity_id:
"""
self.user_id = user_id
self.password = password
self.entity_id = entity_id

def to_params(self):
return {"authentication.userId": self.user_id,
"authentication.password": self.password,
"authentication.entityId": self.entity_id}
return {"entityId": self.entity_id}


class Headers(object):
def __init__(self, bearer_token=None):
"""
:rtype: object
:param bearer_token:
"""''
self.bearer_token = bearer_token

def to_params(self):
return {"Authorization": self.bearer_token}



class BasicPayment(object):
Expand Down Expand Up @@ -771,15 +779,17 @@ def delete(self):


class API(object):
def __init__(self, authentication=None):
def __init__(self, authentication=None, headers=None):
"""
:param authentication:
:param headers:
:rtype : object
"""
self.authentication = authentication
self.core = opp.core.API(**authentication.to_params())
self.headers = headers
self.core = opp.core.API(headers.to_params(), **authentication.to_params())

def refunds(self, payment_id=None):
return Refunds(payment_id=payment_id, core=self.core)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='opp',
version='1.3.1',
version='1.3.2',
description='Python wrapper for OPP',
author='PAY.ON',
author_email='[email protected]',
Expand All @@ -30,5 +30,5 @@
'Topic :: Software Development :: Libraries :: Python Modules',
],
install_requires=install_requires,
download_url='https://github.com/OpenPaymentPlatform/python/tarball/1.3.1'
download_url='https://github.com/OpenPaymentPlatform/python/tarball/1.3.2'
)

0 comments on commit c121244

Please sign in to comment.