Skip to content

Commit f7f7091

Browse files
authored
feat(swift5): use Basic auth instead of Bearer
1 parent fc956f5 commit f7f7091

19 files changed

+877
-586
lines changed

.openapi-generator/FILES

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ Sources/APIs/UploadTokensAPI.swift
1515
Sources/APIs/VideosAPI.swift
1616
Sources/APIs/WatermarksAPI.swift
1717
Sources/APIs/WebhooksAPI.swift
18-
Sources/AlamofireImplementations.swift
19-
Sources/Auth/ApiVideoAuthenticator.swift
20-
Sources/Auth/ApiVideoCredential.swift
2118
Sources/CodableHelper.swift
2219
Sources/Configuration.swift
2320
Sources/Extensions.swift
@@ -99,6 +96,7 @@ Sources/Models/WebhooksCreationPayload.swift
9996
Sources/Models/WebhooksListResponse.swift
10097
Sources/OpenISO8601DateFormatter.swift
10198
Sources/SynchronizedDictionary.swift
99+
Sources/URLSessionImplementations.swift
102100
Sources/Upload/FileChunkInputStream.swift
103101
Sources/Upload/ProgressiveUploadSessionProtocol.swift
104102
Sources/Upload/RequestTaskQueue.swift

ApiVideoClient.podspec

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Pod::Spec.new do |s|
22
s.name = 'ApiVideoClient'
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.1'
@@ -13,5 +13,4 @@ Pod::Spec.new do |s|
1313
s.summary = 'The official Swift api.video client 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

-1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// ViewController.swift
3+
// Example
4+
//
5+
6+
import UIKit
7+
import ApiVideoClient
8+
9+
struct VideosOption{
10+
let title:String
11+
let videoId: String
12+
let thumbnail: String?
13+
let handler: (()->Void)
14+
}
15+
16+
class VideosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
17+
var models = [VideosOption]()
18+
19+
private func configure(){
20+
21+
ApiVideoClient.setApiKey(ClientManager.apiKey)
22+
ApiVideoClient.basePath = ClientManager.environment.rawValue
23+
24+
VideosAPI.list(title: nil, tags: nil, metadata: nil, description: nil, liveStreamId: nil, sortBy: nil, sortOrder: nil, currentPage: nil, pageSize: nil) { (response, error) in
25+
guard error == nil else {
26+
print(error ?? "error")
27+
return
28+
}
29+
30+
if ((response) != nil) {
31+
for item in response!.data {
32+
self.models.append(VideosOption(title: item.title ?? "error title", videoId: item.videoId, thumbnail: item.assets?.thumbnail){
33+
34+
})
35+
self.tableView.reloadData()
36+
}
37+
}
38+
}
39+
}
40+
41+
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
42+
let headerView = UIView()
43+
headerView.backgroundColor = UIColor.clear
44+
return headerView
45+
}
46+
47+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
48+
return models.count
49+
}
50+
51+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
52+
let model = models[indexPath.row]
53+
54+
55+
guard let cell = tableView.dequeueReusableCell(withIdentifier: VideoTableViewCell.identifier, for: indexPath) as? VideoTableViewCell else{
56+
return UITableViewCell()
57+
}
58+
cell.layer.cornerRadius = 8
59+
cell.configure(with: model)
60+
return cell
61+
}
62+
63+
64+
private let tableView: UITableView = {
65+
let table = UITableView(frame: .zero, style: .grouped)
66+
table.register(VideoTableViewCell.self, forCellReuseIdentifier: VideoTableViewCell.identifier)
67+
return table
68+
}()
69+
70+
override func viewDidLoad() {
71+
super.viewDidLoad()
72+
configure()
73+
title = "Videos"
74+
view.addSubview(tableView)
75+
tableView.delegate = self
76+
tableView.dataSource = self
77+
tableView.frame = view.bounds
78+
tableView.rowHeight = 310.0
79+
}
80+
81+
82+
83+
84+
}
85+

Example/Example/MainViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ extension MainViewController: UIImagePickerControllerDelegate, UINavigationContr
157157
progressView.isHidden = false
158158

159159
// Set client configuration
160-
ApiVideoClient.apiKey = SettingsManager.apiKey
160+
ApiVideoClient.setApiKey(SettingsManager.apiKey)
161161
ApiVideoClient.basePath = SettingsManager.environment.rawValue
162162

163163
// Upload

Package.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import PackageDescription
55
let package = Package(
66
name: "ApiVideoClient",
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: "ApiVideoClient",
30-
dependencies: ["AnyCodable", "Alamofire", ],
29+
dependencies: ["AnyCodable", ],
3130
path: "Sources"
3231
),
3332
// Targets for tests

Sources/APIs.swift

+33-11
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,31 @@
66

77
import Foundation
88
public class ApiVideoClient {
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:1.2.1"]
11+
internal static var defaultHeaders:[String: String] = ["AV-Origin-Client": "swift:1.2.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 ApiVideoClient {
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-
ApiVideoClient.customHeaders[key] = name + ":" + version
76+
ApiVideoClient.defaultHeaders[key] = name + ":" + version
6277
}
6378

6479
public static func setSdkName(name: String, version: String) throws {
@@ -68,17 +83,19 @@ public class ApiVideoClient {
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 = ApiVideoClient.credential
131+
return self
132+
}
111133
}
112134

113135
public protocol RequestBuilderFactory {

0 commit comments

Comments
 (0)