Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/aes signature #50

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
format code
  • Loading branch information
YoungWing committed Nov 9, 2022
commit 269688d39d83c16efc53febb943f1ce6b50a9fee
14 changes: 8 additions & 6 deletions aftership/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from aftership.signstring.signstring import SignString

from .const import AFTERSHIP_API_KEY, API_ENDPOINT, API_VERSION, AS_SIGNATURE_HMAC_SHA256, AS_API_KEY, CONTENT_TYPE, SIGNATURE_AES_HMAC_SHA256
from .const import AFTERSHIP_API_KEY, API_ENDPOINT, API_VERSION, AS_SIGNATURE_HMAC_SHA256, AS_API_KEY, CONTENT_TYPE, SIGNATURE_AES_HMAC_SHA256
from .util import get_api_key, get_api_secret


Expand All @@ -22,12 +22,12 @@ def make_request(method, path, **kwargs):

params_str = ""
if params:
params_str = '&'.join([ str(key)+'='+str(value) for key,value in params.items()])
params_str = '&'.join([str(key)+'='+str(value) for key, value in params.items()])

if not path.startswith("/"):
path = '/' + path

if len(params_str)>0:
if len(params_str) > 0:
path = '{}?{}'.format(path, params_str)

signature_type = kwargs.pop('signature_type', None)
Expand All @@ -38,22 +38,23 @@ def make_request(method, path, **kwargs):
content_type = None
if (method == "POST" or method == "PUT" or method == "PATCH") and body is not None:
content_type = CONTENT_TYPE

# if using SignString, you must use AS_API_KEY header
if signature_type == SIGNATURE_AES_HMAC_SHA256:
return request_with_aes_hmac256_signature(method, url, path, content_type, **kwargs)

return None


def request_with_token(method, url, **kwargs):
headers = kwargs.pop('headers', dict())
if headers.get(AFTERSHIP_API_KEY) is None and headers.get(AS_API_KEY) is None:
headers[AFTERSHIP_API_KEY] = get_api_key()

kwargs['headers'] = headers
return requests.request(method, url, **kwargs)


def request_with_aes_hmac256_signature(method, url, path, content_type, **kwargs):
headers = kwargs.pop('headers', dict())
if headers.get(AS_API_KEY, None) is None:
Expand All @@ -73,6 +74,7 @@ def request_with_aes_hmac256_signature(method, url, path, content_type, **kwargs
kwargs['headers'] = headers
return requests.request(method, url, **kwargs)


def gen_sign_string(method, path, body, headers, content_type):
s = SignString(headers[AS_API_KEY])
return s.gen_sign_string(method, path, body, headers, content_type)
2 changes: 1 addition & 1 deletion aftership/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def process_response(response):
json_content = response.json()
except json.JSONDecodeError:
raise InternalError

if response.status_code // 100 == 2:
return json_content['data']

Expand Down