forked from iridia/IRWebAPIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRWebAPIEngine+ExternalTransforms.m
More file actions
82 lines (59 loc) · 2.8 KB
/
IRWebAPIEngine+ExternalTransforms.m
File metadata and controls
82 lines (59 loc) · 2.8 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
//
// IRWebAPIEngine+ExternalTransforms.m
// IRWebAPIKit
//
// Created by Evadne Wu on 11/14/11.
// Copyright (c) 2011 Iridia Productions. All rights reserved.
//
#import "IRWebAPIEngine+ExternalTransforms.h"
#import "IRWebAPIRequestContext.h"
#import "IRWebAPIHelpers.h"
@interface IRWebAPIEngine (ExternalTransforms_KnownPrivate)
- (IRWebAPIRequestContext *) requestContextByTransformingContext:(IRWebAPIRequestContext *)inContext forMethodNamed:(NSString *)inMethodName;
//- (NSURLRequest *) requestWithContext:(IRWebAPIRequestContext *)inContext;
@end
@implementation IRWebAPIEngine (ExternalTransforms)
- (NSURLRequest *) transformedRequestWithRequest:(NSURLRequest *)aRequest usingMethodName:(NSString *)aName {
IRWebAPIRequestContext *baseContext = [IRWebAPIRequestContext new];
baseContext.baseURL = [self.context baseURLForMethodNamed:aName];
baseContext.engineMethod = aName;
NSURL *baseURL = baseContext.baseURL;
NSDictionary *headerFields = baseContext.headerFields;
NSDictionary *arguments = baseContext.queryParams;
NSData *httpBody = baseContext.body;
NSString *httpMethod = baseContext.method;
IRWebAPIResponseParser responseParser = baseContext.parser;
if ([[aRequest allHTTPHeaderFields] count])
headerFields = [aRequest allHTTPHeaderFields];
if ([aRequest HTTPBody])
httpBody = [aRequest HTTPBody];
if ([aRequest URL]) {
NSURL *givenURL = [aRequest URL];
NSString *baseURLString = [[NSArray arrayWithObjects:
[givenURL scheme] ? [[givenURL scheme] stringByAppendingString:@"://"]: @"",
[givenURL host] ? [givenURL host] : @"",
[givenURL port] ? [@":" stringByAppendingString:[[givenURL port] stringValue]] : @"",
[givenURL path] ? [givenURL path] : @"",
// [givenURL query] ? [@"?" stringByAppendingString:[givenURL query]] : @"",
// [givenURL fragment] ? [@"#" stringByAppendingString:[givenURL fragment]] : @"",
nil] componentsJoinedByString:@""];
if ([givenURL query])
arguments = IRQueryParametersFromString([givenURL query]);
baseURL = [NSURL URLWithString:baseURLString];
}
IRWebAPIRequestContext *inferredContext = [IRWebAPIRequestContext new];
inferredContext.baseURL = baseURL;
inferredContext.engineMethod = baseContext.engineMethod;
[headerFields enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[inferredContext setValue:obj forHeaderField:key];
}];
[arguments enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[inferredContext setValue:obj forQueryParam:key];
}];
inferredContext.body = httpBody;
inferredContext.method = httpMethod;
inferredContext.parser = responseParser;
IRWebAPIRequestContext *transformedContext = [self requestContextByTransformingContext:inferredContext forMethodNamed:aName];
return [[IRWebAPIRequestOperation alloc] initWithContext:transformedContext].request;
}
@end