python-3.x - CDON API RESTful Api GET 请求
问题描述
我目前正在从 cdon 获取客户数据,它是一个电子商务平台。他们在这里有他们的 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这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 格式的报告函数中手动获取的。
我做了很多尝试,在那个代码(我的代码)我停止了,在这上面呆了 4 个小时让我放弃并问。相信我已经搜索了尽可能多的参考资料。这真的很混乱。
如何获得我想要的响应?通过网址过滤?或通过标题?这还算安宁吗?帮助T_T
解决方案
文档在第一行中指出,强调我的:
为了生成报告,您使用您希望用于报告的参数对报告 API执行 POST 调用。
您的 Python 代码没有发出 POST 请求,您正在尝试 GET 请求。文档继续
[...] 要过滤瑞典订单,您将
CountryCodes
属性设置为“瑞典”,并且要返回和取消订单,您将 States 属性设置为 2 和 3。所以最终过滤器将如下所示:{ "CountryCodes": [ "Sweden" ], "States": ["2", "3"] }
因此,您需要准备一个带有所需过滤器的filter
对象(Python 中的字典)。幸运的是,字典的 Python 语法是等价的(Python 很灵活,也允许使用单引号字符串):
filter = {
'CountryCodes': [ 'Sweden' ],
'States': [ '0' ]
}
文档继续
然后,您将参数作为表单数据 (
content-type: application/x-www-form-urlencoded
) 发布,因此请求正文将如下所示:ReportId=d4ea173d-bfbc-48f5-b121-60f1a5d35a34&format=json&filter={"CountryCodes":["Sweden"],"States":["2","3"]}
application/x-www-form-urlencoded
是 HTTP 发布的默认设置,请求模块知道这一点并自动为您执行此操作。您需要做的就是准备一个data
包含您要发布的数据的字典。
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)
我将把正确设置 Authorization 标头作为练习留给你。部分是因为它并不难,部分是因为我无意在这个网站上创建一个 API 密钥来测试它,部分是因为您当前的标头完全有可能已经工作。
推荐阅读
- build - vsts中msbuild的路径是什么
- angular - Angular 5 字符串常量外化
- arrays - 如何将可观察对象和属性(字段)数组映射到属性?
- tcp - 发送一个大包与两个小包
- python - 如何在运行时访问容器内的环境变量?
- android - 从 Play 商店安装时,Android Cordova 应用程序崩溃
- c++ - 如何在 C++ 中将 '\' 替换为 '\\' 以处理转义序列?
- javascript - 如何修复 wp 超级缓存错误消息?
- wpf - 如何允许手写笔/笔和手指触摸滚动 WPF Scrollviewer
- c++ - 从不同的容器中移动构造`std::map`