CDON API RESTful Api GET 请求
CDON API RESTful Api GET request
我目前正在从 cdon 获取客户数据,这是一个 e-commerce 平台。他们在此处提供 API 文档:
首先让我向您展示我的代码:
myToken = '<token here>'
myUrl = 'https://admin.marketplace.cdon.com/api/reports/d8578ef8-723d-46cb-bb08-af8c9b5cca4c'
head = {'Authorization': 'token {}'.format(myToken),
'Status':'Online',
'format':'json'}
filters = '?filter={"Status":["Online"],"format": ["json"] }}'
response = requests.get(myUrl + filters, headers=head)
report = response.json()
print(report.products)
这仅返回参数。例如在 JSON: CDON Github
状态有一个值在线这个online
是一组我只想得到的项目。
我想要得到的是这样的回应:
{
"Products": [
{
"SKU": "322352",
"Title": "Fabric Cover",
"GTIN": "532523626",
"ManufacturerArticleNumber": "",
"StatusCDON": "Online",
"ExposeStatusCDON": "Buyable",
"InStock": 0,
"InStockCDON": 0,
"CurrentPriceSE": null,
"OrdinaryPriceSE": null,
"CurrentPriceCDONSE": 299.0000,
"OrdinaryPriceCDONSE": null,
"CurrentPriceDK": null,
"OrdinaryPriceDK": null,
"CurrentPriceCDONDK": null,
"OrdinaryPriceCDONDK": null,
"CurrentPriceNO": null,
"OrdinaryPriceNO": null,
"CurrentPriceCDONNO": null,
"OrdinaryPriceCDONNO": null,
"CurrentPriceFI": null,
"OrdinaryPriceFI": null,
"CurrentPriceCDONFI": null,
"OrdinaryPriceCDONFI": null
},
这意味着 在线
项目的完整列表
我应该怎么放这个...在所有API中我试过这个很混乱,这甚至是RestFul?如果我能实现此 C# 示例代码的 python 等价物:
public string Post(Guid repordId, string path)
{
var filter = new JavaScriptSerializer().Serialize(new
{
States = new[] { "0" } // Pending state
});
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair("ReportId", repordId.ToString()),
new KeyValuePair("format", "json"),
new KeyValuePair("filter", filter)
});
var httpClient = new HttpClient() { BaseAddress = new Uri("https://admin.marketplace.cdon.com/") };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("api", ApiKey);
var response = httpClient.PostAsync(path, content).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync().Result;
}
我可能能够理解这个 API 是如何工作的,我得到的响应是从他们的报告功能中以 JSON 格式手动获取的。
Image
我做了很多尝试,在那个代码(我的代码)上我停了下来,在这个上面待了 4 个小时让我放弃并询问。相信我已经搜索了尽可能多的参考资料。真是一头雾水。
如何获得我想要的回复?通过 url 过滤?或者通过 header?这甚至是 restful?帮助 T_T
文档在第一行指出,重点是我的:
In order to generate a report you perform a POST call to the reports API with the parameters you wish to use for the report.
您的 Python 代码没有发出 POST 请求,您正在尝试 GET 请求。文档继续
[...] to filter on Swedish orders you set the CountryCodes
attribute to “Sweden” and to get returned and cancelled orders you set
the States attribute to 2 and 3. So in the end the filter would look
like this:
{
"CountryCodes": [ "Sweden" ],
"States": ["2", "3"]
}
所以你需要准备一个filter
object(一个Python中的dictionary)你想要的过滤器。幸运的是,字典的 Python 语法是等价的(Python 是灵活的,也允许 single-quoted 字符串):
filter = {
'CountryCodes': [ 'Sweden' ],
'States': [ '0' ]
}
文档继续
You then post the parameters as form data (content-type:
application/x-www-form-urlencoded
) so the request body would look like
this:
ReportId=d4ea173d-bfbc-48f5-b121-60f1a5d35a34&format=json&filter={"CountryCodes":["Sweden"],"States":["2","3"]}
application/x-www-form-urlencoded
是 HTTP post 的默认值,请求模块知道并自动为您执行此操作。您需要做的就是准备一个 data
字典,其中将包含您想要 post 的数据。
data = {
'ReportId': 'd4ea173d-bfbc-48f5-b121-60f1a5d35a34',
'format': 'json'
'filter': json.dumps(filter)
}
filter
参数应该是 JSON 格式。您必须自己通过 json.dumps()
.
对其进行编码
import json
head = { ... as above }
filter = { ... as above }
data = { ... as above }
response = requests.post(url, data, header=head)
我会把正确设置授权 header 作为你的练习。部分是因为它并不难,部分是因为我无意在这个网站上创建一个 API 密钥只是为了测试这个,部分是因为你当前的 header 完全有可能已经工作了。
我目前正在从 cdon 获取客户数据,这是一个 e-commerce 平台。他们在此处提供 API 文档:
首先让我向您展示我的代码:
myToken = '<token here>'
myUrl = 'https://admin.marketplace.cdon.com/api/reports/d8578ef8-723d-46cb-bb08-af8c9b5cca4c'
head = {'Authorization': 'token {}'.format(myToken),
'Status':'Online',
'format':'json'}
filters = '?filter={"Status":["Online"],"format": ["json"] }}'
response = requests.get(myUrl + filters, headers=head)
report = response.json()
print(report.products)
这仅返回参数。例如在 JSON: CDON Github
状态有一个值在线这个online
是一组我只想得到的项目。
我想要得到的是这样的回应:
{
"Products": [
{
"SKU": "322352",
"Title": "Fabric Cover",
"GTIN": "532523626",
"ManufacturerArticleNumber": "",
"StatusCDON": "Online",
"ExposeStatusCDON": "Buyable",
"InStock": 0,
"InStockCDON": 0,
"CurrentPriceSE": null,
"OrdinaryPriceSE": null,
"CurrentPriceCDONSE": 299.0000,
"OrdinaryPriceCDONSE": null,
"CurrentPriceDK": null,
"OrdinaryPriceDK": null,
"CurrentPriceCDONDK": null,
"OrdinaryPriceCDONDK": null,
"CurrentPriceNO": null,
"OrdinaryPriceNO": null,
"CurrentPriceCDONNO": null,
"OrdinaryPriceCDONNO": null,
"CurrentPriceFI": null,
"OrdinaryPriceFI": null,
"CurrentPriceCDONFI": null,
"OrdinaryPriceCDONFI": null
},
这意味着 在线
项目的完整列表我应该怎么放这个...在所有API中我试过这个很混乱,这甚至是RestFul?如果我能实现此 C# 示例代码的 python 等价物:
public string Post(Guid repordId, string path)
{
var filter = new JavaScriptSerializer().Serialize(new
{
States = new[] { "0" } // Pending state
});
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair("ReportId", repordId.ToString()),
new KeyValuePair("format", "json"),
new KeyValuePair("filter", filter)
});
var httpClient = new HttpClient() { BaseAddress = new Uri("https://admin.marketplace.cdon.com/") };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("api", ApiKey);
var response = httpClient.PostAsync(path, content).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync().Result;
}
我可能能够理解这个 API 是如何工作的,我得到的响应是从他们的报告功能中以 JSON 格式手动获取的。
Image
我做了很多尝试,在那个代码(我的代码)上我停了下来,在这个上面待了 4 个小时让我放弃并询问。相信我已经搜索了尽可能多的参考资料。真是一头雾水。
如何获得我想要的回复?通过 url 过滤?或者通过 header?这甚至是 restful?帮助 T_T
文档在第一行指出,重点是我的:
In order to generate a report you perform a POST call to the reports API with the parameters you wish to use for the report.
您的 Python 代码没有发出 POST 请求,您正在尝试 GET 请求。文档继续
[...] to filter on Swedish orders you set the
CountryCodes
attribute to “Sweden” and to get returned and cancelled orders you set the States attribute to 2 and 3. So in the end the filter would look like this:{ "CountryCodes": [ "Sweden" ], "States": ["2", "3"] }
所以你需要准备一个filter
object(一个Python中的dictionary)你想要的过滤器。幸运的是,字典的 Python 语法是等价的(Python 是灵活的,也允许 single-quoted 字符串):
filter = {
'CountryCodes': [ 'Sweden' ],
'States': [ '0' ]
}
文档继续
You then post the parameters as form data (
content-type: application/x-www-form-urlencoded
) so the request body would look like this:ReportId=d4ea173d-bfbc-48f5-b121-60f1a5d35a34&format=json&filter={"CountryCodes":["Sweden"],"States":["2","3"]}
application/x-www-form-urlencoded
是 HTTP post 的默认值,请求模块知道并自动为您执行此操作。您需要做的就是准备一个 data
字典,其中将包含您想要 post 的数据。
data = {
'ReportId': 'd4ea173d-bfbc-48f5-b121-60f1a5d35a34',
'format': 'json'
'filter': json.dumps(filter)
}
filter
参数应该是 JSON 格式。您必须自己通过 json.dumps()
.
import json
head = { ... as above }
filter = { ... as above }
data = { ... as above }
response = requests.post(url, data, header=head)
我会把正确设置授权 header 作为你的练习。部分是因为它并不难,部分是因为我无意在这个网站上创建一个 API 密钥只是为了测试这个,部分是因为你当前的 header 完全有可能已经工作了。