forked from luckymarmot/Paw-SwiftAlamofireCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swift.mustache
117 lines (101 loc) · 4.15 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Alamofire
/// Networking abstraction
///
/// [Alamofire docs](https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#making-requests)
public struct Networking {
let session: Session
let timeout = {{#timeout}}{{{timeout}}}{{/timeout}}{{^timeout}}0.0{{/timeout}}
{{#headers.has_headers}}
/// HTTP Headers
let headers = [
{{#headers.header_list}}
"{{{header_name}}}":"{{{header_value}}}",
{{/headers.header_list}}
]
{{/headers.has_headers}}
// MARK: Lifecycle
init() {
self.session = Networking.makeManager(timeoutInterval: timeout)
}
static func makeManager(timeoutInterval: TimeInterval = 0) -> Session {
let configuration = URLSessionConfiguration.default
configuration.headers = .default
configuration.timeoutIntervalForRequest = timeoutInterval
return Session(configuration: configuration, startRequestsImmediately: false)
}
}
/// {{{request.name}}}
///
/// {{{method}}} {{{url.base}}}
func send{{{codeSlug}}}Request() {
let manager = Networking()
{{! ----- Headers ----- }}
{{#headers.has_headers}}
let headers = HTTPHeaders(manager.headers)
{{/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 { RawDataEncoding() }
func encode<Parameters>(_ parameters: Parameters?, into request: URLRequest) throws -> URLRequest where Parameters : Encodable {
var request = request
{{#body.has_short_body}}
let parameters = parameters as? String
request.httpBody = parameters?.data(using: .utf8)
{{/body.has_short_body}}
{{^body.has_short_body}}
// set your body data here
request.httpBody = nil
{{/body.has_short_body}}
return request
}
}
{{/body.has_raw_body}}
let requestURL = {{#has_params_to_encode}}"{{{url.base}}}"{{/has_params_to_encode}}{{^has_params_to_encode}}"{{{url.fullpath}}}"{{/has_params_to_encode}}
manager.session.request(requestURL, 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()
.responseJSON { debugPrint($0) }
{{/body.has_multipart_body}}
{{! ----- Upload (Multipart) ----- }}
{{#body.has_multipart_body}}
// Upload Request
manager.session.upload(multipartFormData: { multipartFormData in
{{#body.multipart_body}}
multipartFormData.append(Data("{{{value}}}".utf8), withName: "{{{name}}}")
{{/body.multipart_body}}
},
to: "{{{url.fullpath}}}",
method: .{{{method}}}{{#headers.has_headers}}, headers: headers{{/headers.has_headers}})
.response {
debugPrint($0)
}
{{/body.has_multipart_body}}
}