blob: 39010dafb01dc7d406a33924ed120dd10511e73a [file] [log] [blame]
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +00004 *
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
tkchinc3f46a92015-07-23 12:50:55 -070030#import "RTCLogging.h"
31
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000032@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) {
tkchinc3f46a92015-07-23 12:50:55 -070041 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000042 }
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) {
tkchinc3f46a92015-07-23 12:50:55 -070051 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000052 }
53 return dict;
54}
55
56@end
57
58@implementation NSURLConnection (ARDUtilities)
59
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000060+ (void)sendAsyncRequest:(NSURLRequest *)request
61 completionHandler:(void (^)(NSURLResponse *response,
62 NSData *data,
63 NSError *error))completionHandler {
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000064 // 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.org87776a82014-12-09 19:32:35 +000070 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) {
tkchinc3f46a92015-07-23 12:50:55 -070089 RTCLogError(@"Error posting data: %@", error.localizedDescription);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000090 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;
tkchinc3f46a92015-07-23 12:50:55 -0700100 RTCLogError(@"Received bad response: %@", serverResponse);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000101 if (completionHandler) {
102 completionHandler(NO, data);
103 }
104 return;
105 }
106 if (completionHandler) {
107 completionHandler(YES, data);
108 }
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +0000109 }];
110}
111
112@end