Amzon MWS API開發之 請求報告


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天,很多東西也不是很懂,不過希望感興趣的朋友,大家一起交流學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM