| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #import "ARDLogging.h" | |
| 29 #import "ARDUtilities.h" | |
| 30 | |
| 31 @implementation NSDictionary (ARDUtilites) | |
| 32 | |
| 33 + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { | |
| 34 NSParameterAssert(jsonString.length > 0); | |
| 35 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; | |
| 36 NSError *error = nil; | |
| 37 NSDictionary *dict = | |
| 38 [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | |
| 39 if (error) { | |
| 40 ARDLog(@"Error parsing JSON: %@", error.localizedDescription); | |
| 41 } | |
| 42 return dict; | |
| 43 } | |
| 44 | |
| 45 + (NSDictionary *)dictionaryWithJSONData:(NSData *)jsonData { | |
| 46 NSError *error = nil; | |
| 47 NSDictionary *dict = | |
| 48 [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; | |
| 49 if (error) { | |
| 50 ARDLog(@"Error parsing JSON: %@", error.localizedDescription); | |
| 51 } | |
| 52 return dict; | |
| 53 } | |
| 54 | |
| 55 @end | |
| 56 | |
| 57 @implementation NSURLConnection (ARDUtilities) | |
| 58 | |
| 59 + (void)sendAsyncRequest:(NSURLRequest *)request | |
| 60 completionHandler:(void (^)(NSURLResponse *response, | |
| 61 NSData *data, | |
| 62 NSError *error))completionHandler { | |
| 63 // Kick off an async request which will call back on main thread. | |
| 64 [NSURLConnection sendAsynchronousRequest:request | |
| 65 queue:[NSOperationQueue mainQueue] | |
| 66 completionHandler:^(NSURLResponse *response, | |
| 67 NSData *data, | |
| 68 NSError *error) { | |
| 69 if (completionHandler) { | |
| 70 completionHandler(response, data, error); | |
| 71 } | |
| 72 }]; | |
| 73 } | |
| 74 | |
| 75 // Posts data to the specified URL. | |
| 76 + (void)sendAsyncPostToURL:(NSURL *)url | |
| 77 withData:(NSData *)data | |
| 78 completionHandler:(void (^)(BOOL succeeded, | |
| 79 NSData *data))completionHandler { | |
| 80 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
| 81 request.HTTPMethod = @"POST"; | |
| 82 request.HTTPBody = data; | |
| 83 [[self class] sendAsyncRequest:request | |
| 84 completionHandler:^(NSURLResponse *response, | |
| 85 NSData *data, | |
| 86 NSError *error) { | |
| 87 if (error) { | |
| 88 ARDLog(@"Error posting data: %@", error.localizedDescription); | |
| 89 if (completionHandler) { | |
| 90 completionHandler(NO, data); | |
| 91 } | |
| 92 return; | |
| 93 } | |
| 94 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; | |
| 95 if (httpResponse.statusCode != 200) { | |
| 96 NSString *serverResponse = data.length > 0 ? | |
| 97 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : | |
| 98 nil; | |
| 99 ARDLog(@"Received bad response: %@", serverResponse); | |
| 100 if (completionHandler) { | |
| 101 completionHandler(NO, data); | |
| 102 } | |
| 103 return; | |
| 104 } | |
| 105 if (completionHandler) { | |
| 106 completionHandler(YES, data); | |
| 107 } | |
| 108 }]; | |
| 109 } | |
| 110 | |
| 111 @end | |
| OLD | NEW |