-
Notifications
You must be signed in to change notification settings - Fork 30
/
XcodeServer+Integration.swift
170 lines (131 loc) · 6.25 KB
/
XcodeServer+Integration.swift
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
//
// XcodeServer+Integration.swift
// XcodeServerSDK
//
// Created by Mateusz Zając on 01.07.2015.
// Copyright © 2015 Honza Dvorsky. All rights reserved.
//
import Foundation
import BuildaUtils
// MARK: - XcodeSever API Routes for Integrations management
extension XcodeServer {
// MARK: Bot releated integrations
/**
XCS API call for getting a list of filtered integrations for bot.
Available queries:
- **last** - find last integration for bot
- **from** - find integration based on date range
- **number** - find integration for bot by its number
- parameter botId: ID of bot.
- parameter query: Query which should be used to filter integrations.
- parameter integrations: Optional array of integrations returned from XCS.
- parameter error: Optional error.
*/
public final func getBotIntegrations(botId: String, query: [String: String], completion: (integrations: [Integration]?, error: NSError?) -> ()) {
let params = [
"bot": botId
]
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: params, query: query, body: nil) { (response, body, error) -> () in
if error != nil {
completion(integrations: nil, error: error)
return
}
if let body = (body as? NSDictionary)?["results"] as? NSArray {
let integrations: [Integration] = XcodeServerArray(body)
completion(integrations: integrations, error: nil)
} else {
completion(integrations: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}
/**
XCS API call for firing integration for specified bot.
- parameter botId: ID of the bot.
- parameter integration: Optional object of integration returned if run was successful.
- parameter error: Optional error.
*/
public final func postIntegration(botId: String, completion: (integration: Integration?, error: NSError?) -> ()) {
let params = [
"bot": botId
]
self.sendRequestWithMethod(.POST, endpoint: .Integrations, params: params, query: nil, body: nil) { (response, body, error) -> () in
if error != nil {
completion(integration: nil, error: error)
return
}
if let body = body as? NSDictionary {
let integration = Integration(json: body)
completion(integration: integration, error: nil)
} else {
completion(integration: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}
// MARK: General integrations methods
/**
XCS API call for retrievieng all available integrations on server.
- parameter integrations: Optional array of integrations.
- parameter error: Optional error.
*/
public final func getIntegrations(completion: (integrations: [Integration]?, error: NSError?) -> ()) {
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: nil, query: nil, body: nil) {
(response, body, error) -> () in
guard error == nil else {
completion(integrations: nil, error: error)
return
}
guard let integrationsBody = (body as? NSDictionary)?["results"] as? NSArray else {
completion(integrations: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let integrations: [Integration] = XcodeServerArray(integrationsBody)
completion(integrations: integrations, error: nil)
}
}
/**
XCS API call for retrievieng specified Integration.
- parameter integrationId: ID of integration which is about to be retrieved.
- parameter completion:
- Optional retrieved integration.
- Optional operation error.
*/
public final func getIntegration(integrationId: String, completion: (integration: Integration?, error: NSError?) -> ()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: params, query: nil, body: nil) {
(response, body, error) -> () in
guard error == nil else {
completion(integration: nil, error: error)
return
}
guard let integrationBody = body as? NSDictionary else {
completion(integration: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let integration = Integration(json: integrationBody)
completion(integration: integration, error: nil)
}
}
/**
XCS API call for canceling specified integration.
- parameter integrationId: ID of integration.
- parameter success: Integration cancelling success indicator.
- parameter error: Optional operation error.
*/
public final func cancelIntegration(integrationId: String, completion: (success: Bool, error: NSError?) -> ()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.POST, endpoint: .CancelIntegration, params: params, query: nil, body: nil) { (response, body, error) -> () in
if error != nil {
completion(success: false, error: error)
return
}
completion(success: true, error: nil)
}
}
// TODO: Methods about to be implemented...
// public func reportQueueSizeAndEstimatedWaitingTime(integration: Integration, completion: ((queueSize: Int, estWait: Double), NSError?) -> ()) {
//TODO: we need to call getIntegrations() -> filter pending and running Integrations -> get unique bots of these integrations -> query for the average integration time of each bot -> estimate, based on the pending/running integrations, how long it will take for the passed in integration to finish
}