Amzon MWS API开发之 请求报告
2014-08-06 19:48
197 查看
时间一晃而过又过了两周,博客园更新的速度确实有点慢,今天我要分享的是对请求报告的调用。
在文档中,相信大家也看了下面这个流程图吧?
View Code
大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。
根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。
当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。
接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。
本文很简单,因为本人也是亚马逊MWS的菜鸟一名,刚接触40天,很多东西也不是很懂,不过希望感兴趣的朋友,大家一起交流学习。
在文档中,相信大家也看了下面这个流程图吧?
public class ReportClient { private ReportClient() { } public ReportClient(string reportType) { this.ReportType = reportType; } public string ReportType { get; set; } /// <summary> /// 获得账户信息 /// </summary> private static AccountConfig Account { get { return AccountConfig.Instance; } } private MarketplaceWebServiceConfig GetConfig() { var config = new MarketplaceWebServiceConfig(); config.ServiceURL = Account.ServiceUrl; return config; } private MarketplaceWebServiceClient GetClient() { var config = this.GetConfig(); var client = new MarketplaceWebServiceClient(Account.AccessKeyId, Account.SecretAccessKey, Account.AppName, Account.AppVersion, config); return client; } public void GetReportList() { var reportList = GetReportListInfo(); foreach (var item in reportList) { GetReport(item); } } private List<string> GetReportListInfo() { List<string> reportIdList = new List<string>(); var client = GetClient(); var request = new GetReportListRequest(); request.Acknowledged = false; request.Merchant = Account.MerchantId; request.ReportTypeList = new TypeList(); request.ReportTypeList.Type = new List<string>() { ReportType }; request.Marketplace = Account.MarketplaceId; request.AvailableFromDate = new DateTime(2014, 7, 15, 0, 0, 0); request.AvailableToDate = new DateTime(2014, 7, 31, 0, 0, 0); var response = client.GetReportList(request); var result = response.GetReportListResult; result.ReportInfo.ForEach(u => reportIdList.Add(u.ReportId)); return reportIdList; } /// <summary> /// 获得请求报告: 未测试 /// </summary> /// <param name="client"></param> /// <param name="reportId"></param> /// <returns></returns> public void GetReport(string reportId) { var client = this.GetClient(); var request = new GetReportRequest(); request.Merchant = Account.MerchantId; request.ReportId = reportId; string fileName = GetFilePath(); request.Report = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite); GetReportResponse response = client.GetReport(request); request.Report.Close(); var result = response.GetReportResult; if (!result.IsSetContentMD5()) return; } private string GetFilePath() { return PathInfo.ReportPath + Account.AppName + "__" + DateTime.Now.ToFileTime() + ".txt"; } }
View Code
大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。
根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。
当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。
接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。
public List<AmazonFee> GetContent(string fileName) { //打开下载好了的文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); string content = sr.ReadLine(); //获得头行,也就是所有字段名称 string[] fields = content.Split('\t'); List<string> fileList = new List<string>(fields); //接下来,我们记录字段对应所在的列的索引 int settlementIndex = fileList.IndexOf("settlement-id"); int orderId = fileList.IndexOf("order-id"); int shipmentId = fileList.IndexOf("shipment-id"); int postedDataIndex = fileList.IndexOf("posted-date"); int orderItemIndex = fileList.IndexOf("orderItemCode"); int skuIndex = fileList.IndexOf("sku"); int quantityIndex = fileList.IndexOf("quantity-purchased"); int priceTypeIndex = fileList.IndexOf("price-type"); int priceAmountIndex = fileList.IndexOf("price-amount"); content = sr.ReadLine(); //读取下一行文字,注意,这行就开始是数据了。 List<AmazonFee> afList = new List<AmazonFee>(); while (!string.IsNullOrEmpty(content)) { content = sr.ReadLine(); if (!string.IsNullOrEmpty(content)) { string[] values = content.Split('\t'); //每个字段间都有“\t”间隔 AmazonFee af = new AmazonFee(); af.AmazonOrderID = values[orderId]; af.AmazonShop = Account.AppName; af.SKU = values[skuIndex]; af.Quantity = values[quantityIndex]; af.ShipmentId = values[shipmentId]; af.Amount = values[priceAmountIndex]; afList.Add(af); //获得值 } } return afList; }
本文很简单,因为本人也是亚马逊MWS的菜鸟一名,刚接触40天,很多东西也不是很懂,不过希望感兴趣的朋友,大家一起交流学习。
相关文章推荐
- Amzon MWS API开发之 请求报告
- Amzon MWS API开发之 上传数据
- Amzon MWS API开发之订单接口
- Amzon MWS API开发之 上传数据
- Amzon MWS API开发之订单接口
- Amzon MWS API开发之订单接口
- 使用SpringMvc 开发 RESTful API 用户详情请求、JsonView注解使用(二)
- 【笔记】vue-cli 开发环境中跨域连接后台api(vue-resource 跨域post 请求)
- 模拟post请求-->测试api是否可用-->再交给ios开发
- 梦网云通讯获取状态报告API接口get_rpt说明POST请求示例
- (0029) iOS 开发之API & HTTP 请求调试网站
- (微信API接口开发) 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- Spring MVC开发restful API之用户查询请求
- 如何测量并报告ASP.NET Core Web API请求的响应时间
- iOS开发--大众点评API请求签名生成代码示例
- React项目配置4(如何在开发时跨域获取api请求)
- 用WinINet Api 开发FTP客户端 (三) ------ 完整代码
- 用WinINet Api 开发FTP客户端 (一)
- Google Maps API 2.0解析(11-GCopyrightCollection动态向图片服务器请求代码)
- 利用google maps API开发应用精选