66
77import Foundation
88public class ApiVideoUploader {
9- public static var apiKey : String ? = nil
9+ private static var apiKey : String ? = nil
1010 public static var basePath = " https://ws.api.video "
11- internal static var customHeaders : [ String : String ] = [ " AV-Origin-Client " : " ios-uploader:1.1.1 " ]
11+ internal static var defaultHeaders : [ String : String ] = [ " AV-Origin-Client " : " ios-uploader:1.1.1 " ]
12+ internal static var credential : URLCredential ?
1213 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 ( )
1515 public static var apiResponseQueue : DispatchQueue = . main
1616 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+ }
1732
18- public static func setChunkSize( chunkSize: Int ) throws {
33+ public static func setChunkSize( _ chunkSize: Int ) throws {
1934 if ( chunkSize > 128 * 1024 * 1024 ) {
2035 throw ParameterError . outOfRange
2136 } else if ( chunkSize < 5 * 1024 * 1024 ) {
@@ -40,25 +55,25 @@ public class ApiVideoUploader {
4055 }
4156 }
4257
43- static func isValidVersion( version: String ) -> Bool {
58+ static func isValidVersion( _ version: String ) -> Bool {
4459 let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"#
4560 return isValid ( pattern: pattern, field: version)
4661 }
4762
48- static func isValidName( name: String ) -> Bool {
63+ static func isValidName( _ name: String ) -> Bool {
4964 let pattern = #"^[\w\-]{1,50}$"#
5065 return isValid ( pattern: pattern, field: name)
5166 }
5267
5368 static func setName( key: String , name: String , version: String ) throws {
54- if ( !isValidName( name: name ) ) {
69+ if ( !isValidName( name) ) {
5570 throw ParameterError . invalidName
5671 }
5772
58- if ( !isValidVersion( version: version ) ) {
73+ if ( !isValidVersion( version) ) {
5974 throw ParameterError . invalidVersion
6075 }
61- ApiVideoUploader . customHeaders [ key] = name + " : " + version
76+ ApiVideoUploader . defaultHeaders [ key] = name + " : " + version
6277 }
6378
6479 public static func setSdkName( name: String , version: String ) throws {
@@ -68,17 +83,19 @@ public class ApiVideoUploader {
6883 public static func setApplicationName( name: String , version: String ) throws {
6984 try setName ( key: " AV-Origin-App " , name: name, version: version)
7085 }
71-
7286}
7387
7488open class RequestBuilder < T> {
89+ var credential : URLCredential ?
7590 var headers : [ String : String ]
7691 public var parameters : [ String : Any ] ?
7792 public let method : String
7893 public let URLString : String
7994 public let requestTask : RequestTask = RequestTask ( )
8095
8196 /// 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.
8299 public var onProgressReady : ( ( Progress ) -> Void ) ?
83100
84101 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> {
108125 }
109126 return self
110127 }
128+
129+ open func addCredential( ) -> Self {
130+ credential = ApiVideoUploader . credential
131+ return self
132+ }
111133}
112134
113135public protocol RequestBuilderFactory {
0 commit comments