api中文文档 mws_Amzon MWS API开发之 请求报告

时间一晃而过又过了两周,博客园更新的速度确实有点慢,今天我要分享的是对请求报告的调用。

在文档中,相信大家也看了下面这个流程图吧?

相关流程,在文档中也有细说,我就不一一去Copy了:http://docs.developer.amazonservices.com/zh_CN/reports/Reports_Overview.html

接着我们说ReportTypes 枚举,请求报告类型有很多种,我们可以可以使用ReportTypes 枚举,来指定报告类型,从而获取我们想要得到的相关数据。

ReportTypes枚举有以下分类:

具体大家可以参考以下详细文档:

http://docs.developer.amazonservices.com/zh_CN/reports/Reports_ReportType.html

接下来就以GetReportList为例

1 public classReportClient2 {3

4 privateReportClient() { }5

6

7 public ReportClient(stringreportType)8 {9 this.ReportType =reportType;10 }11

12 public string ReportType { get; set; }13

14 ///

15 ///获得账户信息16 ///

17 private staticAccountConfig Account18 {19 get

20 {21 returnAccountConfig.Instance;22 }23 }24

25

26

27 privateMarketplaceWebServiceConfig GetConfig()28 {29 var config = newMarketplaceWebServiceConfig();30 config.ServiceURL =Account.ServiceUrl;31 returnconfig;32 }33

34 privateMarketplaceWebServiceClient GetClient()35 {36 var config = this.GetConfig();37 var client = newMarketplaceWebServiceClient(Account.AccessKeyId, Account.SecretAccessKey, Account.AppName, Account.AppVersion, config);38 returnclient;39 }40

41 public voidGetReportList()42 {43 var reportList =GetReportListInfo();44 foreach (var item inreportList)45 {46 GetReport(item);47 }48

49 }50

51

52 private ListGetReportListInfo()53 {54 List reportIdList = new List();55 var client =GetClient();56 var request = newGetReportListRequest();57 request.Acknowledged = false;58 request.Merchant =Account.MerchantId;59 request.ReportTypeList = newTypeList();60 request.ReportTypeList.Type = new List() { ReportType };61 request.Marketplace =Account.MarketplaceId;62 request.AvailableFromDate = new DateTime(2014, 7, 15, 0, 0, 0);63 request.AvailableToDate = new DateTime(2014, 7, 31, 0, 0, 0);64

65 var response =client.GetReportList(request);66 var result =response.GetReportListResult;67 result.ReportInfo.ForEach(u =>reportIdList.Add(u.ReportId));68

69 returnreportIdList;70 }71

72

73 ///

74 ///获得请求报告: 未测试75 ///

76 ///

77 ///

78 ///

79 public void GetReport(stringreportId)80 {81 var client = this.GetClient();82 var request = newGetReportRequest();83 request.Merchant =Account.MerchantId;84 request.ReportId =reportId;85

86 string fileName =GetFilePath();87 request.Report =File.Open(fileName, FileMode.Create, FileAccess.ReadWrite);88 GetReportResponse response =client.GetReport(request);89 request.Report.Close();90 var result =response.GetReportResult;91 if (!result.IsSetContentMD5())92 return;93 }94

95

96 private stringGetFilePath()97 {98 return PathInfo.ReportPath + Account.AppName + "__" + DateTime.Now.ToFileTime() + ".txt";99 }100

101 }

View Code

大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。

根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。

当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。

接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。

1 public List GetContent(stringfileName)2 {3 //打开下载好了的文件

4 FileStream fs = newFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);5 StreamReader sr = newStreamReader(fs, System.Text.Encoding.UTF8);6 string content = sr.ReadLine(); //获得头行,也就是所有字段名称

7 string[] fields = content.Split('\t');8 List fileList = new List(fields);9

10 //接下来,我们记录字段对应所在的列的索引

11 int settlementIndex = fileList.IndexOf("settlement-id");12 int orderId = fileList.IndexOf("order-id");13 int shipmentId = fileList.IndexOf("shipment-id");14 int postedDataIndex = fileList.IndexOf("posted-date");15 int orderItemIndex = fileList.IndexOf("orderItemCode");16 int skuIndex = fileList.IndexOf("sku");17 int quantityIndex = fileList.IndexOf("quantity-purchased");18

19 int priceTypeIndex = fileList.IndexOf("price-type");20 int priceAmountIndex = fileList.IndexOf("price-amount");21 content = sr.ReadLine(); //读取下一行文字,注意,这行就开始是数据了。

22

23 List afList = new List();24 while (!string.IsNullOrEmpty(content))25 {26 content =sr.ReadLine();27 if (!string.IsNullOrEmpty(content))28 {29 string[] values = content.Split('\t'); //每个字段间都有“\t”间隔

30

31 AmazonFee af = newAmazonFee();32 af.AmazonOrderID =values[orderId];33 af.AmazonShop =Account.AppName;34 af.SKU =values[skuIndex];35 af.Quantity =values[quantityIndex];36 af.ShipmentId =values[shipmentId];37 af.Amount =values[priceAmountIndex];38 afList.Add(af); //获得值

39 }40 }41 returnafList;42 }

本文很简单,因为本人也是亚马逊MWS的菜鸟一名,刚接触40天,很多东西也不是很懂,不过希望感兴趣的朋友,大家一起交流学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值