-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathXcodeServerEndpoints.swift
More file actions
222 lines (168 loc) · 6.72 KB
/
Copy pathXcodeServerEndpoints.swift
File metadata and controls
222 lines (168 loc) · 6.72 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// XcodeServerEndpoints.swift
// Buildasaur
//
// Created by Honza Dvorsky on 14/12/2014.
// Copyright (c) 2014 Honza Dvorsky. All rights reserved.
//
import Foundation
import BuildaUtils
public class XcodeServerEndpoints {
enum Endpoint {
case Bots
case CancelIntegration
case Commits
case Devices
case Hostname
case Integrations
case Issues
case LiveUpdates
case Login
case Logout
case Platforms
case Repositories
case SCM_Branches
case UserCanCreateBots
case Versions
case Toolchains
}
let serverConfig: XcodeServerConfig
/**
Designated initializer.
- parameter serverConfig: Object of XcodeServerConfig class
- returns: Initialized object of XcodeServer endpoints
*/
public init(serverConfig: XcodeServerConfig) {
self.serverConfig = serverConfig
}
func endpointURL(endpoint: Endpoint, params: [String: String]? = nil) -> String {
let base = "/api"
switch endpoint {
case .Bots:
let bots = "\(base)/bots"
if let bot = params?["bot"] {
let bot = "\(bots)/\(bot)"
if
let rev = params?["rev"],
let method = params?["method"] where method == "DELETE"
{
let rev = "\(bot)/\(rev)"
return rev
}
return bot
}
return bots
case .Integrations:
if let _ = params?["bot"] {
//gets a list of integrations for this bot
let bots = self.endpointURL(.Bots, params: params)
return "\(bots)/integrations"
}
let integrations = "\(base)/integrations"
if let integration = params?["integration"] {
let oneIntegration = "\(integrations)/\(integration)"
return oneIntegration
}
return integrations
case .CancelIntegration:
let integration = self.endpointURL(.Integrations, params: params)
let cancel = "\(integration)/cancel"
return cancel
case .Devices:
let devices = "\(base)/devices"
return devices
case .UserCanCreateBots:
let users = "\(base)/auth/isBotCreator"
return users
case .Login:
let login = "\(base)/auth/login"
return login
case .Logout:
let logout = "\(base)/auth/logout"
return logout
case .Platforms:
let platforms = "\(base)/platforms"
return platforms
case .SCM_Branches:
let branches = "\(base)/scm/branches"
return branches
case .Repositories:
let repositories = "\(base)/repositories"
return repositories
case .Commits:
let integration = self.endpointURL(.Integrations, params: params)
let commits = "\(integration)/commits"
return commits
case .Issues:
let integration = self.endpointURL(.Integrations, params: params)
let issues = "\(integration)/issues"
return issues
case .LiveUpdates:
let base = "/xcode/internal/socket.io/1"
if let pollId = params?["poll_id"] {
return "\(base)/xhr-polling/\(pollId)"
}
return base
case .Hostname:
let hostname = "\(base)/hostname"
return hostname
case .Versions:
let versions = "\(base)/versions"
return versions
case .Toolchains:
let toolchains = "\(base)/toolchains"
return toolchains
}
}
/**
Builder method for URlrequests based on input parameters.
- parameter method: HTTP method used for request (GET, POST etc.)
- parameter endpoint: Endpoint object
- parameter params: URL params (default is nil)
- parameter query: Query parameters (default is nil)
- parameter body: Request's body (default is nil)
- parameter doBasicAuth: Requirement of authorization (default is true)
- returns: NSMutableRequest or nil if wrong URL was provided
*/
func createRequest(method: HTTP.Method, endpoint: Endpoint, params: [String : String]? = nil, query: [String : String]? = nil, body: NSDictionary? = nil, doBasicAuth auth: Bool = true, portOverride: Int? = nil) -> NSMutableURLRequest? {
var allParams = [
"method": method.rawValue
]
//merge the two params
if let params = params {
for (key, value) in params {
allParams[key] = value
}
}
let port = portOverride ?? self.serverConfig.port
let endpointURL = self.endpointURL(endpoint, params: allParams)
let queryString = HTTP.stringForQuery(query)
let wholePath = "\(self.serverConfig.host):\(port)\(endpointURL)\(queryString)"
guard let url = NSURL(string: wholePath) else {
return nil
}
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = method.rawValue
if auth {
//add authorization header
let user = self.serverConfig.user ?? ""
let password = self.serverConfig.password ?? ""
let plainString = "\(user):\(password)" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
request.setValue("Basic \(base64String!)", forHTTPHeaderField: "Authorization")
}
if let body = body {
do {
let data = try NSJSONSerialization.dataWithJSONObject(body, options: .PrettyPrinted)
request.HTTPBody = data
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
} catch {
let error = error as NSError
Log.error("Parsing error \(error.description)")
return nil
}
}
return request
}
}