Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new Reporter tool’s endpoints to download reports #287

Merged
merged 1 commit into from
Jan 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Classes/ReportDownloadOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down