-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPutioAPITypes.swift
99 lines (83 loc) · 3.1 KB
/
PutioAPITypes.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
import Foundation
import Alamofire
import SwiftyJSON
public struct PutioAPIConfig {
public let baseURL: String
public var token: String
public var clientID: String
public var clientSecret: String
public var clientName: String
public var timeoutInterval: Double
public init(clientID: String, clientSecret: String = "", clientName: String = "", token: String = "", timeoutInterval: Double = 15.0) {
self.baseURL = PutioAPI.apiURL
self.clientID = clientID
self.clientSecret = clientSecret
self.clientName = clientName
self.token = token
self.timeoutInterval = timeoutInterval
}
}
public struct PutioAPIRequestConfig {
let url: String
let method: HTTPMethod
let headers: HTTPHeaders
let query: Parameters
let body: Parameters?
init(apiConfig: PutioAPIConfig, url: String, method: HTTPMethod, headers: HTTPHeaders = [:], query: Parameters = [:], body: Parameters = [:]) {
if (query.isEmpty) {
self.url = "\(apiConfig.baseURL)\(url)"
} else {
let encodedURLRequest = try! URLEncoding.queryString.encode(URLRequest(url: URL(string: url)!), with: query)
self.url = "\(apiConfig.baseURL)\((encodedURLRequest.url?.absoluteString)!)"
}
self.method = method
var enhancedHeaders = headers
if enhancedHeaders.value(for: "authorization") == nil {
if apiConfig.token != "" {
let authorizationHeader = HTTPHeader.authorization("token \(apiConfig.token)")
enhancedHeaders.add(authorizationHeader)
}
}
self.headers = enhancedHeaders
self.query = query
switch method {
case .post, .put, .patch:
self.body = body
default:
self.body = nil
}
}
}
public enum PutioAPIErrorType {
case httpError(statusCode: Int, errorType: String)
case networkError
case unknownError
}
public struct PutiopAPIErrorRequestInformation {
let config: PutioAPIRequestConfig
}
public struct PutioAPIError: Error {
public let request: PutiopAPIErrorRequestInformation
public let type: PutioAPIErrorType
public let message: String
public let underlyingError: Error
init(request: PutiopAPIErrorRequestInformation, errorJSON: JSON, error: AFError) {
self.request = request
self.type = .httpError(statusCode: errorJSON["status_code"].intValue, errorType: errorJSON["error_type"].stringValue)
self.message = errorJSON["message"].stringValue
self.underlyingError = error
}
init(request: PutiopAPIErrorRequestInformation, error: AFError) {
self.request = request
self.type = .networkError
self.message = error.localizedDescription
self.underlyingError = error
}
init(request: PutiopAPIErrorRequestInformation, error: Error) {
self.request = request
self.type = .unknownError
self.message = error.localizedDescription
self.underlyingError = error
}
}
public typealias PutioAPIBoolCompletion = (Result<JSON, PutioAPIError>) -> Void