blob: 066ee0294c65f9589365059bd93051d2abb9027e [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
Zeke Chin2d3b7e22015-07-14 12:55:44 -070028#import "ARDLogging.h"
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000029#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 Chin2d3b7e22015-07-14 12:55:44 -070040 ARDLog(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000041 }
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 Chin2d3b7e22015-07-14 12:55:44 -070050 ARDLog(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000051 }
52 return dict;
53}
54
55@end
56
57@implementation NSURLConnection (ARDUtilities)
58
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000059+ (void)sendAsyncRequest:(NSURLRequest *)request
60 completionHandler:(void (^)(NSURLResponse *response,
61 NSData *data,
62 NSError *error))completionHandler {
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000063 // 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.org87776a82014-12-09 19:32:35 +000069 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 Chin2d3b7e22015-07-14 12:55:44 -070088 ARDLog(@"Error posting data: %@", error.localizedDescription);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000089 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 Chin2d3b7e22015-07-14 12:55:44 -070099 ARDLog(@"Received bad response: %@", serverResponse);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000100 if (completionHandler) {
101 completionHandler(NO, data);
102 }
103 return;
104 }
105 if (completionHandler) {
106 completionHandler(YES, data);
107 }
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +0000108 }];
109}
110
111@end