6
6
7
7
import Foundation
8
8
public class ApiVideoClient {
9
- public static var apiKey : String ? = nil
9
+ private static var apiKey : String ? = nil
10
10
public static var basePath = " https://ws.api.video "
11
- internal static var customHeaders : [ String : String ] = [ " AV-Origin-Client " : " swift:1.2.1 " ]
11
+ internal static var defaultHeaders : [ String : String ] = [ " AV-Origin-Client " : " swift:1.2.1 " ]
12
+ internal static var credential : URLCredential ?
12
13
private static var chunkSize : Int = 50 * 1024 * 1024
13
- internal static var requestBuilderFactory : RequestBuilderFactory = AlamofireRequestBuilderFactory ( )
14
- internal static var credential = ApiVideoCredential ( )
14
+ internal static var requestBuilderFactory : RequestBuilderFactory = URLSessionRequestBuilderFactory ( )
15
15
public static var apiResponseQueue : DispatchQueue = . main
16
16
public static var timeout : TimeInterval = 60
17
+ internal static var customHeaders : [ String : String ] {
18
+ var headers = defaultHeaders
19
+ if let apiKey = apiKey {
20
+ headers [ " Authorization " ] = apiKey
21
+ }
22
+ return headers
23
+ }
24
+
25
+ public static func setApiKey( _ apiKey: String ? ) {
26
+ if let apiKey = apiKey {
27
+ self . apiKey = " Basic " + " \( apiKey) : " . toBase64 ( )
28
+ } else {
29
+ self . apiKey = nil
30
+ }
31
+ }
17
32
18
- public static func setChunkSize( chunkSize: Int ) throws {
33
+ public static func setChunkSize( _ chunkSize: Int ) throws {
19
34
if ( chunkSize > 128 * 1024 * 1024 ) {
20
35
throw ParameterError . outOfRange
21
36
} else if ( chunkSize < 5 * 1024 * 1024 ) {
@@ -40,25 +55,25 @@ public class ApiVideoClient {
40
55
}
41
56
}
42
57
43
- static func isValidVersion( version: String ) -> Bool {
58
+ static func isValidVersion( _ version: String ) -> Bool {
44
59
let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"#
45
60
return isValid ( pattern: pattern, field: version)
46
61
}
47
62
48
- static func isValidName( name: String ) -> Bool {
63
+ static func isValidName( _ name: String ) -> Bool {
49
64
let pattern = #"^[\w\-]{1,50}$"#
50
65
return isValid ( pattern: pattern, field: name)
51
66
}
52
67
53
68
static func setName( key: String , name: String , version: String ) throws {
54
- if ( !isValidName( name: name ) ) {
69
+ if ( !isValidName( name) ) {
55
70
throw ParameterError . invalidName
56
71
}
57
72
58
- if ( !isValidVersion( version: version ) ) {
73
+ if ( !isValidVersion( version) ) {
59
74
throw ParameterError . invalidVersion
60
75
}
61
- ApiVideoClient . customHeaders [ key] = name + " : " + version
76
+ ApiVideoClient . defaultHeaders [ key] = name + " : " + version
62
77
}
63
78
64
79
public static func setSdkName( name: String , version: String ) throws {
@@ -68,17 +83,19 @@ public class ApiVideoClient {
68
83
public static func setApplicationName( name: String , version: String ) throws {
69
84
try setName ( key: " AV-Origin-App " , name: name, version: version)
70
85
}
71
-
72
86
}
73
87
74
88
open class RequestBuilder < T> {
89
+ var credential : URLCredential ?
75
90
var headers : [ String : String ]
76
91
public var parameters : [ String : Any ] ?
77
92
public let method : String
78
93
public let URLString : String
79
94
public let requestTask : RequestTask = RequestTask ( )
80
95
81
96
/// Optional block to obtain a reference to the request's progress instance when available.
97
+ /// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
98
+ /// If you need to get the request's progress in older OS versions, please use Alamofire http client.
82
99
public var onProgressReady : ( ( Progress ) -> Void ) ?
83
100
84
101
required public init ( method: String , URLString: String , parameters: [ String : Any ] ? , headers: [ String : String ] = [ : ] , onProgressReady: ( ( Progress ) -> Void ) ? = nil ) {
@@ -108,6 +125,11 @@ open class RequestBuilder<T> {
108
125
}
109
126
return self
110
127
}
128
+
129
+ open func addCredential( ) -> Self {
130
+ credential = ApiVideoClient . credential
131
+ return self
132
+ }
111
133
}
112
134
113
135
public protocol RequestBuilderFactory {
0 commit comments