tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2014 Google Inc. |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 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 | |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 28 | #import "ARDLogging.h" |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 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) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 40 | ARDLog(@"Error parsing JSON: %@", error.localizedDescription); |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 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) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 50 | ARDLog(@"Error parsing JSON: %@", error.localizedDescription); |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 51 | } |
| 52 | return dict; |
| 53 | } |
| 54 | |
| 55 | @end |
| 56 | |
| 57 | @implementation NSURLConnection (ARDUtilities) |
| 58 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 59 | + (void)sendAsyncRequest:(NSURLRequest *)request |
| 60 | completionHandler:(void (^)(NSURLResponse *response, |
| 61 | NSData *data, |
| 62 | NSError *error))completionHandler { |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 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) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 88 | ARDLog(@"Error posting data: %@", error.localizedDescription); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 99 | ARDLog(@"Received bad response: %@", serverResponse); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 100 | if (completionHandler) { |
| 101 | completionHandler(NO, data); |
| 102 | } |
| 103 | return; |
| 104 | } |
| 105 | if (completionHandler) { |
| 106 | completionHandler(YES, data); |
| 107 | } |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 108 | }]; |
| 109 | } |
| 110 | |
| 111 | @end |