Skip to content

Commit

Permalink
feat(swift5): use Basic auth instead of Bearer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee authored Mar 10, 2023
1 parent 1da5549 commit 7aab0ec
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 588 deletions.
4 changes: 1 addition & 3 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ Sources/APIs/UploadTokensAPI.swift
Sources/APIs/VideosAPI.swift
Sources/APIs/WatermarksAPI.swift
Sources/APIs/WebhooksAPI.swift
Sources/AlamofireImplementations.swift
Sources/Auth/ApiVideoAuthenticator.swift
Sources/Auth/ApiVideoCredential.swift
Sources/CodableHelper.swift
Sources/Configuration.swift
Sources/Extensions.swift
Expand Down Expand Up @@ -95,6 +92,7 @@ Sources/Models/WebhooksCreationPayload.swift
Sources/Models/WebhooksListResponse.swift
Sources/OpenISO8601DateFormatter.swift
Sources/SynchronizedDictionary.swift
Sources/URLSessionImplementations.swift
Sources/Upload/FileChunkInputStream.swift
Sources/Upload/ProgressiveUploadSessionProtocol.swift
Sources/Upload/RequestTaskQueue.swift
Expand Down
7 changes: 3 additions & 4 deletions ApiVideoClient.podspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Pod::Spec.new do |s|
s.name = 'ApiVideoClient'
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.1.1'
Expand All @@ -13,5 +13,4 @@ Pod::Spec.new do |s|
s.summary = 'The official iOS client library for api.video'
s.source_files = 'Sources/**/*.swift'
s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1'
s.dependency 'Alamofire', '~> 5.4.3'
end
1 change: 0 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
github "Flight-School/AnyCodable" ~> 0.6.1
github "Alamofire/Alamofire" ~> 5.4.3
2 changes: 1 addition & 1 deletion Example/Example/Controller/VideosViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class VideosViewController: UIViewController, UITableViewDelegate, UITableViewDa

private func configure(){

ApiVideoClient.apiKey = ClientManager.apiKey
ApiVideoClient.setApiKey(ClientManager.apiKey)
ApiVideoClient.basePath = ClientManager.environment.rawValue

VideosAPI.list(title: nil, tags: nil, metadata: nil, description: nil, liveStreamId: nil, sortBy: nil, sortOrder: nil, currentPage: nil, pageSize: nil) { (response, error) in
Expand Down
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import PackageDescription
let package = Package(
name: "ApiVideoClient",
platforms: [
.iOS(.v10),
.macOS(.v10_12),
.tvOS(.v10),
.iOS(.v9),
.macOS(.v10_11),
.tvOS(.v9),
.watchOS(.v3),
],
products: [
Expand All @@ -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: "ApiVideoClient",
dependencies: ["AnyCodable", "Alamofire", ],
dependencies: ["AnyCodable", ],
path: "Sources"
),
// Targets for tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Please follow the [installation](#installation) instruction and execute the foll
```swift
import ApiVideoClient

ApiVideoClient.apiKey = "YOUR_API_KEY"
ApiVideoClient.setApiKey("YOUR_API_KEY")
// if you rather like to use the sandbox environment:
// ApiVideoClient.basePath = Environment.sandbox.rawValue

Expand Down Expand Up @@ -346,7 +346,7 @@ Method | HTTP request | Description
Most endpoints required to be authenticated using the API key mechanism described in our [documentation](https://docs.api.video/reference#authentication).
The access token generation mechanism is automatically handled by the client. All you have to do is provide an API key:
```swift
ApiVideoClient.apiKey = YOUR_API_KEY
ApiVideoClient.setApiKey(YOUR_API_KEY)
```

### Public endpoints
Expand Down
44 changes: 33 additions & 11 deletions Sources/APIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,31 @@

import Foundation
public class ApiVideoClient {
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": "ios:1.1.1"]
internal static var defaultHeaders:[String: String] = ["AV-Origin-Client": "ios:1.1.1"]
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 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) {
Expand All @@ -40,25 +55,25 @@ public class ApiVideoClient {
}
}

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
}
ApiVideoClient.customHeaders[key] = name + ":" + version
ApiVideoClient.defaultHeaders[key] = name + ":" + version
}

public static func setSdkName(name: String, version: String) throws {
Expand All @@ -68,17 +83,19 @@ public class ApiVideoClient {
public static func setApplicationName(name: String, version: String) throws {
try setName(key: "AV-Origin-App", name: name, version: version)
}

}

open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String: String]
public var parameters: [String: Any]?
public let method: String
public let URLString: String
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) {
Expand Down Expand Up @@ -108,6 +125,11 @@ open class RequestBuilder<T> {
}
return self
}

open func addCredential() -> Self {
credential = ApiVideoClient.credential
return self
}
}

public protocol RequestBuilderFactory {
Expand Down
Loading

0 comments on commit 7aab0ec

Please sign in to comment.