-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseNetworking.m
More file actions
95 lines (85 loc) · 3.38 KB
/
Copy pathBaseNetworking.m
File metadata and controls
95 lines (85 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// BaseNetworking.m
//
// Created by Sardorbek on 12/13/15.
// Copyright © 2015 Sardorbek Ruzmatov. All rights reserved.
//
#import "BaseNetworking.h"
@implementation BaseNetworking
-(void)fetchDataAtURLString:(NSString *)urlString withBlock:(BaseNetworkingResponse)block
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error)
{
NSLog(@"%@, error: %@", [self class], [error localizedDescription]);
block(nil, response, error);
return;
}
NSLog(@"%@, response ok", [self class]);
block(data, response, nil);
}];
[postDataTask resume];
}
-(void)fetchContentAtURLString:(NSString *)urlString withBlock:(BaseNetworkingResponse)block
{
[self fetchDataAtURLString:urlString withBlock:^(id responseData, NSURLResponse *responseObject, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseObject;
NSDictionary *headers = [httpResponse allHeaderFields];
if (httpResponse.statusCode == 404)
{
block(nil, responseObject, error);
return;
}
if (responseData)
{
/**
* Filter response types on the basis of 'content-type' header
*/
NSString *contentType = headers[@"Content-Type"];
if ([contentType containsString:@"text/xml"])
{
block([[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding], nil, nil);
}
else if ([contentType containsString:@"text/html"])
{
block([[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding], nil, nil);
}
else if ([contentType containsString:@"application/json"])
{
block([NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil], nil, nil);
}
else if ([contentType containsString:@"image/jpeg"])
{
block(responseData, responseObject, nil);
}
}
else
{
block(nil, responseObject, error);
}
}];
}
-(void)postContent:(id)content atURLString:(NSString *)urlString withBlock:(BaseNetworkingResponse)block
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:content options:0 error:nil];
request.HTTPBody = postData;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error)
{
NSLog(@"%@, error: %@", [self class], [error localizedDescription]);
block(nil, response, error);
return;
}
NSLog(@"%@, response ok", [self class]);
block([NSJSONSerialization JSONObjectWithData:data options:0 error:nil], nil, nil);
}];
[postDataTask resume];
}
@end