-
Notifications
You must be signed in to change notification settings - Fork 4
/
swift.mustache
103 lines (96 loc) · 4.2 KB
/
swift.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
func send{{{codeSlug}}}Request() {
/**
{{{request.name}}}
{{{method}}} {{{url.base}}}
*/
{{! ----- Timeout ----- }}
{{#timeout}}
// Add timeout
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = {{{timeout}}}
let manager = Alamofire.SessionManager(configuration: configuration)
{{/timeout}}
{{! ----- Headers ----- }}
{{#headers.has_headers}}
// Add Headers
let headers = [
{{#headers.header_list}}
"{{{header_name}}}":"{{{header_value}}}",
{{/headers.header_list}}
]
{{/headers.has_headers}}
{{! ----- URL Parameters ----- }}
{{#has_params_to_encode}}
// Add URL parameters
let urlParams = [
{{#url.params}}
"{{{name}}}":"{{{value}}}",
{{/url.params}}
]
{{/has_params_to_encode}}
{{! ----- Form URL-Encoded Body ----- }}
{{#body.has_url_encoded_body}}
// Form URL-Encoded Body
let body = [
{{#body.url_encoded_body}}
"{{{name}}}":"{{{value}}}",
{{/body.url_encoded_body}}
]
{{/body.has_url_encoded_body}}
{{! ----- JSON Body ----- }}
{{#body.has_json_body}}
// JSON Body
{{{body.json_body_object}}}
{{/body.has_json_body}}
{{! ----- Request ----- }}
{{^body.has_multipart_body}}
{{#body.has_raw_body}}
// Custom Body Encoding
struct RawDataEncoding: ParameterEncoding {
public static var `default`: RawDataEncoding { return RawDataEncoding() }
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
{{#body.has_short_body}}
request.httpBody = "{{{body.short_body}}}".data(using: String.Encoding.utf8, allowLossyConversion: false)
{{/body.has_short_body}}
{{^body.has_short_body}}
request.httpBody = nil // set your body data here
{{/body.has_short_body}}
return request
}
}
{{/body.has_raw_body}}
// Fetch Request
{{#timeout}}manager{{/timeout}}{{^timeout}}Alamofire{{/timeout}}.request({{#has_params_to_encode}}"{{{url.base}}}"{{/has_params_to_encode}}{{^has_params_to_encode}}"{{{url.fullpath}}}"{{/has_params_to_encode}}, method: .{{{method}}}{{#body}}{{^body.has_raw_body}}, parameters: body{{/body.has_raw_body}}{{/body}}{{#body.has_raw_body}}, encoding: RawDataEncoding.default{{/body.has_raw_body}}{{^body}}{{#has_params_to_encode}}, parameters: urlParams{{/has_params_to_encode}}{{/body}}{{#body.has_json_body}}, encoding: JSONEncoding.default{{/body.has_json_body}}{{#body.has_url_encoded_body}}, encoding: URLEncoding.default{{/body.has_url_encoded_body}}{{#headers.has_headers}}, headers: headers{{/headers.has_headers}})
{{#httpBasicAuth}}
.authenticate(user: "{{httpBasicAuth.username}}", password: "{{httpBasicAuth.password}}")
{{/httpBasicAuth}}
.validate(statusCode: 200..<300)
.responseJSON { response in
if (response.result.error == nil) {
debugPrint("HTTP Response Body: \(response.data)")
}
else {
debugPrint("HTTP Request failed: \(response.result.error)")
}
}
{{/body.has_multipart_body}}
{{! ----- Upload (Multipart) ----- }}
{{#body.has_multipart_body}}
// Fetch Request
{{#timeout}}manager{{/timeout}}{{^timeout}}Alamofire{{/timeout}}.upload(multipartFormData: { multipartFormData in
{{#body.multipart_body}}
multipartFormData.append("{{{value}}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"{{{name}}}")
{{/body.multipart_body}}
}, usingThreshold: UInt64.init(), to: "{{{url.fullpath}}}", method: .{{{method}}}{{#headers.has_headers}}, headers: headers{{/headers.has_headers}}, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
{{/body.has_multipart_body}}
}