2

I'm using the camfind api, I post a request where I send the api an image and get a token, and then I can use the token to get what the image is, but this takes two steps, a post and a get request. I want to combine them, so the get request auto fires right when the server responds to the post request. Here is the code :

I specify a value for the token at the top

 NSString *tokenValue;

the request method, here an image is selected and sent to the server, the server sends a token response back in this request

- (void *) requestMethod: (UIImage *)imageToConvert{

    NSString *temp = @"saved";
    NSLog(@"%@", temp);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = [paths objectAtIndex:0];
    NSString *imagePath = [documentDirectoryPath stringByAppendingPathComponent:@"tmp_image.jpg"];
    NSURL *imageURL = [NSURL fileURLWithPath:imagePath];

    NSData *imageData = UIImageJPEGRepresentation(imageToConvert , 1.0);
    [imageData writeToURL:imageURL atomically:YES];
    //    NSString *base64image = [NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
    NSString *base64image = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    // These code snippets use an open-source library.
    //  NSString *baseboy =  [base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // NSURL *urlimage_request = [NSURL URLWithString:[base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSDictionary *headers = @{@"X-Mashape-Key": @"********"};
    NSDictionary *parameters = @{@"focus[x]": @"480", @"focus[y]": @"640", @"image_request[altitude]": @"27.912109375", @"image_request[image]": imageURL, @"image_request[language]": @"en", @"image_request[latitude]": @"35.8714220766008", @"image_request[locale]": @"en_US", @"image_request[longitude]": @"14.3583203002251"};

    UNIUrlConnection *asyncConnection = [[UNIRest post:^(UNISimpleRequest *request) {
        [request setUrl:@"https://camfind.p.mashape.com/image_requests"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
        NSInteger code = response.code;
        NSDictionary *responseHeaders = response.headers;
        UNIJsonNode *body = response.body;
        NSData *rawBody = response.rawBody;
        NSString *token = response.description;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:nil];
        NSLog(@"Response status: %ld\n%@", (long) response.code, json);
        for(NSString *key in [json allValues])
        {


            tokenValue = [json valueForKey: @"token" ];
            NSString *two = [json valueForKey: @"token" ]; // assuming the value is indeed a string
            NSLog(@"Token :%@", two);
            NSString *one = @"https://camfind.p.mashape.com/image_responses" ;
            NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
            NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, two];


            // NSString *responseURL = [one stringByAppendingString:two];
            NSLog(@"response URL %@", responseString);
            //            UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
            //                [request setUrl:responseString];
            //                [request setHeaders:headers];
            //            }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
            //                NSInteger code = response.code;
            //                NSDictionary *responseHeaders = response.headers;
            //                UNIJsonNode *body = response.body;
            //                NSData *rawBody = response.rawBody;
            //                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
            //                                                                     options:kNilOptions
            //                                                                       error:nil];
            //                NSLog(@"Response status: %ld\n%@", (long) response.code, json);
            //
            //
            //                //              NSLog(@"didfinishLoadingbody%@",rawBody);
            //                //        NSLog(@"didfinishLoadingbody%@",body);
            //                //
            //               // NSLog(@"didfinishLoading responseheader%@",responseHeaders);
            //                //           NSLog(@"didfinishLoading tok%@",token);
            //            }];







        }

        //              NSLog(@"didfinishLoadingbody%@",rawBody);
        //        NSLog(@"didfinishLoadingbody%@",body);
        //
        // NSLog(@"didfinishLoading responseheader%@",responseHeaders);
        //           NSLog(@"didfinishLoading tok%@",token);

    }];
    return (__bridge void *)(self);
}

the response method below uses the token from the request above to print out what the image is

- (void *) responseMethod {
    // These code snippets use an open-source library.
    NSString *one = @"https://camfind.p.mashape.com/image_responses" ;

    NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, tokenValue];

    NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
    UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
        [request setUrl:responseString];
        [request setHeaders:headers];
    }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
        NSInteger code = response.code;
        NSDictionary *responseHeaders = response.headers;
        UNIJsonNode *body = response.body;
        NSData *rawBody = response.rawBody;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:nil];
        NSLog(@"Response status: %ld\n%@", (long) response.code, json);


        //              NSLog(@"didfinishLoadingbody%@",rawBody);
        //        NSLog(@"didfinishLoadingbody%@",body);
        //
        NSLog(@"didfinishLoading responseheader%@",responseHeaders);
        //           NSLog(@"didfinishLoading tok%@",token);
    }];
    return (__bridge void *)(self);
}

rest of the vc other stuff

 -(void)loadView {
        [super loadView];


    }

    - (void)viewDidLoad{
        [super viewDidLoad];
    }

    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
    }

    -(void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];

    }
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];

    }

I don't know how to make the response method fire right away when the server replies, I don't know objc, im using this in a swift project

@end
Kashish Goel
  • 303
  • 3
  • 15
  • You included a lot of code, but I don't see where your `responseMethod` or `requestMethod` are being called in your view controller? Also, you're saying you are using swift, but this code is Objective-C. – Michael Dautermann Aug 29 '15 at 05:03
  • Hey sorry for the confusion, I broke the code up see my edits above. the request method sends a picture and gets a token, the response one takes the token and prints what the object is. And yeah I'm using a bridging header to use this code in swift. But I need a solution in objc anyway, – Kashish Goel Aug 29 '15 at 14:03
  • I basically want the response method to execute as soon as a token is received – Kashish Goel Aug 29 '15 at 14:04

0 Answers0