-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConnection+Request.swift
66 lines (52 loc) · 2.51 KB
/
Connection+Request.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
//
// Connection+Request.swift
// IFTTT SDK
//
// Copyright © 2019 IFTTT. All rights reserved.
//
import Foundation
public extension Connection {
/// Handles network requests related to the `Connection`.
struct Request {
/// The HTTP request method options.
enum Method: String {
/// The HTTP GET method.
case GET = "GET"
/// The HTTP POST method.
case POST = "POST"
}
/// The `Request`'s `URLRequest` that task are completed on.
public let urlRequest: URLRequest
/// A `Request` configured to get a `Connection` with the provided identifier.
///
/// - Parameters:
/// - id: The identifier of the `Connection`.
/// - credentialProvider: An object that handle providing credentials for a request.
/// - Returns: A `Request` configured to get the `Connection`.
public static func fetchConnection(for id: String, credentialProvider: ConnectionCredentialProvider) -> Request {
return Request(path: "/connections/\(id)", method: .GET, credentialProvider: credentialProvider)
}
/// A disconnection `Request` for a `Connection` with the provided identifier.
///
/// - Parameters:
/// - id: The identifier of the `Connection`.
/// - credentialProvider: An object that handle providing credentials for a request.
/// - Returns: A `Request` configured to disconnect the `Connection`.
public static func disconnectConnection(with id: String, credentialProvider: ConnectionCredentialProvider) -> Request {
return Request(path: "/connections/\(id)/disable", method: .POST, credentialProvider: credentialProvider)
}
private init(path: String, method: Method, credentialProvider: ConnectionCredentialProvider) {
let url = API.base.appendingPathComponent(path)
var request = URLRequest(url: url)
request.httpMethod = method.rawValue
if let userToken = credentialProvider.userToken, userToken.isEmpty == false {
request.addIftttServiceToken(userToken)
}
if let inviteCode = credentialProvider.inviteCode, inviteCode.isEmpty == false {
request.addIftttInviteCode(inviteCode)
}
request.addVersionTracking()
self.urlRequest = request
}
}
}