diff --git a/.openapi-generator/FILES b/.openapi-generator/FILES index ccad4c1..a1cc86b 100644 --- a/.openapi-generator/FILES +++ b/.openapi-generator/FILES @@ -7,9 +7,6 @@ Sources/APIHelper.swift Sources/APIs.swift Sources/APIs/AdvancedAuthenticationAPI.swift Sources/APIs/VideosAPI.swift -Sources/AlamofireImplementations.swift -Sources/Auth/ApiVideoAuthenticator.swift -Sources/Auth/ApiVideoCredential.swift Sources/CodableHelper.swift Sources/Configuration.swift Sources/Extensions.swift @@ -31,6 +28,7 @@ Sources/Models/VideoSourceLiveStream.swift Sources/Models/VideoSourceLiveStreamLink.swift Sources/OpenISO8601DateFormatter.swift Sources/SynchronizedDictionary.swift +Sources/URLSessionImplementations.swift Sources/Upload/FileChunkInputStream.swift Sources/Upload/ProgressiveUploadSessionProtocol.swift Sources/Upload/RequestTaskQueue.swift diff --git a/ApiVideoUploader.podspec b/ApiVideoUploader.podspec index aab0b94..3bded24 100644 --- a/ApiVideoUploader.podspec +++ b/ApiVideoUploader.podspec @@ -1,8 +1,8 @@ Pod::Spec.new do |s| s.name = 'ApiVideoUploader' - s.ios.deployment_target = '10.0' - s.osx.deployment_target = '10.12' - s.tvos.deployment_target = '10.0' + s.ios.deployment_target = '9.0' + s.osx.deployment_target = '10.11' + s.tvos.deployment_target = '9.0' # Add back when CocoaPods/CocoaPods#11558 is released #s.watchos.deployment_target = '3.0' s.version = '1.2.2' @@ -13,5 +13,4 @@ Pod::Spec.new do |s| s.summary = 'The official Swift api.video uploader for iOS, macOS and tvOS' s.source_files = 'Sources/**/*.swift' s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1' - s.dependency 'Alamofire', '~> 5.4.3' end diff --git a/Cartfile b/Cartfile index 4fdc3a6..3f7e630 100644 --- a/Cartfile +++ b/Cartfile @@ -1,2 +1 @@ github "Flight-School/AnyCodable" ~> 0.6.1 -github "Alamofire/Alamofire" ~> 5.4.3 diff --git a/Package.swift b/Package.swift index ed08137..714471d 100644 --- a/Package.swift +++ b/Package.swift @@ -5,9 +5,9 @@ import PackageDescription let package = Package( name: "ApiVideoUploader", platforms: [ - .iOS(.v10), - .macOS(.v10_12), - .tvOS(.v10), + .iOS(.v9), + .macOS(.v10_11), + .tvOS(.v9), .watchOS(.v3), ], products: [ @@ -20,14 +20,13 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"), - .package(url: "https://github.com/Alamofire/Alamofire", from: "5.4.3"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages which this package depends on. .target( name: "ApiVideoUploader", - dependencies: ["AnyCodable", "Alamofire", ], + dependencies: ["AnyCodable", ], path: "Sources" ), // Targets for tests diff --git a/Sources/APIs.swift b/Sources/APIs.swift index 5ba2602..38e1a2b 100644 --- a/Sources/APIs.swift +++ b/Sources/APIs.swift @@ -6,16 +6,33 @@ import Foundation public class ApiVideoUploader { - public static var apiKey: String? = nil + private static var apiKey: String? = nil public static var basePath = "https://ws.api.video" - internal static var customHeaders:[String: String] = ["AV-Origin-Client": "swift-uploader:1.2.2"] + internal static var defaultHeaders:[String: String] = ["AV-Origin-Client": "swift-uploader:1.2.2"] + internal static var credential: URLCredential? private static var chunkSize: Int = 50 * 1024 * 1024 - internal static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory() - internal static var credential = ApiVideoCredential() + internal static var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory() public static var apiResponseQueue: DispatchQueue = .main + + public static var backgroundIdentifier: String = "video.api.upload.background" public static var timeout: TimeInterval = 60 + internal static var customHeaders:[String: String] { + var headers = defaultHeaders + if let apiKey = apiKey { + headers["Authorization"] = apiKey + } + return headers + } + + public static func setApiKey(_ apiKey: String?) { + if let apiKey = apiKey { + self.apiKey = "Basic " + "\(apiKey):".toBase64() + } else { + self.apiKey = nil + } + } - public static func setChunkSize(chunkSize: Int) throws { + public static func setChunkSize(_ chunkSize: Int) throws { if (chunkSize > 128 * 1024 * 1024) { throw ParameterError.outOfRange } else if (chunkSize < 5 * 1024 * 1024) { @@ -40,25 +57,25 @@ public class ApiVideoUploader { } } - static func isValidVersion(version: String) -> Bool { + static func isValidVersion(_ version: String) -> Bool { let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"# return isValid(pattern: pattern, field: version) } - static func isValidName(name: String) -> Bool { + static func isValidName(_ name: String) -> Bool { let pattern = #"^[\w\-]{1,50}$"# return isValid(pattern: pattern, field: name) } static func setName(key: String, name: String, version: String) throws { - if(!isValidName(name: name)) { + if(!isValidName(name)) { throw ParameterError.invalidName } - if(!isValidVersion(version: version)) { + if(!isValidVersion(version)) { throw ParameterError.invalidVersion } - ApiVideoUploader.customHeaders[key] = name + ":" + version + ApiVideoUploader.defaultHeaders[key] = name + ":" + version } public static func setSdkName(name: String, version: String) throws { @@ -68,10 +85,10 @@ public class ApiVideoUploader { public static func setApplicationName(name: String, version: String) throws { try setName(key: "AV-Origin-App", name: name, version: version) } - } open class RequestBuilder { + var credential: URLCredential? var headers: [String: String] public var parameters: [String: Any]? public let method: String @@ -79,6 +96,8 @@ open class RequestBuilder { public let requestTask: RequestTask = RequestTask() /// Optional block to obtain a reference to the request's progress instance when available. + /// 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. + /// If you need to get the request's progress in older OS versions, please use Alamofire http client. public var onProgressReady: ((Progress) -> Void)? required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], onProgressReady: ((Progress) -> Void)? = nil) { @@ -108,9 +127,15 @@ open class RequestBuilder { } return self } + + open func addCredential() -> Self { + credential = ApiVideoUploader.credential + return self + } } public protocol RequestBuilderFactory { func getNonDecodableBuilder() -> RequestBuilder.Type func getBuilder() -> RequestBuilder.Type + func getBackgroundBuilder() -> RequestBuilder.Type } diff --git a/Sources/APIs/VideosAPI.swift b/Sources/APIs/VideosAPI.swift index 925add5..4afb8ca 100644 --- a/Sources/APIs/VideosAPI.swift +++ b/Sources/APIs/VideosAPI.swift @@ -144,7 +144,7 @@ The latter allows you to split a video source into X chunks and send those chunk let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders) - let localVariableRequestBuilder: RequestBuilder