This repository was archived by the owner on Nov 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathAFHTTPSessionManager+RACSupport.m
More file actions
100 lines (82 loc) · 4.28 KB
/
AFHTTPSessionManager+RACSupport.m
File metadata and controls
100 lines (82 loc) · 4.28 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
96
97
98
99
100
//
// AFHTTPSessionManager+RACSupport.m
// Reactive AFNetworking Example
//
// Created by Robert Widmann on 5/20/14.
// Copyright (c) 2014 CodaFi. All rights reserved.
//
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)
#import "AFHTTPSessionManager+RACSupport.h"
NSString *const RACAFNResponseObjectErrorKey = @"responseObject";
@implementation AFHTTPSessionManager (RACSupport)
- (RACSignal *)rac_GET:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodGet]
setNameWithFormat:@"%@ -rac_GET: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_HEAD:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodHead]
setNameWithFormat:@"%@ -rac_HEAD: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_POST:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodPost]
setNameWithFormat:@"%@ -rac_POST: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_POST:(NSString *)path parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block {
return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:RACAFNHttpMethodPost URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:nil];
NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSMutableDictionary *userInfo = [error.userInfo mutableCopy];
if (responseObject) {
userInfo[RACAFNResponseObjectErrorKey] = responseObject;
}
NSError *errorWithRes = [NSError errorWithDomain:error.domain code:error.code userInfo:[userInfo copy]];
[subscriber sendError:errorWithRes];
} else {
[subscriber sendNext:RACTuplePack(responseObject, response)];
[subscriber sendCompleted];
}
}];
[task resume];
return [RACDisposable disposableWithBlock:^{
[task cancel];
}];
}] setNameWithFormat:@"%@ -rac_POST: %@, parameters: %@, constructingBodyWithBlock:", self.class, path, parameters];
;
}
- (RACSignal *)rac_PUT:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodPut]
setNameWithFormat:@"%@ -rac_PUT: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_PATCH:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodPatch]
setNameWithFormat:@"%@ -rac_PATCH: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_DELETE:(NSString *)path parameters:(id)parameters {
return [[self rac_requestPath:path parameters:parameters method:RACAFNHttpMethodDelete]
setNameWithFormat:@"%@ -rac_DELETE: %@, parameters: %@", self.class, path, parameters];
}
- (RACSignal *)rac_requestPath:(NSString *)path parameters:(id)parameters method:(NSString *)method {
return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
NSURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSMutableDictionary *userInfo = [error.userInfo mutableCopy];
if (responseObject) {
userInfo[RACAFNResponseObjectErrorKey] = responseObject;
}
NSError *errorWithRes = [NSError errorWithDomain:error.domain code:error.code userInfo:[userInfo copy]];
[subscriber sendError:errorWithRes];
} else {
[subscriber sendNext:RACTuplePack(responseObject, response)];
[subscriber sendCompleted];
}
}];
[task resume];
return [RACDisposable disposableWithBlock:^{
[task cancel];
}];
}];
}
@end
#endif