I'm trying to use Diffbot to parse some URLs into the relevant article portion. They have an "Article API" that allows you to submit one link at a time and receive it back, but for speed I'd prefer to use the Batch API which basically allows you to submit a bunch of Article API requests into one big request and get one big response, instead of one at a time.
Here's what the Batch API described in their documentation (that is oddly behind a login wall):
I'm submitting to the Article API as so:
NSURLRequest *request = [[AFDiffbotClient sharedClient]
requestWithMethod:@"GET"
path:[NSString stringWithFormat:@"article?token=MYTOKEN&fields=url,text,title&url=%@", URL]
parameters:nil];
And it's working perfectly. AFDiffbotClient
is a singleton combined with AFNetworking
to help me make requests easier, and the URL parameter is just the URL of the article I'm looking at. (Perhaps I could be doing that without creating the URL manually, bonus points if anyone could offer tips on that.)
However, with the Batch API, you're supposed to submit (POST) a bunch of these requests as a JSON array. I'm confused how I would go about doing this.
EDIT: I've worked some more on it, and made some progress, but I'm getting a 400 error back. I can't figure out what I'm doing wrong, but I must be along the right path. I'm passing parameters in the POST request with my token and my JSON array, but it still won't work.
[AFDiffbotClient sharedClient].operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
NSMutableArray *DiffbotRequests = [[NSMutableArray alloc] init];
for (NSDictionary *URLAndID in URLsAndIDs) {
NSString *articleURL = [URLAndID objectForKey:@"URL"];
NSDictionary *request = @{@"token": @"TOKEN",
@"fields": @"text,title,url",
@"url": articleURL};
[DiffbotRequests addObject:request];
}
NSError *error;
NSData *DiffbotRequestsJSONData = [NSJSONSerialization dataWithJSONObject:DiffbotRequests options:kNilOptions error:&error];
NSString *DiffbotRequestsJSONString = [[NSString alloc] initWithData:DiffbotRequestsJSONData encoding:NSUTF8StringEncoding];
NSDictionary *parameters = @{@"token": @"TOKEN",
@"batch": DiffbotRequestsJSONString};
[[AFDiffbotClient sharedClient] getPath:@"batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
And here's the response I get back:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0xc2ee4d0 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey= { URL:
And after that it's just all the URLs I submitted.
EDIT 2: Added an image of the API above.
EDIT 3: Current, unworking code:
[AFDiffbotClient sharedClient].operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
NSMutableArray *DiffbotRequests = [[NSMutableArray alloc] init];
for (NSDictionary *URLAndID in URLsAndIDs) {
NSString *articleURL = [URLAndID objectForKey:@"URL"];
NSDictionary *request = @{@"token": @"TOKEN",
@"fields": @"text,title,url",
@"url": articleURL};
[DiffbotRequests addObject:request];
}
NSError *error;
NSData *DiffbotRequestsJSONData = [NSJSONSerialization dataWithJSONObject:DiffbotRequests options:NSJSONWritingPrettyPrinted error:&error];
NSString *DiffbotRequestsJSONString = [[NSString alloc] initWithData:DiffbotRequestsJSONData encoding:NSUTF8StringEncoding];
NSDictionary *parameters = @{@"token": @"TOKEN",
@"batch": DiffbotRequestsJSONString};
[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];
[[AFDiffbotClient sharedClient] postPath:@"batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];