-
-
Save luzihang123/6cd2503501deb56f993f20d16a7fdc73 to your computer and use it in GitHub Desktop.
京东联盟开放平台API的一个通用的Client封装
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
''' | |
京东联盟开放平台API的一个通用的Client封装。 | |
京东联盟开放平台的文档详见 https://union.jd.com/openplatform | |
Example: | |
client = JdApiClient("<YOUR_APP_KEY>", "<YOUR_SECRET_KEY>") | |
resp = client.call("jd.union.open.goods.promotiongoodsinfo.query", | |
{'skuIds':'12072066'}) | |
print(resp.json()) | |
''' | |
import datetime | |
import requests | |
import hashlib | |
import json | |
import urllib.parse | |
JD_API_ROOT = 'https://router.jd.com/api' | |
class JdApiClient(object): | |
def __init__(self, app_key, secret_key): | |
self.app_key = app_key | |
self.secret_key = secret_key | |
def get_sign(self, params): | |
params_list = sorted(list(params.items()), key=lambda x: x[0]) | |
params_bytes = (self.secret_key + ''.join("%s%s" % (k, v) for k, v in params_list) + self.secret_key).encode('utf-8') | |
sign = hashlib.md5(params_bytes).hexdigest().upper() | |
return sign | |
def call(self, method, param_json, **kwargs): | |
params = { | |
"v": "1.0", | |
"method": method, | |
"app_key": self.app_key, | |
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
"format": "json", | |
"sign_method": "md5" | |
} | |
if isinstance(param_json, (dict, list)): | |
params["param_json"] = json.dumps(param_json) | |
else: | |
params["param_json"] = param_json | |
params['sign'] = self.get_sign(params) | |
resp = requests.get(JD_API_ROOT, params=params, **kwargs) | |
return resp | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment