-
Notifications
You must be signed in to change notification settings - Fork 19
/
WBHTTPClient.m
119 lines (98 loc) · 3.56 KB
/
WBHTTPClient.m
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// WBHTTPClient.m
// SinaWeibo
//
// Created by 东方 刘 on 12-3-12.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "WBHTTPClient.h"
#import "JSON.h"
#define kWBbaseURL @"https://api.weibo.com/"
@implementation WBHTTPClient
+ (id)sharedClient
{
static WBHTTPClient *_sharedClient = nil;
static dispatch_once_t oncePredicate = 0;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kWBbaseURL]];
});
return _sharedClient;
}
#pragma mark - Custom requests
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
//
// // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
// [self setDefaultHeader:@"Accept" value:@"application/json"];
//
// // X-Gowalla-API-Key HTTP Header; see http://api.gowalla.com/api/docs
// [self setDefaultHeader:@"X-Gowalla-API-Key" value:kAFGowallaClientID];
//
// // X-Gowalla-API-Version HTTP Header; see http://api.gowalla.com/api/docs
// [self setDefaultHeader:@"X-Gowalla-API-Version" value:@"1"];
//
// // X-UDID HTTP Header
// [self setDefaultHeader:@"X-UDID" value:[[UIDevice currentDevice] uniqueIdentifier]];
return self;
}
- (void) post:(NSString*)urlstring
parameters:(NSDictionary*)objects
success:(void (^)(AFHTTPRequestOperation *request, NSArray *jsonobjects))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
[self postPath:urlstring
parameters:objects
success:^(AFHTTPRequestOperation *request, id JSON){
NSLog(@"postPath request: %@", request.request.URL);
if ([JSON isKindOfClass: [NSString class]]){
JSON = [JSON JSONValueWithOptions: nil];
if ([JSON isKindOfClass: [NSDictionary class]]){
if(success) {
success(request,JSON);
}
}
}
else
{
if ([JSON isKindOfClass: [NSDictionary class]]){
if(success) {
success(request,JSON);
}
}
}
}
failure:failure];
}
- (void) get:(NSString*)urlstring
parameters:(NSDictionary*)objects
success:(void (^)(AFHTTPRequestOperation *request, NSArray *jsonobjects))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
[self getPath:urlstring
parameters:objects
success:^(AFHTTPRequestOperation *request, id JSON){
NSLog(@"getPath request: %@", request.request.URL);
if ([JSON isKindOfClass: [NSString class]]){
JSON = [JSON JSONValueWithOptions: nil];
if ([JSON isKindOfClass: [NSDictionary class]]){
if(success) {
success(request,JSON);
}
}
}
else
{
if ([JSON isKindOfClass: [NSDictionary class]]){
if(success) {
success(request,JSON);
}
}
}
}
failure:failure];
}
@end