|
9 | 9 |
|
10 | 10 | #import "PFCommandURLRequestConstructor.h"
|
11 | 11 |
|
| 12 | +#import "BFTask+Private.h" |
12 | 13 | #import "PFAssert.h"
|
13 | 14 | #import "PFCommandRunningConstants.h"
|
14 | 15 | #import "PFDevice.h"
|
@@ -47,58 +48,62 @@ + (instancetype)constructorWithDataSource:(id<PFInstallationIdentifierStoreProvi
|
47 | 48 | #pragma mark - Data
|
48 | 49 | ///--------------------------------------
|
49 | 50 |
|
50 |
| -- (NSURLRequest *)dataURLRequestForCommand:(PFRESTCommand *)command { |
51 |
| - NSURL *url = [PFURLConstructor URLFromAbsoluteString:[PFInternalUtils parseServerURLString] |
52 |
| - path:[NSString stringWithFormat:@"/1/%@", command.httpPath] |
53 |
| - query:nil]; |
54 |
| - NSDictionary *headers = [self _URLRequestHeadersForCommand:command]; |
55 |
| - |
56 |
| - NSString *requestMethod = command.httpMethod; |
57 |
| - NSDictionary *requestParameters = nil; |
58 |
| - if (command.parameters) { |
59 |
| - NSDictionary *parameters = nil; |
60 |
| - |
61 |
| - // The request URI may be too long to include parameters in the URI. |
62 |
| - // To avoid this problem we send the parameters in a POST request json-encoded body |
63 |
| - // and add a custom parameter that overrides the method in a request. |
64 |
| - if ([requestMethod isEqualToString:PFHTTPRequestMethodGET] || |
65 |
| - [requestMethod isEqualToString:PFHTTPRequestMethodHEAD] || |
66 |
| - [requestMethod isEqualToString:PFHTTPRequestMethodDELETE]) { |
67 |
| - NSMutableDictionary *mutableParameters = [command.parameters mutableCopy]; |
68 |
| - mutableParameters[PFCommandParameterNameMethodOverride] = command.httpMethod; |
69 |
| - |
70 |
| - requestMethod = PFHTTPRequestMethodPOST; |
71 |
| - parameters = [mutableParameters copy]; |
72 |
| - } else { |
73 |
| - parameters = command.parameters; |
| 51 | +- (BFTask PF_GENERIC(NSURLRequest *)*)getDataURLRequestAsyncForCommand:(PFRESTCommand *)command { |
| 52 | + return (BFTask *)[[self _getURLRequestHeadersAsyncForCommand:command] continueWithSuccessBlock:^id(BFTask PF_GENERIC(NSDictionary *)*task) { |
| 53 | + NSURL *url = [PFURLConstructor URLFromAbsoluteString:[PFInternalUtils parseServerURLString] |
| 54 | + path:[NSString stringWithFormat:@"/1/%@", command.httpPath] |
| 55 | + query:nil]; |
| 56 | + NSDictionary *headers = task.result; |
| 57 | + |
| 58 | + NSString *requestMethod = command.httpMethod; |
| 59 | + NSDictionary *requestParameters = nil; |
| 60 | + if (command.parameters) { |
| 61 | + NSDictionary *parameters = nil; |
| 62 | + |
| 63 | + // The request URI may be too long to include parameters in the URI. |
| 64 | + // To avoid this problem we send the parameters in a POST request json-encoded body |
| 65 | + // and add a custom parameter that overrides the method in a request. |
| 66 | + if ([requestMethod isEqualToString:PFHTTPRequestMethodGET] || |
| 67 | + [requestMethod isEqualToString:PFHTTPRequestMethodHEAD] || |
| 68 | + [requestMethod isEqualToString:PFHTTPRequestMethodDELETE]) { |
| 69 | + NSMutableDictionary *mutableParameters = [command.parameters mutableCopy]; |
| 70 | + mutableParameters[PFCommandParameterNameMethodOverride] = command.httpMethod; |
| 71 | + |
| 72 | + requestMethod = PFHTTPRequestMethodPOST; |
| 73 | + parameters = [mutableParameters copy]; |
| 74 | + } else { |
| 75 | + parameters = command.parameters; |
| 76 | + } |
| 77 | + requestParameters = [[PFPointerObjectEncoder objectEncoder] encodeObject:parameters]; |
74 | 78 | }
|
75 |
| - requestParameters = [[PFPointerObjectEncoder objectEncoder] encodeObject:parameters]; |
76 |
| - } |
77 | 79 |
|
78 |
| - return [PFHTTPURLRequestConstructor urlRequestWithURL:url |
79 |
| - httpMethod:requestMethod |
80 |
| - httpHeaders:headers |
81 |
| - parameters:requestParameters]; |
| 80 | + return [PFHTTPURLRequestConstructor urlRequestWithURL:url |
| 81 | + httpMethod:requestMethod |
| 82 | + httpHeaders:headers |
| 83 | + parameters:requestParameters]; |
| 84 | + }]; |
82 | 85 | }
|
83 | 86 |
|
84 | 87 | ///--------------------------------------
|
85 | 88 | #pragma mark - File
|
86 | 89 | ///--------------------------------------
|
87 | 90 |
|
88 |
| -- (NSURLRequest *)fileUploadURLRequestForCommand:(PFRESTCommand *)command |
89 |
| - withContentType:(NSString *)contentType |
90 |
| - contentSourceFilePath:(NSString *)contentFilePath { |
91 |
| - NSMutableURLRequest *request = [[self dataURLRequestForCommand:command] mutableCopy]; |
| 91 | +- (BFTask PF_GENERIC(NSURLRequest *)*)getFileUploadURLRequestAsyncForCommand:(PFRESTCommand *)command |
| 92 | + withContentType:(NSString *)contentType |
| 93 | + contentSourceFilePath:(NSString *)contentFilePath { |
| 94 | + return [[self getDataURLRequestAsyncForCommand:command] continueWithSuccessBlock:^id(BFTask PF_GENERIC(NSURLRequest *)*task) { |
| 95 | + NSMutableURLRequest *request = [task.result mutableCopy]; |
92 | 96 |
|
93 |
| - if (contentType) { |
94 |
| - [request setValue:contentType forHTTPHeaderField:PFHTTPRequestHeaderNameContentType]; |
95 |
| - } |
| 97 | + if (contentType) { |
| 98 | + [request setValue:contentType forHTTPHeaderField:PFHTTPRequestHeaderNameContentType]; |
| 99 | + } |
96 | 100 |
|
97 |
| - //TODO (nlutsenko): Check for error here. |
98 |
| - NSNumber *fileSize = [PFInternalUtils fileSizeOfFileAtPath:contentFilePath error:nil]; |
99 |
| - [request setValue:[fileSize stringValue] forHTTPHeaderField:PFHTTPRequestHeaderNameContentLength]; |
| 101 | + //TODO (nlutsenko): Check for error here. |
| 102 | + NSNumber *fileSize = [PFInternalUtils fileSizeOfFileAtPath:contentFilePath error:nil]; |
| 103 | + [request setValue:[fileSize stringValue] forHTTPHeaderField:PFHTTPRequestHeaderNameContentLength]; |
100 | 104 |
|
101 |
| - return request; |
| 105 | + return request; |
| 106 | + }]; |
102 | 107 | }
|
103 | 108 |
|
104 | 109 | ///--------------------------------------
|
@@ -135,15 +140,18 @@ + (NSDictionary *)defaultURLRequestHeadersForApplicationId:(NSString *)applicati
|
135 | 140 | return [mutableHeaders copy];
|
136 | 141 | }
|
137 | 142 |
|
138 |
| -- (NSDictionary *)_URLRequestHeadersForCommand:(PFRESTCommand *)command { |
139 |
| - NSMutableDictionary *headers = [NSMutableDictionary dictionary]; |
140 |
| - [headers addEntriesFromDictionary:command.additionalRequestHeaders]; |
141 |
| - PFInstallationIdentifierStore *installationIdentifierStore = self.dataSource.installationIdentifierStore; |
142 |
| - headers[PFCommandHeaderNameInstallationId] = installationIdentifierStore.installationIdentifier; |
143 |
| - if (command.sessionToken) { |
144 |
| - headers[PFCommandHeaderNameSessionToken] = command.sessionToken; |
145 |
| - } |
146 |
| - return [headers copy]; |
| 143 | +- (BFTask PF_GENERIC(NSDictionary *)*)_getURLRequestHeadersAsyncForCommand:(PFRESTCommand *)command { |
| 144 | + return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id { |
| 145 | + NSMutableDictionary *headers = [NSMutableDictionary dictionary]; |
| 146 | + [headers addEntriesFromDictionary:command.additionalRequestHeaders]; |
| 147 | + if (command.sessionToken) { |
| 148 | + headers[PFCommandHeaderNameSessionToken] = command.sessionToken; |
| 149 | + } |
| 150 | + return [[self.dataSource.installationIdentifierStore getInstallationIdentifierAsync] continueWithSuccessBlock:^id(BFTask PF_GENERIC(NSString *)*task) { |
| 151 | + headers[PFCommandHeaderNameInstallationId] = task.result; |
| 152 | + return [headers copy]; |
| 153 | + }]; |
| 154 | + }]; |
147 | 155 | }
|
148 | 156 |
|
149 | 157 | @end
|
0 commit comments