/
objc.mustache
112 lines (98 loc) · 4.62 KB
/
objc.mustache
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
- (void)send{{{codeSlug}}}Request:(id)sender
{
// {{{request.name}}} ({{{request.method}}} {{{url.base}}})
// Create manager
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
{{! ----- }}
{{^has_params_and_body}}
{{#url.has_params}}
// Create request
NSDictionary* URLParameters = @{
{{#url.params}}
@"{{{name}}}":@"{{{value}}}",
{{/url.params}}
};
NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.base}}}" parameters:URLParameters error:NULL];
{{/url.has_params}}
{{^url.has_params}}
// Create request
NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.base}}}" parameters:nil error:NULL];
{{/url.has_params}}
{{/has_params_and_body}}
{{! ----- }}
{{#body.has_url_encoded_body}}
// Form URL-Encoded Body
NSDictionary* bodyParameters = @{
{{#body.url_encoded_body}}
@"{{{name}}}":@"{{{value}}}",
{{/body.url_encoded_body}}
};
{{^has_params_and_body}}
NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.base}}}" parameters:bodyParameters error:NULL];
{{/has_params_and_body}}
{{#has_params_and_body}}
NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.fullpath}}}" parameters:bodyParameters error:NULL];
{{/has_params_and_body}}
{{/body.has_url_encoded_body}}
{{! ----- }}
{{#body.has_multipart_body}}
// Form Multipart Body
NSDictionary* bodyParameters = @{
{{#body.multipart_body}}
@"{{{name}}}":@"{{{value}}}",
{{/body.multipart_body}}
};
{{^has_params_and_body}}
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.base}}}" parameters:bodyParameters constructingBodyWithBlock:nil error:NULL];
{{/has_params_and_body}}
{{#has_params_and_body}}
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.fullpath}}}" parameters:bodyParameters constructingBodyWithBlock:nil error:NULL];
{{/has_params_and_body}}
{{/body.has_multipart_body}}
{{! ----- }}
{{#body.has_json_body}}
// JSON Body
{{{body.json_body_object}}}
{{^has_params_and_body}}
NSMutableURLRequest* request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.base}}}" parameters:bodyObject error:NULL];
{{/has_params_and_body}}
{{#has_params_and_body}}
NSMutableURLRequest* request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"{{{request.method}}}" URLString:@"{{{url.fullpath}}}" parameters:bodyObject error:NULL];
{{/has_params_and_body}}
{{/body.has_json_body}}
{{! ----- }}
{{#body.has_raw_body}}
// Body
NSURL* URL = [NSURL URLWithString:@"{{{url.fullpath}}}"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"{{{request.method}}}"];
[request setHTTPBody:[@"{{{body.raw_body}}}" dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"" forHTTPHeaderField:@"Content-Type"];
{{/body.has_raw_body}}
{{! ----- }}
{{#body.has_long_body}}
// Body
NSURL* URL = [NSURL URLWithString:@"{{{url.fullpath}}}"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"{{{request.method}}}"];
[request setHTTPBody:@"Set your own body data" dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"" forHTTPHeaderField:@"Content-Type"];
{{/body.has_long_body}}
{{! ----- }}
{{#headers.has_headers}}
// Add Headers
{{#headers.header_list}}
[request setValue:@"{{{header_value}}}" forHTTPHeaderField:@"{{{header_name}}}"];
{{/headers.header_list}}
{{/headers.has_headers}}
{{! ----- }}
// Fetch Request
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"HTTP Response Status Code: %ld", [operation.response statusCode]);
NSLog(@"HTTP Response Body: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"HTTP Request failed: %@", error);
}];
[manager.operationQueue addOperation:operation];
}