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 ...
随机推荐
- [翻译]Orchard文档-命令行基架
Code generation是一个用于创建附加文件和扩展的自动化任务Orchard模块.这个功能对开发者创建控制器[Controllers].数据迁移类[Data Migration].模块[Mod ...
- SqlCommand类
一.常用属性 CommandText 获取或设置要对数据源执行的 Transact-SQL 语句.表名或存储过程. CommandTimeout 获取或设置在终止执行命令的尝试并生成错误之前的等待时间 ...
- 【转】ubuntu下putty的复制粘贴 -- 不错
原文网址:http://www.nwber.com/?p=165 今天在ubutnu下想用putty玩玩,突然发现在windows里直接点击右键的复制居然不管用了,调设置也没有用.这可麻烦了,要是手动 ...
- atoi 和itoa用法
1.itoa 在linux下没有itoa这个函数 原型:char *itoa(int value,char *string,int radix) 用法 ...
- json中换行问题
json中不能存在换行,但可以进行替换后给服务器 function(text_info) { text_info=text_info.replace(/\r/gm,"<br\>& ...
- 8个华丽的HTML5相册动画欣赏
HTML5的图片动画非常丰富,我们也在网站上分享过很多关于HTML5的图片动画.相册在网络中也十分常见,本文我们要分享一些比较华丽的jQuery/HTML5相册动画,希望大家喜欢. 1.HTML5 3 ...
- The Java™ Tutorials下载地址
1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...
- UIView 弹出动画
// 展开动画 - (void)beginAnimations { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView begi ...
- Oracle Enterprise linux 7 安装Oracle11gR2
一.修改主机名和IP地址: [root@localhost Desktop]# cat /etc/hosts127.0.0.1 localhost.localdomain localhost 192. ...
- 关于继承扩展ASP.NET控件(以Textbox为例)
以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展. using System; using System.Collections.Generic; using System.Linq ...