-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPutioAPI.swift
87 lines (70 loc) · 3.59 KB
/
PutioAPI.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
import Foundation
import Alamofire
import SwiftyJSON
public protocol PutioAPIDelegate: class {
func onPutioAPIApiError(error: PutioAPIError)
}
public final class PutioAPI {
public typealias RequestCompletion = (Result<JSON, PutioAPIError>) -> Void
weak var delegate: PutioAPIDelegate?
static let apiURL = "https://api.put.io/v2"
public var config: PutioAPIConfig
public init(config: PutioAPIConfig) {
self.config = config
}
public func setToken(token: String) {
self.config.token = token
}
public func clearToken() {
self.config.token = ""
}
public func get(_ url: String, headers: HTTPHeaders = [:], query: Parameters = [:], _ completion: @escaping RequestCompletion) {
let requestConfig = PutioAPIRequestConfig(apiConfig: config, url: url, method: .get, headers: headers, query: query)
self.send(requestConfig: requestConfig, completion)
}
public func post(_ url: String, headers: HTTPHeaders = [:], query: Parameters = [:], body: Parameters = [:], _ completion: @escaping RequestCompletion) {
let requestConfig = PutioAPIRequestConfig(apiConfig: config, url: url, method: .post, headers: headers, query: query, body: body)
self.send(requestConfig: requestConfig, completion)
}
public func put(_ url: String, headers: HTTPHeaders = [:], query: Parameters = [:], body: Parameters = [:], _ completion: @escaping RequestCompletion) {
let requestConfig = PutioAPIRequestConfig(apiConfig: config, url: url, method: .put, headers: headers, query: query, body: body)
self.send(requestConfig: requestConfig, completion)
}
public func delete(_ url: String, headers: HTTPHeaders = [:], query: Parameters = [:], _ completion: @escaping RequestCompletion) {
let requestConfig = PutioAPIRequestConfig(apiConfig: config, url: url, method: .delete, headers: headers, query: query)
self.send(requestConfig: requestConfig, completion)
}
private func send(requestConfig: PutioAPIRequestConfig, _ completion: @escaping (Result<JSON, PutioAPIError>) -> Void) {
let requestInformation = PutiopAPIErrorRequestInformation(config: requestConfig)
AF.request(
requestConfig.url,
method: requestConfig.method,
parameters: requestConfig.body,
encoding: JSONEncoding.default,
headers: requestConfig.headers
) { $0.timeoutInterval = self.config.timeoutInterval }
.validate()
.responseData(completionHandler: { dataResponse in
do {
switch dataResponse.result {
case .success(let data):
let json = try JSON(data: data)
return completion(.success(json))
case .failure(let error):
if let data = dataResponse.data {
let apiError = PutioAPIError(request: requestInformation, errorJSON: try JSON(data: data), error: error)
self.delegate?.onPutioAPIApiError(error: apiError)
return completion(.failure(apiError))
}
let apiError = PutioAPIError(request: requestInformation, error: error)
self.delegate?.onPutioAPIApiError(error: apiError)
return completion(.failure(apiError))
}
} catch {
let apiError = PutioAPIError(request: requestInformation, error: error)
self.delegate?.onPutioAPIApiError(error: apiError)
return completion(.failure(apiError))
}
})
}
}