Skip to content

Commit 85da812

Browse files
authored
swift5: upload in background
1 parent 435cf28 commit 85da812

17 files changed

+847
-584
lines changed

.openapi-generator/FILES

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ Sources/APIHelper.swift
77
Sources/APIs.swift
88
Sources/APIs/AdvancedAuthenticationAPI.swift
99
Sources/APIs/VideosAPI.swift
10-
Sources/AlamofireImplementations.swift
11-
Sources/Auth/ApiVideoAuthenticator.swift
12-
Sources/Auth/ApiVideoCredential.swift
1310
Sources/CodableHelper.swift
1411
Sources/Configuration.swift
1512
Sources/Extensions.swift
@@ -31,6 +28,7 @@ Sources/Models/VideoSourceLiveStream.swift
3128
Sources/Models/VideoSourceLiveStreamLink.swift
3229
Sources/OpenISO8601DateFormatter.swift
3330
Sources/SynchronizedDictionary.swift
31+
Sources/URLSessionImplementations.swift
3432
Sources/Upload/FileChunkInputStream.swift
3533
Sources/Upload/ProgressiveUploadSessionProtocol.swift
3634
Sources/Upload/RequestTaskQueue.swift

ApiVideoUploader.podspec

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Pod::Spec.new do |s|
22
s.name = 'ApiVideoUploader'
3-
s.ios.deployment_target = '10.0'
4-
s.osx.deployment_target = '10.12'
5-
s.tvos.deployment_target = '10.0'
3+
s.ios.deployment_target = '9.0'
4+
s.osx.deployment_target = '10.11'
5+
s.tvos.deployment_target = '9.0'
66
# Add back when CocoaPods/CocoaPods#11558 is released
77
#s.watchos.deployment_target = '3.0'
88
s.version = '1.2.2'
@@ -13,5 +13,4 @@ Pod::Spec.new do |s|
1313
s.summary = 'The official Swift api.video uploader for iOS, macOS and tvOS'
1414
s.source_files = 'Sources/**/*.swift'
1515
s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1'
16-
s.dependency 'Alamofire', '~> 5.4.3'
1716
end

Cartfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
github "Flight-School/AnyCodable" ~> 0.6.1
2-
github "Alamofire/Alamofire" ~> 5.4.3

Package.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import PackageDescription
55
let package = Package(
66
name: "ApiVideoUploader",
77
platforms: [
8-
.iOS(.v10),
9-
.macOS(.v10_12),
10-
.tvOS(.v10),
8+
.iOS(.v9),
9+
.macOS(.v10_11),
10+
.tvOS(.v9),
1111
.watchOS(.v3),
1212
],
1313
products: [
@@ -20,14 +20,13 @@ let package = Package(
2020
dependencies: [
2121
// Dependencies declare other packages that this package depends on.
2222
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"),
23-
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.4.3"),
2423
],
2524
targets: [
2625
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2726
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2827
.target(
2928
name: "ApiVideoUploader",
30-
dependencies: ["AnyCodable", "Alamofire", ],
29+
dependencies: ["AnyCodable", ],
3130
path: "Sources"
3231
),
3332
// Targets for tests

Sources/APIs.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,31 @@
66

77
import Foundation
88
public 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
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

7488
open 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

113135
public protocol RequestBuilderFactory {

Sources/APIs/VideosAPI.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ The latter allows you to split a video source into X chunks and send those chunk
144144

145145
let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
146146

147-
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
147+
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()
148148

149149
return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
150150
}
@@ -202,7 +202,7 @@ The latter allows you to split a video source into X chunks and send those chunk
202202

203203
let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
204204

205-
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
205+
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()
206206

207207
return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
208208
}
@@ -349,7 +349,7 @@ The latter allows you to split a video source into X chunks and send those chunk
349349

350350
let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
351351

352-
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
352+
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()
353353

354354
return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
355355
}
@@ -392,7 +392,7 @@ The latter allows you to split a video source into X chunks and send those chunk
392392

393393
let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
394394

395-
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
395+
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()
396396

397397
return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
398398
}

0 commit comments

Comments
 (0)