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