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为例
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(, , , , , );
request.AvailableToDate = new DateTime(, , , , , ); 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";
} }
大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。
根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。
当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。
接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。
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开发之 上传数据
亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...
- Amzon MWS API开发之订单接口
Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据. 在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息.如下: 在此我将所有信息定义在一个类中 ...
- Google advertiser api开发概述——最佳做法&建议
最佳做法 本指南介绍了一些最佳做法,您可以运用它们来优化 AdWords API 应用的效率和性能. 日常维护 为确保您的应用不间断运行,可采取以下做法: 确保 AdWords API 中心中的开发者 ...
- 循序渐进学.Net Core Web Api开发系列【14】:异常处理
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍异 ...
- ASP.NET Core Web API 开发-RESTful API实现
ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...
- 基于.Net Framework 4.0 Web API开发(2):ASP.NET Web APIs 参数传递方式详解
概述: ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.调用API过程中参数的传递是必须的,本节就来谈谈 ...
- Rest API 开发 学习笔记(转)
Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...
- Windows下mock环境搭建-加速项目Api开发
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 公司进行技术部拆分,以项目制作为新的开发模式,前端+移动端+后端,于是加速Api开发变得很有必要,准 ...
- Web API开发实例——对产品Product进行增删改查
1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...
随机推荐
- Frogger(最短路)
Frogger(poj2253) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31854 Accepted: 1026 ...
- mysql 创建外键引用时眼瞎了,然而mysql 报的错也是认人摸不着头脑
问题描述: 在创建外键约束时mysql 报 Create table 'tempdb/student' with foreign key constraint failed. There is no ...
- 观点:哪些人适合做FPGA开发?(转)
原文:http://xilinx.eetrend.com/blog/561 FPGA目前非常火,各个高校也开了FPGA的课程,但是FPGA并不是每个人都适合,FPGA讲究的是一个入道,入什么道,入电子 ...
- Json之语法,格式
JSON 文本格式在语法上与创建 JavaScript 对象的代码相同.由于这种相似性,无需解析器,JavaScript 程序能够使用内建的 eval() 函数,用 JSON 数据来生成原生的 Jav ...
- .net 反编译利器 dnspy
Binaries Latest release: https://github.com/0xd4d/dnSpy/releases Latest build (possibly unstable): h ...
- HDU1232 畅通工程 (并查集模板题)
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 利用Inotify和Rsync将webproject文件自己主动同步到多台应用server
背景:须要搭建一套跟线上一模一样的环境,用来预公布,这是当中的web分发的一个小模块的实现过程. 1 工具以及环境简单介绍 1.1,Inotify工具 Inotify,它是一个内核用于通知用户空间程序 ...
- java学习笔记day06---匿名内部类
1.匿名内部类:其实就是内部类的简化形式,它所体现的就是一个类或者接口的子类对象.前提: 内部类必须继承或实现外部类或接口. 格式: new 父类&接口(){}; 其实就是 ...
- CSS入门教程——定位(positon)
CSS入门教程——定位(positon) CSS定位在网页布局中是起着决定性作用. 定位 CSS的定位功能是很强大的,利用它你可以做出各种各样的网页布局.本节就介绍一些CSS常用的定位语句. 1. ...
- hibernate某些版本(4.3)下报错 NoSuchMethodError: javax.persistence.Table.indexes()
其实本来没啥大问题,但到网上查的时候发现了一些误人子弟的说法,所以还是记下来吧. 现象: hibernate从低版本升级到某一个版本时(我们是升到4.3.10)时,在程序启动时会报错: java.la ...