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 " : " swift-uploader:1.2.2 " ]
11+ internal static var defaultHeaders : [ String : String ] = [ " AV-Origin-Client " : " swift-uploader:1.2.2 " ]
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
16+
17+ public static var backgroundIdentifier : String = " video.api.upload.background "
1618 public static var timeout : TimeInterval = 60
19+ internal static var customHeaders : [ String : String ] {
20+ var headers = defaultHeaders
21+ if let apiKey = apiKey {
22+ headers [ " Authorization " ] = apiKey
23+ }
24+ return headers
25+ }
26+
27+ public static func setApiKey( _ apiKey: String ? ) {
28+ if let apiKey = apiKey {
29+ self . apiKey = " Basic " + " \( apiKey) : " . toBase64 ( )
30+ } else {
31+ self . apiKey = nil
32+ }
33+ }
1734
18- public static func setChunkSize( chunkSize: Int ) throws {
35+ public static func setChunkSize( _ chunkSize: Int ) throws {
1936 if ( chunkSize > 128 * 1024 * 1024 ) {
2037 throw ParameterError . outOfRange
2138 } else if ( chunkSize < 5 * 1024 * 1024 ) {
@@ -40,25 +57,25 @@ public class ApiVideoUploader {
4057 }
4158 }
4259
43- static func isValidVersion( version: String ) -> Bool {
60+ static func isValidVersion( _ version: String ) -> Bool {
4461 let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"#
4562 return isValid ( pattern: pattern, field: version)
4663 }
4764
48- static func isValidName( name: String ) -> Bool {
65+ static func isValidName( _ name: String ) -> Bool {
4966 let pattern = #"^[\w\-]{1,50}$"#
5067 return isValid ( pattern: pattern, field: name)
5168 }
5269
5370 static func setName( key: String , name: String , version: String ) throws {
54- if ( !isValidName( name: name ) ) {
71+ if ( !isValidName( name) ) {
5572 throw ParameterError . invalidName
5673 }
5774
58- if ( !isValidVersion( version: version ) ) {
75+ if ( !isValidVersion( version) ) {
5976 throw ParameterError . invalidVersion
6077 }
61- ApiVideoUploader . customHeaders [ key] = name + " : " + version
78+ ApiVideoUploader . defaultHeaders [ key] = name + " : " + version
6279 }
6380
6481 public static func setSdkName( name: String , version: String ) throws {
@@ -68,17 +85,19 @@ public class ApiVideoUploader {
6885 public static func setApplicationName( name: String , version: String ) throws {
6986 try setName ( key: " AV-Origin-App " , name: name, version: version)
7087 }
71-
7288}
7389
7490open class RequestBuilder < T> {
91+ var credential : URLCredential ?
7592 var headers : [ String : String ]
7693 public var parameters : [ String : Any ] ?
7794 public let method : String
7895 public let URLString : String
7996 public let requestTask : RequestTask = RequestTask ( )
8097
8198 /// Optional block to obtain a reference to the request's progress instance when available.
99+ /// 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.
100+ /// If you need to get the request's progress in older OS versions, please use Alamofire http client.
82101 public var onProgressReady : ( ( Progress ) -> Void ) ?
83102
84103 required public init ( method: String , URLString: String , parameters: [ String : Any ] ? , headers: [ String : String ] = [ : ] , onProgressReady: ( ( Progress ) -> Void ) ? = nil ) {
@@ -108,6 +127,11 @@ open class RequestBuilder<T> {
108127 }
109128 return self
110129 }
130+
131+ open func addCredential( ) -> Self {
132+ credential = ApiVideoUploader . credential
133+ return self
134+ }
111135}
112136
113137public protocol RequestBuilderFactory {
0 commit comments