[英]How can I make a request to aws cloudsearch using the AWS iOS SDK?
I have a client that runs their search functionality on their website through cloudsearch. 我有一个客户端通过cloudsearch在他们的网站上运行他们的搜索功能。 I have been going through the documentation for days, and haven't been able to make a successful search request.
我已经阅读了几天的文档,并且无法成功搜索请求。 I created an NSMutableRequest object, and am running that request through the AWSSignature method [signature interceptRequest:request];
我创建了一个NSMutableRequest对象,并通过AWSSignature方法[signature interceptRequest:request]运行该请求; but my task.result is coming back (null).
但我的task.result回来了(null)。
Here is my code: 这是我的代码:
AWSTask *task = [signature interceptRequest:request];
[task continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {
NSLog(@"task.result fromSearch:%@", task.result);
NSData *responseData = task.result;
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"newStr:%@", newStr);
NSLog(@"task.error:%@", task.error);
return nil;
}];
Am I on the right track, or is there a better way to do this through the aws iOS sdk? 我是在正确的轨道上,还是有更好的方法通过aws iOS sdk做到这一点?
To put a little more flesh on the bones of Robert's comment, I did it with some help from AFNetworking like so: 为了给罗伯特评论的骨子多一点点,我在AFNetworking的帮助下做到了这样:
#import <AWSCore/AWSSignature.h>
#import <AWSCore/AWSService.h>
#import <AWSCore/AWSCategory.h>
#import <AWSCore/AWSCredentialsProvider.h>
#import <AWSCore/AWSTask.h>
#import "AFNetworking.h"
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = [[NSOperationQueue alloc] init];
}
- (void)performSearch {
AWSAnonymousCredentialsProvider* credentialsProvider = [[AWSAnonymousCredentialsProvider alloc] init];
NSString* searchHost = @"<CloudSearchEndPoint>.eu-west-1.cloudsearch.amazonaws.com";
NSString* query = [self.searchTerms aws_stringWithURLEncoding];
NSURL* searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/2013-01-01/search?q=%@", searchHost, query]];
AWSEndpoint* endpoint = [[AWSEndpoint alloc] initWithURL:searchURL];
AWSSignatureV4Signer* signer = [[AWSSignatureV4Signer alloc] initWithCredentialsProvider:credentialsProvider endpoint:endpoint];
NSMutableURLRequest* mutableRequest = [[NSMutableURLRequest alloc] initWithURL:searchURL];
AWSTask* task = [signer interceptRequest:mutableRequest];
[task continueWithBlock:^id(AWSTask* _Nonnull t) {
if (t.error) {
NSLog(@"Error: %@", t.error);
} else if (t.completed) {
NSLog(@"Result is %@", t.result);
}
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:mutableRequest success:^(NSURLRequest* request, NSHTTPURLResponse* response, id JSON) {
NSLog(@"Success fetching results!");
if (JSON) {
NSDictionary* hitsContainer = [JSON objectForKey:@"hits"];
NSArray* hits = [hitsContainer objectForKey:@"hit"];
NSMutableArray* allResults = [[NSMutableArray alloc] initWithCapacity:hits.count];
for (NSDictionary* hit in hits) {
NSDictionary* fields = [hit objectForKey:@"fields"];
[allResults addObject:fields];
}
self.searchResults = allResults;
[self.tableView reloadData];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failure fetching search results :-( %@", error);
}
];
[self.queue addOperation:operation];
return nil;
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.