Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Compatibility with AFNetworking 3 and HTTP/2 #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions AFJSONRPCClient.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Pod::Spec.new do |s|
s.source_files = 'AFJSONRPCClient'
s.requires_arc = true

s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.ios.deployment_target = '7.0'
s.osx.deployment_target = '10.9'

s.dependency 'AFNetworking', '~> 2.1'
s.dependency 'AFNetworking', '> 2.1'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to lock to ~> 3.0 or perhaps a range of > 2.1, < 4.0 if AFNetworking 2 should still be supported. Otherwise any breaking changes in AFNetworking 4.0 would break AFJSONRPCClient.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code only works with AFNetworking 3.0, It is better to lock it to ~> 3.0.
We can create a branch with current code for AFNetworking 2.0 if anyone uses it.
I have changed it in a new commit.
@kylef

end
16 changes: 8 additions & 8 deletions AFJSONRPCClient/AFJSONRPCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just import AFNetworkings framework header.

#import <AFNetworking/AFNetworking.h>


/**
AFJSONRPCClient objects communicate with web services using the JSON-RPC 2.0 protocol.

@see http://www.jsonrpc.org/specification
*/
@interface AFJSONRPCClient : AFHTTPRequestOperationManager
@interface AFJSONRPCClient : AFHTTPSessionManager

/**
The endpoint URL for the webservice.
Expand Down Expand Up @@ -74,8 +74,8 @@
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred.
*/
- (void)invokeMethod:(NSString *)method
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;

/**
Creates a request with the specified method and parameters, and enqueues a request operation for it.
Expand All @@ -87,8 +87,8 @@
*/
- (void)invokeMethod:(NSString *)method
withParameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;

/**
Creates a request with the specified method and parameters, and enqueues a request operation for it.
Expand All @@ -102,8 +102,8 @@
- (void)invokeMethod:(NSString *)method
withParameters:(id)parameters
requestId:(id)requestId
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;

///----------------------
/// @name Method Proxying
Expand Down
51 changes: 16 additions & 35 deletions AFJSONRPCClient/AFJSONRPCClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
// THE SOFTWARE.

#import "AFJSONRPCClient.h"
#import "AFHTTPRequestOperation.h"

#import <objc/runtime.h>

NSString * const AFJSONRPCErrorDomain = @"com.alamofire.networking.json-rpc";
Expand Down Expand Up @@ -81,34 +79,25 @@ - (id)initWithEndpointURL:(NSURL *)URL {
}

- (void)invokeMethod:(NSString *)method
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
[self invokeMethod:method withParameters:@[] success:success failure:failure];
}

- (void)invokeMethod:(NSString *)method
withParameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
[self invokeMethod:method withParameters:parameters requestId:@(1) success:success failure:failure];
}

- (void)invokeMethod:(NSString *)method
withParameters:(id)parameters
requestId:(id)requestId
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self requestWithMethod:method parameters:parameters requestId:requestId];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self.operationQueue addOperation:operation];
}

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
parameters:(id)parameters
requestId:(id)requestId
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
NSParameterAssert(method);

Expand All @@ -127,17 +116,8 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
payload[@"method"] = method;
payload[@"params"] = parameters;
payload[@"id"] = [requestId description];

return [self.requestSerializer requestWithMethod:@"POST" URLString:[self.endpointURL absoluteString] parameters:payload error:nil];
}

#pragma mark - AFHTTPClient

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
return [super HTTPRequestOperationWithRequest:urlRequest success:^(AFHTTPRequestOperation *operation, id responseObject) {

[self POST:@"" parameters:payload success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSInteger code = 0;
NSString *message = nil;
id data = nil;
Expand All @@ -148,7 +128,7 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlR

if (result && result != [NSNull null]) {
if (success) {
success(operation, result);
success(task, result);
return;
}
} else if (error && error != [NSNull null]) {
Expand Down Expand Up @@ -185,12 +165,13 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlR
}

NSError *error = [NSError errorWithDomain:AFJSONRPCErrorDomain code:code userInfo:userInfo];

failure(operation, error);
failure(task, error);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(operation, error);
failure(task, error);
}
}];
}
Expand Down Expand Up @@ -253,11 +234,11 @@ - (void)forwardInvocation:(NSInvocation *)invocation {
__strong AFJSONRPCProxySuccessBlock strongSuccess = [unsafeSuccess copy];
__strong AFJSONRPCProxyFailureBlock strongFailure = [unsafeFailure copy];

[self.client invokeMethod:RPCMethod withParameters:arguments success:^(__unused AFHTTPRequestOperation *operation, id responseObject) {
[self.client invokeMethod:RPCMethod withParameters:arguments success:^(__unused NSURLSessionDataTask *task, id responseObject) {
if (strongSuccess) {
strongSuccess(responseObject);
}
} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
} failure:^(__unused NSURLSessionDataTask *task, NSError *error) {
if (strongFailure) {
strongFailure(error);
}
Expand Down