Permalink
Browse files

Merge pull request #287 from danielctull-forks/issue/286

Use the new Reporter tool’s endpoints to download reports
  • Loading branch information...
omz committed Jan 4, 2017
2 parents 16ddf3d + 8b74ce9 commit ac17736ada11580460e9ce7c0f5c8a43d3a30ba5
Showing with 20 additions and 6 deletions.
  1. +20 −6 Classes/ReportDownloadOperation.m
@@ -159,22 +159,36 @@ - (void)main
NSString *escapedUsername = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)username, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8) autorelease];
NSString *escapedPassword = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)password, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8) autorelease];
- NSString *reportDownloadBodyString = [NSString stringWithFormat:@"USERNAME=%@&PASSWORD=%@&VNDNUMBER=%@&TYPEOFREPORT=%@&DATETYPE=%@&REPORTTYPE=%@&REPORTDATE=%@",
- escapedUsername, escapedPassword, vendorID, @"Sales", dateType, @"Summary", reportDateString];
+ NSURL *URL = [NSURL URLWithString:@"https://reportingitc-reporter.apple.com/reportservice/sales/v1"];
+
+ NSString *command = [NSString stringWithFormat:@"[p=Reporter.properties, Sales.getReport, %@,Sales,Summary,%@,%@]", vendorID, dateType, reportDateString];
+ NSDictionary *body = @{
+ @"userid" : escapedUsername,
+ @"password" : escapedPassword,
+ @"version" : @"2.0",
+ @"mode" : @"Normal",
+ @"queryInput" : command
+ };
+
+ NSData *bodyData = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:NULL];
+ NSString *bodyString = [[[NSString alloc] initWithData:bodyData encoding:NSUTF8StringEncoding] autorelease];
+
+ NSString *reportDownloadBodyString = [NSString stringWithFormat:@"jsonRequest=%@", bodyString];
NSData *reportDownloadBodyData = [reportDownloadBodyString dataUsingEncoding:NSUTF8StringEncoding];
- NSMutableURLRequest *reportDownloadRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://reportingitc.apple.com/autoingestion.tft"]];
+ NSMutableURLRequest *reportDownloadRequest = [NSMutableURLRequest requestWithURL:URL];
[reportDownloadRequest setHTTPMethod:@"POST"];
[reportDownloadRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[reportDownloadRequest setValue:@"java/1.6.0_26" forHTTPHeaderField:@"User-Agent"];
+ [reportDownloadRequest setValue:@"text/html,image/gif,image/jpeg; q=.2, */*; q=.2" forHTTPHeaderField:@"Accept"];
[reportDownloadRequest setHTTPBody:reportDownloadBodyData];
NSHTTPURLResponse *response = nil;
NSData *reportData = [NSURLConnection sendSynchronousRequest:reportDownloadRequest returningResponse:&response error:NULL];
- NSString *errorMessage = [[response allHeaderFields] objectForKey:@"Errormsg"];
- if (errorMessage) {
- NSLog(@" %@", errorMessage);
+ if (response.statusCode != 200) {
+ NSString *responseDataString = [[[NSString alloc] initWithData:reportData encoding:NSUTF8StringEncoding] autorelease];
+ NSLog(@"Failed to retrieve %@ sales report for %@ with response: \n%@", dateType, reportDateString, responseDataString);
} else if (reportData) {
NSString *originalFilename = [[response allHeaderFields] objectForKey:@"Filename"];
NSData *inflatedReportData = [reportData gzipInflate];

0 comments on commit ac17736

Please sign in to comment.