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 | |
| 28 | #import "ARDUtilities.h" |
| 29 | |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame^] | 30 | #import "RTCLogging.h" |
| 31 | |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 32 | @implementation NSDictionary (ARDUtilites) |
| 33 | |
| 34 | + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { |
| 35 | NSParameterAssert(jsonString.length > 0); |
| 36 | NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; |
| 37 | NSError *error = nil; |
| 38 | NSDictionary *dict = |
| 39 | [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; |
| 40 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame^] | 41 | RTCLogError(@"Error parsing JSON: %@", error.localizedDescription); |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 42 | } |
| 43 | return dict; |
| 44 | } |
| 45 | |
| 46 | + (NSDictionary *)dictionaryWithJSONData:(NSData *)jsonData { |
| 47 | NSError *error = nil; |
| 48 | NSDictionary *dict = |
| 49 | [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; |
| 50 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame^] | 51 | RTCLogError(@"Error parsing JSON: %@", error.localizedDescription); |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 52 | } |
| 53 | return dict; |
| 54 | } |
| 55 | |
| 56 | @end |
| 57 | |
| 58 | @implementation NSURLConnection (ARDUtilities) |
| 59 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 60 | + (void)sendAsyncRequest:(NSURLRequest *)request |
| 61 | completionHandler:(void (^)(NSURLResponse *response, |
| 62 | NSData *data, |
| 63 | NSError *error))completionHandler { |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 64 | // Kick off an async request which will call back on main thread. |
| 65 | [NSURLConnection sendAsynchronousRequest:request |
| 66 | queue:[NSOperationQueue mainQueue] |
| 67 | completionHandler:^(NSURLResponse *response, |
| 68 | NSData *data, |
| 69 | NSError *error) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 70 | if (completionHandler) { |
| 71 | completionHandler(response, data, error); |
| 72 | } |
| 73 | }]; |
| 74 | } |
| 75 | |
| 76 | // Posts data to the specified URL. |
| 77 | + (void)sendAsyncPostToURL:(NSURL *)url |
| 78 | withData:(NSData *)data |
| 79 | completionHandler:(void (^)(BOOL succeeded, |
| 80 | NSData *data))completionHandler { |
| 81 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 82 | request.HTTPMethod = @"POST"; |
| 83 | request.HTTPBody = data; |
| 84 | [[self class] sendAsyncRequest:request |
| 85 | completionHandler:^(NSURLResponse *response, |
| 86 | NSData *data, |
| 87 | NSError *error) { |
| 88 | if (error) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame^] | 89 | RTCLogError(@"Error posting data: %@", error.localizedDescription); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 90 | if (completionHandler) { |
| 91 | completionHandler(NO, data); |
| 92 | } |
| 93 | return; |
| 94 | } |
| 95 | NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; |
| 96 | if (httpResponse.statusCode != 200) { |
| 97 | NSString *serverResponse = data.length > 0 ? |
| 98 | [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : |
| 99 | nil; |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame^] | 100 | RTCLogError(@"Received bad response: %@", serverResponse); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 101 | if (completionHandler) { |
| 102 | completionHandler(NO, data); |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | if (completionHandler) { |
| 107 | completionHandler(YES, data); |
| 108 | } |
tkchin@webrtc.org | 3e9ad26 | 2014-11-27 00:52:38 +0000 | [diff] [blame] | 109 | }]; |
| 110 | } |
| 111 | |
| 112 | @end |