Commit ddec80b6 by Long Phan

u

parents
from lazop.base import *
\ No newline at end of file
# -*- coding: utf-8 -*-
'''
Created on 2018-03-21
@author: xuteng.xt
'''
import requests
import time
import hmac
import hashlib
import json
import mimetypes
import itertools
import random
import logging
import os
from os.path import expanduser
import socket
import platform
# dir = os.getenv('HOME')
dir = expanduser("~")
isExists = os.path.exists(dir + "/logs")
if not isExists:
os.makedirs(dir + "/logs")
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.ERROR)
handler = logging.FileHandler(dir + "/logs/lazopsdk.log." + time.strftime("%Y-%m-%d", time.localtime()))
handler.setLevel(logging.ERROR)
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
P_SDK_VERSION = "lazop-sdk-python-20181207"
P_APPKEY = "app_key"
P_ACCESS_TOKEN = "access_token"
P_TIMESTAMP = "timestamp"
P_SIGN = "sign"
P_SIGN_METHOD = "sign_method"
P_PARTNER_ID = "partner_id"
P_DEBUG = "debug"
P_CODE = 'code'
P_TYPE = 'type'
P_MESSAGE = 'message'
P_REQUEST_ID = 'request_id'
P_API_GATEWAY_URL_SG = 'https://api.lazada.sg/rest'
P_API_GATEWAY_URL_MY = 'https://api.lazada.com.my/rest'
P_API_GATEWAY_URL_VN = 'https://api.lazada.vn/rest'
P_API_GATEWAY_URL_TH = 'https://api.lazada.co.th/rest'
P_API_GATEWAY_URL_PH = 'https://api.lazada.com.ph/rest'
P_API_GATEWAY_URL_ID = 'https://api.lazada.co.id/rest'
P_API_AUTHORIZATION_URL = 'https://auth.lazada.com/rest'
P_LOG_LEVEL_DEBUG = "DEBUG"
P_LOG_LEVEL_INFO = "INFO"
P_LOG_LEVEL_ERROR = "ERROR"
def sign(secret,api, parameters):
#===========================================================================
# @param secret
# @param parameters
#===========================================================================
sort_dict = sorted(parameters)
parameters_str = "%s%s" % (api,
str().join('%s%s' % (key, parameters[key]) for key in sort_dict))
h = hmac.new(secret.encode(encoding="utf-8"), parameters_str.encode(encoding="utf-8"), digestmod=hashlib.sha256)
return h.hexdigest().upper()
def mixStr(pstr):
if(isinstance(pstr, str)):
return pstr
elif(isinstance(pstr, unicode)):
return pstr.encode('utf-8')
else:
return str(pstr)
def logApiError(appkey, sdkVersion, requestUrl, code, message):
localIp = socket.gethostbyname(socket.gethostname())
platformType = platform.platform()
logger.error("%s^_^%s^_^%s^_^%s^_^%s^_^%s^_^%s^_^%s" % (
appkey, sdkVersion,
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
localIp, platformType, requestUrl, code, message))
class LazopRequest(object):
def __init__(self,api_pame,http_method = 'POST'):
self._api_params = {}
self._file_params = {}
self._api_pame = api_pame
self._http_method = http_method
def add_api_param(self,key,value):
self._api_params[key] = value
def add_file_param(self,key,value):
self._file_params[key] = value
class LazopResponse(object):
def __init__(self):
self.type = None
self.code = None
self.message = None
self.request_id = None
self.body = None
def __str__(self, *args, **kwargs):
sb = "type=" + mixStr(self.type) +\
" code=" + mixStr(self.code) +\
" message=" + mixStr(self.message) +\
" requestId=" + mixStr(self.request_id)
return sb
class LazopClient(object):
log_level = P_LOG_LEVEL_ERROR
def __init__(self, server_url,app_key,app_secret,timeout=30):
self._server_url = server_url
self._app_key = app_key
self._app_secret = app_secret
self._timeout = timeout
def execute(self, request,access_token = None):
sys_parameters = {
P_APPKEY: self._app_key,
P_SIGN_METHOD: "sha256",
P_TIMESTAMP: str(int(round(time.time()))) + '000',
P_PARTNER_ID: P_SDK_VERSION
}
if(self.log_level == P_LOG_LEVEL_DEBUG):
sys_parameters[P_DEBUG] = 'true'
if(access_token):
sys_parameters[P_ACCESS_TOKEN] = access_token
application_parameter = request._api_params;
sign_parameter = sys_parameters.copy()
sign_parameter.update(application_parameter)
sign_parameter[P_SIGN] = sign(self._app_secret,request._api_pame,sign_parameter)
api_url = "%s%s" % (self._server_url,request._api_pame)
full_url = api_url + "?";
for key in sign_parameter:
full_url += key + "=" + str(sign_parameter[key]) + "&";
full_url = full_url[0:-1]
try:
if(request._http_method == 'POST' or len(request._file_params) != 0) :
r = requests.post(api_url,sign_parameter,files=request._file_params, timeout=self._timeout)
else:
r = requests.get(api_url,sign_parameter, timeout=self._timeout)
except Exception as err:
logApiError(self._app_key, P_SDK_VERSION, full_url, "HTTP_ERROR", str(err))
raise err
response = LazopResponse()
jsonobj = r.json()
if P_CODE in jsonobj:
response.code = jsonobj[P_CODE]
if P_TYPE in jsonobj:
response.type = jsonobj[P_TYPE]
if P_MESSAGE in jsonobj:
response.message = jsonobj[P_MESSAGE]
if P_REQUEST_ID in jsonobj:
response.request_id = jsonobj[P_REQUEST_ID]
if response.code is not None and response.code != "0":
logApiError(self._app_key, P_SDK_VERSION, full_url, response.code, response.message)
else:
if(self.log_level == P_LOG_LEVEL_DEBUG or self.log_level == P_LOG_LEVEL_INFO):
logApiError(self._app_key, P_SDK_VERSION, full_url, "", "")
response.body = jsonobj
return response
2018-04-22 15:53:49,044 - lazop.base - ERROR - 192.168.31.103^_^Darwin-17.5.0-x86_64-i386-64bit^_^https://auth.lazada.com/rest/auth/token/create?app_key=100627&sign_method=sha256&timestamp=1524383629000&partner_id=lazop-sdk-python-20180422&code=0_1VD1csgglmJhiCks6GrblB0A2&sign=A3D8AD2A6F52D7F87769312EC0BF81AEDEF4CD5A3CB6A122F77B307112F0028E^_^InvalidCode^_^Invalid authorization code
192.168.31.103^_^Darwin-17.5.0-x86_64-i386-64bit^_^https://auth.lazada.com/rest/auth/token/create?app_key=100627&sign_method=sha256&timestamp=1524383748000&partner_id=lazop-sdk-python-20180422&code=0_1VD1csgglmJhiCks6GrblB0A2&sign=520B38C698EF4AA678353C22D2E134AD1DF4005A88E025C69519AA38C89B42C4^_^InvalidCode^_^Invalid authorization code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(
name='lazop-sdk-python',
version='1.1.0',
author='top',
author_email='xuteng.xt@alibaba-inc.com',
packages=find_packages(),
install_requires=[],
license='MIT',
)
# -*- coding: utf-8 -*-
import lazop
# params 1 : gateway url
# params 2 : appkey
# params 3 : appSecret
client = lazop.LazopClient('https://api.lazada.test/rest', '${appKey}', '${appSecret}')
# create a api request set GET mehotd
# default http method is POST
request = lazop.LazopRequest('/mock/api/get','GET')
# simple type params ,Number ,String
request.add_api_param('api_id','1')
response = client.execute(request)
#response = client.execute(request,access_token)
# response type nil,ISP,ISV,SYSTEM
# nil :no error
# ISP : API Service Provider Error
# ISV : API Request Client Error
# SYSTEM : Lazop platform Error
print(response.type)
# response code, 0 is no error
print(response.code)
# response error message
print(response.message)
# response unique id
print(response.request_id)
# full response
print(response.body)
# -*- coding: utf-8 -*-
import lazop
# params 1 : gateway url
# params 2 : appkey
# params 3 : appSecret
client = lazop.LazopClient('https://api.lazada.test/rest', '${appKey}', '${appSecret}')
# create a api request
request = lazop.LazopRequest('/xiaoxuan/mockfileupload')
# simple type params ,Number ,String
request.add_api_param('file_name','pom.xml')
# file params, value should be file content
request.add_file_param('file_bytes',open('/Users/xt/Documents/work/tasp/tasp/pom.xml').read())
response = client.execute(request)
#response = client.execute(request,access_token)
# response type nil,ISP,ISV,SYSTEM
# nil :no error
# ISP : API Service Provider Error
# ISV : API Request Client Error
# SYSTEM : Lazop platform Error
print(response.type)
# response code, 0 is no error
print(response.code)
# response error message
print(response.message)
# response unique id
print(response.request_id)
# full response
print(response.body)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment