您的位置:首页 > 其它

Amzon MWS API开发之 请求报告

2014-08-06 19:48 197 查看
时间一晃而过又过了两周,博客园更新的速度确实有点慢,今天我要分享的是对请求报告的调用。

    在文档中,相信大家也看了下面这个流程图吧?

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(2014, 7, 15, 0, 0, 0);
request.AvailableToDate = new DateTime(2014, 7, 31, 0, 0, 0);

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";
}

}


View Code
    大家要知道报告有一个特别之处,不是你想要什么时候的数据,他就会给你什么时候的数据,亚马逊服务器会根据一段时间生成,如果没有生成,你也只能获取之前生成了的报告数据。正所谓,不是你想要,我就给你,你得看我的心情。呵呵。

根据调用以上代码就能下载到报告了,能生成一个个你需要的文件。

    当然我们可能需要的还不止这样,这样只给我一些文本文件,岂能满足于我做开发?只有把这些数据导入到我的数据库中,我才能心安理得,酣睡长眠呢。

    接下来,我们要做的就是解析这些文本文件了,当然,你怎么解析都行,看你自己了。为了暂时想不出怎么解析或者说没怎么研究过的朋友,我献上我的小小法子。

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天,很多东西也不是很懂,不过希望感兴趣的朋友,大家一起交流学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: