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 ...
随机推荐
- Discuz!X2.5论坛在IIS和Apache环境配置实现伪静态
最近在研究自己的网站,然后把这文章分享出来,让不清楚怎么设置的童鞋参考,高手可以飘过~~~ URL 静态化是一个有利于搜索引擎的设置,通过 URL 静态化,达到原来是动态的 PHP 页面转换为静态化的 ...
- 有意思的GacUI
所有方法,无论是你写还是工具来codegen还是用宏,最终都指向把这些名字和对应的指针存在一个map里.C++是不提供这个功能的,我也没仔细研究过qt怎么做,不过我在我自己的gacui里面实现了类似的 ...
- SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型
原文:SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测 ...
- [PowerShell] Backup Folder and Files Across Network
## This Script is used to backup folder/files and delete the old backup files. ## Author: Stefanie # ...
- rpm包制作
ubuntu下先下载sudo apt-get install rpm就行了. 然后测试下rpm和rpmbuild命令都是存在的.好了,OK. rpm安装包的制作有严格的自定义的路径,这个路径是在/us ...
- Climbing Stairs 解答
Question You are climbing a stair case. It takes n steps to reach to the top. Each time you can eith ...
- poj 2299 Ultra-QuickSort(归并排序或是bit 树+离散化皆可)
题意:给一个数组,计算需要的冒泡排序的次数,元素个数很大,不能用n^2的冒泡排序计算. 解析:这题实际上就是求逆序对的个数,可以用归并排序的方法,我这里用另一种方法写,bit树+离散化.由于元素的值可 ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- OpenStack 部署总结之:在CentOS 6.5上使用RDO单机安装icehouse(Ml2+GRE)
本文主要介绍怎样在CentOS6.5上通过RDO来安装icehouse,因为安装的过程中涉及的软件较多,以及依赖关系比較复杂,建议使用一个全新的操作系统来进行安装. 安装步骤详细例如以下 (1)安装操 ...
- 【HTML+CSS】浅谈:相对定位与绝对定位
相对定位和绝对定位 ·定位标签:position ·包括属性:relative(相对) absolute(绝对) 1.position:relative; 假设对一个元素进行相对定位.首先它将出如今 ...