Skip to content

Commit 9669dc7

Browse files
authored
swift5: upload in background
1 parent 435cf28 commit 9669dc7

17 files changed

+849
-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: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@
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
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

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

113137
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)