-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAFJSONRPCClient.m
executable file
·141 lines (116 loc) · 5.09 KB
/
AFJSONRPCClient.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// AFJSONRPCClient.m
// JustCommunication.com
//
// Created by [email protected] on 27.03.12.
// Copyright (c) 2012 JustCommunication. All rights reserved.
//
#import "AFJSONRPCClient.h"
#import "AFJSONRequestOperation.h"
NSString * const AFJSONRPCErrorDomain = @"org.json-rpc";
@implementation AFJSONRPCClient
@synthesize endpointURL = _endpointURL;
@synthesize operationQueue = _operationQueue;
- (id)initWithURL:(NSURL *)url
{
self = [super init];
if (!self) {
return nil;
}
self.endpointURL = url;
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
[self.operationQueue setMaxConcurrentOperationCount:4];
return self;
}
/*
- (void)setEndpointURL:(NSURL*)url
{
_endpointURL = url;
}
*/
- (void)invokeMethod:(NSString *)method
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
[self invokeMethod:method withParameters:[NSArray array] withRequestId:@"1" success:success failure:failure];
}
- (void)invokeMethod:(NSString *)method
withParameters:(NSObject *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
[self invokeMethod:method withParameters:parameters withRequestId:@"1" success:success failure:failure];
}
- (void)invokeMethod:(NSString *)method
withParameters:(NSObject *)parameters
withRequestId:(NSString *)requestId
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSURLRequest *request = [self requestWithMethod:method parameters:parameters requestId:requestId];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"application/json-rpc", @"application/jsonrequest", nil]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger errorCode = 0;
NSString *errorMessage = nil;
if ([responseObject isKindOfClass:[NSDictionary class]]) {
id result = [responseObject objectForKey:@"result"];
id error = [responseObject objectForKey:@"error"];
if (result && result != [NSNull null]) {
if (success) {
success(operation, result);
}
} else if (error && error != [NSNull null]) {
if ([error isKindOfClass:[NSDictionary class]] && [error objectForKey:@"code"] && [error objectForKey:@"message"]) {
errorCode = [[error objectForKey:@"code"] intValue];
errorMessage = [error objectForKey:@"message"];
} else {
errorMessage = @"Unknown error";
}
} else {
errorMessage = @"Unknown json-rpc response";
}
} else {
errorMessage = @"Unknown json-rpc response";
}
if (errorMessage && failure) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:errorMessage, NSLocalizedDescriptionKey, nil];
NSError *error = [NSError errorWithDomain:AFJSONRPCErrorDomain code:errorCode userInfo:userInfo];
failure(operation, error);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(operation, error);
}
}];
[self.operationQueue addOperation:operation];
[operation release];
}
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
parameters:(NSObject *)parameters
requestId:(NSString *)requestId
{
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.endpointURL];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
NSDictionary *JSONRPCStruct = [NSDictionary dictionaryWithObjectsAndKeys:
@"2.0", @"jsonrpc",
method, @"method",
parameters, @"params",
requestId, @"id",
nil];
NSError *error = nil;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:JSONRPCStruct options:0 error:&error];
if (!error) {
[request setHTTPBody:JSONData];
}
return request;
}
- (void)dealloc
{
[_endpointURL release];
[_operationQueue release];
[super dealloc];
}
@end