blob: 257b6a6a5c9e6ed6d61b5654990ac50a1a269b92 [file] [log] [blame]
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +00009 */
10
11#import "ARDUtilities.h"
12
tkchinc3f46a92015-07-23 12:50:55 -070013#import "RTCLogging.h"
14
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000015@implementation NSDictionary (ARDUtilites)
16
17+ (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString {
18 NSParameterAssert(jsonString.length > 0);
19 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
20 NSError *error = nil;
21 NSDictionary *dict =
22 [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
23 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -070024 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000025 }
26 return dict;
27}
28
29+ (NSDictionary *)dictionaryWithJSONData:(NSData *)jsonData {
30 NSError *error = nil;
31 NSDictionary *dict =
32 [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
33 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -070034 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription);
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000035 }
36 return dict;
37}
38
39@end
40
41@implementation NSURLConnection (ARDUtilities)
42
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000043+ (void)sendAsyncRequest:(NSURLRequest *)request
44 completionHandler:(void (^)(NSURLResponse *response,
45 NSData *data,
46 NSError *error))completionHandler {
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000047 // Kick off an async request which will call back on main thread.
48 [NSURLConnection sendAsynchronousRequest:request
49 queue:[NSOperationQueue mainQueue]
50 completionHandler:^(NSURLResponse *response,
51 NSData *data,
52 NSError *error) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000053 if (completionHandler) {
54 completionHandler(response, data, error);
55 }
56 }];
57}
58
59// Posts data to the specified URL.
60+ (void)sendAsyncPostToURL:(NSURL *)url
61 withData:(NSData *)data
62 completionHandler:(void (^)(BOOL succeeded,
63 NSData *data))completionHandler {
64 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
65 request.HTTPMethod = @"POST";
66 request.HTTPBody = data;
67 [[self class] sendAsyncRequest:request
68 completionHandler:^(NSURLResponse *response,
69 NSData *data,
70 NSError *error) {
71 if (error) {
tkchinc3f46a92015-07-23 12:50:55 -070072 RTCLogError(@"Error posting data: %@", error.localizedDescription);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000073 if (completionHandler) {
74 completionHandler(NO, data);
75 }
76 return;
77 }
78 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
79 if (httpResponse.statusCode != 200) {
80 NSString *serverResponse = data.length > 0 ?
81 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] :
82 nil;
tkchinc3f46a92015-07-23 12:50:55 -070083 RTCLogError(@"Received bad response: %@", serverResponse);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000084 if (completionHandler) {
85 completionHandler(NO, data);
86 }
87 return;
88 }
89 if (completionHandler) {
90 completionHandler(YES, data);
91 }
tkchin@webrtc.org3e9ad262014-11-27 00:52:38 +000092 }];
93}
94
95@end