Home > other >  Ali cloud API calls
Ali cloud API calls

Time:11-23

Masters, about ali cloud API calls, studied several days, still persist, consult everybody, the code is as follows:

# aliyun
The from aliyunsdkcore. Client import AcsClient
The from aliyunsdkcore. Acs_exception. Exceptions import ClientException
The from aliyunsdkcore. Acs_exception. Exceptions import ServerException
The from aliyunsdkcore. Request the import CommonRequest

Def getAladdinConf () :
The options={
'hzHost' : 'XXX. XXX. XXX. XXX',
"AppKey" : "XXXXXXXXX",
'AppSecret' : 'XXXXX',
'region_id' : 'cn - Shanghai,
'baseURL' : 'http://ali-api.XXXXXX-e.com/',
'reqheader: {
'the content-type:' application/json,
'Authorization' : 'Bearer XXXXXXXXXXXXX',
'cache-control' : 'no - Cache'
},
'stock' : {' method ':' POST ', 'url' : 'index. PHP/rest/V2/product/stock'}, # inventory
'price: {' method: "POST", "url" :' index. PHP/rest/V2/product/price '}, # prices
'update: {' method' : 'GET' and 'url' : 'index. PHP/rest/V2/product/update'}, # product updates
'new' : {' method ':' GET 'and' url ':' index. PHP/rest/V2/product/new '} # products new
}
Return the options

Def getAladdinRequest (data_type, logfile=False) :
Result={' MSG ':' OK 'and' data ':'}
Curr=datetime. Datetime. Now (). The strftime (' % % Y - m - H: % d % % m: % S ')
The conf=getAladdinConf ()
The client=AcsClient (conf [' AppKey], the conf [' AppSecret], the conf [' region_id '])
If data_type in conf:
# url=conf [' baseURL] + conf [data_type] [' url ']
Url='http://0bbbae2b380a49238d3b7317d2cd7e86-cn-hangzhou.alicloudapi.com/index.php/rest/V2/product/price' # test url address
R=CommonRequest ()
R.s et_accept_format (' json)
R.s et_domain (url)
R.s et_method (' POST ')
R.s et_action_name (' DescribeInstanceStatus)
R.s et_version (' 2014-05-26 ')
Of state Richard armitage dd_query_param (" RegionId ", the conf [' region_id '])
The response=client. Do_action (r)
Ret=STR (response, encoding="utf-8")
The line=ret
If logfile:
Logfile. Write (' & gt;> - '+ curr +' \ n ')
Logfile. Write ('... '+ line +' \ n ')
The else:
Logfile. Write (' & gt;> - '+ curr +' \ n ')
Logfile. Write (' & gt;> Unknown Error, data type: '+ data_type +' \ n ')
LEN_TH=41943040 # 40 m
If len (line) & gt; LEN_TH:
Result [' MSG ']='SIZE_TOO_BIG'
The else:
Result [' data ']=line
Return the result

If __name__=="__main__" :
GetAladdinRequest_result=getAladdinRequest (' price ')
Print (getAladdinRequest_result)

CodePudding user response:

Def getAladdinRequest (data_type, request_data=https://bbs.csdn.net/topics/[], logfile=False) :
Result={' MSG ':' OK 'and' data ':'}
Curr=datetime. Datetime. Now (). The strftime (' % % Y - m - H: % d % % m: % S ')
The conf=getAladdinConf ()
Cli=AcsClient (conf [' AppKey], the conf [' AppSecret], the conf [' region_id '])
If data_type in conf:
# url=conf [' baseURL] + conf [data_type] [' url ']
Url='http://0bbbae2b380a49238d3b7317d2cd7e86-cn-hangzhou.alicloudapi.com/index.php/rest/V2/product/price' # test url
Headers=conf [' reqheader]
# host=conf [' hzHost]
Print (" -- -- -- & gt; Access to the '+ url)
# print (' - & gt; Headers: "+ json. Dumps (Headers))
If the conf [data_type] [' method ']=='POST' :
Content=json. Dumps ({" sku ": request_data})
Req_post=requests. Post (url=url, headers=headers, data=https://bbs.csdn.net/topics/payload)

R=cli. Do_action_with_exception (req_post) # feeling is here have a question, the master can help see,
The line=r.t ext
The else:
R=requests. Get (url, headers=headers)
The line=r.t ext
If logfile:
Logfile. Write (' & gt;> - '+ curr +' \ n ')
Logfile. Write (' & gt;> '+ STR (request_data) +' \ n ')
Logfile. Write ('... '+ line +' \ n ')
The else:
Logfile. Write (' & gt;> - '+ curr +' \ n ')
Logfile. Write (' & gt;> Unknown Error, data type: '+ data_type +' \ n ')
LEN_TH=41943040 # 40 m
If len (line) & gt; LEN_TH:
Result [' MSG ']='SIZE_TOO_BIG'
The else:
Result [' data ']=line
Return the result

The error information:
R=cli. Do_action_with_exception (req_post)
The File "D: \ apps \ py36 \ lib \ site - packages \ aliyunsdkcore \ client py", line 472, in do_action_with_exception
Acs_request. Set_accept_format (' JSON)
AttributeError: 'the Response object has no attribute' set_accept_format '
  • Related