Created
August 11, 2020 11:27
-
-
Save mavitm/20f11b6a3b349053ce0496f168a467e9 to your computer and use it in GitHub Desktop.
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
import hashlib | |
from collections import OrderedDict | |
import requests | |
def sort_od(od): | |
res = OrderedDict() | |
for k, v in sorted(od.items()): | |
if isinstance(v, dict): | |
res[k] = sort_od(v) | |
else: | |
res[k] = v | |
return res | |
def implode_recursive(arr, separator): | |
str_p = '' | |
if isinstance(arr, dict): | |
values = arr.values() | |
else: | |
values = arr | |
for val in values: | |
if isinstance(val, (list, tuple, dict)): | |
str_p = str_p + str(implode_recursive(val, separator)) + separator | |
else: | |
str_p = str_p + str(val) + separator | |
return str_p[0:len(str_p) - len(separator)] | |
def getSign(userSecret, allParams): | |
#keys that will not be included in the request structure, edit this part according to you | |
exceptedParams = ( | |
'token', | |
'auth', | |
'channel', | |
'clientId', | |
'locale' | |
) | |
separator = '' | |
# 1. Remove | |
params = {} | |
for (key, value) in allParams.items(): | |
if key not in exceptedParams: | |
params[key] = value | |
# 2. Sort | |
params = sort_od(params) | |
# 3. Join | |
strParams = implode_recursive(params, separator) | |
# 4. md5 | |
m = hashlib.md5() | |
m.update(str(strParams + userSecret).encode('utf-8')) | |
sign = m.hexdigest() | |
# 5. Signature | |
return sign | |
def call_api(url, params): | |
try: | |
r = requests.get(url=url, params=params) | |
# extracting data in json format | |
data = r.json() | |
print(data) | |
return data | |
except requests.exceptions.RequestException as e: # This is the correct syntax | |
print(e) | |
print('skip') | |
return 'false' | |
def get_status(): | |
# defining a params dict for the parameters to be sent to the API | |
params = {'code': 'inquiry'} | |
userSecret = "asdfghjkl221" | |
token = getSign(userSecret, params) | |
params[token] = token | |
# api-endpoint | |
url = 'https://********/order-status-get-mobile-api/1/1/' | |
# sending get request and saving the response as response object | |
data = call_api(url, params) | |
if data['code'] != '200': | |
return 'false' | |
if not data['data']: | |
return 'false' | |
orders = data['data'] | |
return orders | |
print(get_status()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment