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
获取相关的报告也分两种形式,有的报告通过:RequestReport 操作,有的是通过ManageReportSchedule或者GetReportList的API接口来获取。
接下来就以GetReportList为例
1 public class ReportClient 2 { 3 4 private ReportClient() { } 5 6 7 public ReportClient(string reportType) 8 { 9 this.ReportType = reportType; 10 } 11 12 public string ReportType { get; set; } 13 14 /// <summary> 15 /// 获得账户信息 16 /// </summary> 17 private static AccountConfig Account 18 { 19 get 20 { 21 return AccountConfig.Instance; 22 } 23 } 24 25 26 27 private MarketplaceWebServiceConfig GetConfig() 28 { 29 var config = new MarketplaceWebServiceConfig(); 30 config.ServiceURL = Account.ServiceUrl; 31 return config; 32 } 33 34 private MarketplaceWebServiceClient GetClient() 35 { 36 var config = this.GetConfig(); 37 var client = new MarketplaceWebServiceClient(Account.AccessKeyId, Account.SecretAccessKey, Account.AppName, Account.AppVersion, config); 38 return client; 39 } 40 41 public void GetReportList() 42 { 43 var reportList = GetReportListInfo(); 44 foreach (var item in reportList) 45 { 46 GetReport(item); 47 } 48 49 } 50 51 52 private List<string> GetReportListInfo() 53 { 54 List<string> reportIdList = new List<string>(); 55 var client = GetClient(); 56 var request = new GetReportListRequest(); 57 request.Acknowledged = false; 58 request.Merchant = Account.MerchantId; 59 request.ReportTypeList = new TypeList(); 60 request.ReportTypeList.Type = new List<string>() { 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 return reportIdList; 70 } 71 72 73 /// <summary> 74 /// 获得请求报告: 未测试 75 /// </summary> 76 /// <param name="client"></param> 77 /// <param name="reportId"></param> 78 /// <returns></returns> 79 public void GetReport(string reportId) 80 { 81 var client = this.GetClient(); 82 var request = new GetReportRequest(); 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 string GetFilePath() 97 { 98 return PathInfo.ReportPath + Account.AppName + "__" + DateTime.Now.ToFileTime() + ".txt"; 99 } 100 101 }
大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。
根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。
当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。
接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。
1 public List<AmazonFee> GetContent(string fileName) 2 { 3 //打开下载好了的文件 4 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 5 StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); 6 string content = sr.ReadLine(); //获得头行,也就是所有字段名称 7 string[] fields = content.Split('\t'); 8 List<string> fileList = new List<string>(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<AmazonFee> afList = new List<AmazonFee>(); 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 = new AmazonFee(); 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 return afList; 42 }
本文很简单,因为本人也是亚马逊MWS的菜鸟一名,刚接触40天,很多东西也不是很懂,不过希望感兴趣的朋友,大家一起交流学习。