From 4f82bfbac68632837fc82c5b258078660689ef6c Mon Sep 17 00:00:00 2001 From: olivierapivideo Date: Tue, 5 Jul 2022 10:53:03 +0000 Subject: [PATCH] feat(ios): add origin sdk field support --- ApiVideoClient.podspec | 4 +- CHANGELOG.md | 3 + README.md | 4 +- Sources/APIs.swift | 66 ++++++++++--------- .../Integration/VideosApiTests.swift | 2 +- project.yml | 2 +- 6 files changed, 45 insertions(+), 36 deletions(-) diff --git a/ApiVideoClient.podspec b/ApiVideoClient.podspec index ce76519..6a48275 100644 --- a/ApiVideoClient.podspec +++ b/ApiVideoClient.podspec @@ -4,8 +4,8 @@ Pod::Spec.new do |s| s.osx.deployment_target = '10.12' s.tvos.deployment_target = '10.0' s.watchos.deployment_target = '3.0' - s.version = '1.0.4' - s.source = { :git => 'https://github.com/apivideo/api.video-ios-client', :tag => 'v1.0.4' } + s.version = '1.0.5' + s.source = { :git => 'https://github.com/apivideo/api.video-ios-client', :tag => 'v1.0.5' } s.authors = { 'Ecosystem Team' => 'ecosystem@api.video' } s.license = { :type => 'MIT' } s.homepage = 'https://docs.api.video' diff --git a/CHANGELOG.md b/CHANGELOG.md index 0671fc3..155410d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All changes to this project will be documented in this file. +## [1.0.5] - 2022-06-30 +- Add SDK origin header + ## [1.0.4] - 2022-04-21 - Fix `video.publishedAt` type diff --git a/README.md b/README.md index 4a02486..af85910 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,14 @@ api.video's iOS streamlines the coding process. Chunking files is handled for y Specify it in your `Cartfile`: ``` -github "apivideo/api.video-ios-client" ~> 1.0.4 +github "apivideo/api.video-ios-client" ~> 1.0.5 ``` Run `carthage update` ### CocoaPods -Add `pod 'ApiVideoClient', '1.0.4'` in your `Podfile` +Add `pod 'ApiVideoClient', '1.0.5'` in your `Podfile` Run `pod install` diff --git a/Sources/APIs.swift b/Sources/APIs.swift index 88ce5af..727b065 100644 --- a/Sources/APIs.swift +++ b/Sources/APIs.swift @@ -6,16 +6,14 @@ import Foundation enum ApiVideoClientError: Error { - case invalidApplicationName - case invalidApplicationVersion - case missingApplicationName + case invalidName + case invalidVersion } public class ApiVideoClient { - public static var apiKey: String? = nil public static var basePath = "https://ws.api.video" - internal static var customHeaders:[String: String] = ["AV-Origin-Client": "ios:1.0.4"] + internal static var customHeaders:[String: String] = ["AV-Origin-Client": "ios:1.0.5"] private static var chunkSize: Int = 50 * 1024 * 1024 internal static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory() internal static var credential = ApiVideoCredential() @@ -35,36 +33,44 @@ public class ApiVideoClient { return ApiVideoClient.chunkSize } - - public static func setApplicationName(applicationName: String, applicationVersion: String?) throws { - if(applicationName.isEmpty) { - if(applicationVersion != nil && !applicationVersion!.isEmpty) { - throw ApiVideoClientError.missingApplicationName - } - ApiVideoClient.customHeaders["AV-Origin-App"] = nil - return - } - - let pattern = #"^[\w\-]{1,50}$"# + static func isValid(pattern: String, field: String) -> Bool { let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) - let stringRange = NSRange(location: 0, length: applicationName.utf16.count) - let matches = regex.matches(in: applicationName, range: stringRange) + let stringRange = NSRange(location: 0, length: field.utf16.count) + let matches = regex.matches(in: field, range: stringRange) if(matches.isEmpty) { - throw ApiVideoClientError.invalidApplicationName + return false + } else { + return true } + } + + static func isValidVersion(version: String) -> Bool { + let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"# + return isValid(pattern: pattern, field: version) + } - if(applicationVersion != nil && !applicationVersion!.isEmpty) { - let pattern2 = #"^[\w\-]{1,50}$"# - let regex2 = try! NSRegularExpression(pattern: pattern2, options: .anchorsMatchLines) - let stringRange2 = NSRange(location: 0, length: applicationVersion!.utf16.count) - let matches2 = regex2.matches(in: applicationVersion!, range: stringRange2) - if(matches2.isEmpty) { - throw ApiVideoClientError.invalidApplicationVersion - } - ApiVideoClient.customHeaders["AV-Origin-App"] = applicationName + ":" + applicationVersion! - return + 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)) { + throw ApiVideoClientError.invalidName + } + + if(!isValidVersion(version: version)) { + throw ApiVideoClientError.invalidVersion } - ApiVideoClient.customHeaders["AV-Origin-App"] = applicationName + ApiVideoClient.customHeaders[key] = name + ":" + version + } + + public static func setSdkName(name: String, version: String) throws { + try setName(key: "AV-Origin-Sdk", name: name, version: version) + } + + public static func setApplicationName(name: String, version: String) throws { + try setName(key: "AV-Origin-App", name: name, version: version) } } diff --git a/Tests/ApiVideoClient/Integration/VideosApiTests.swift b/Tests/ApiVideoClient/Integration/VideosApiTests.swift index 9303d5a..4ccb3f4 100644 --- a/Tests/ApiVideoClient/Integration/VideosApiTests.swift +++ b/Tests/ApiVideoClient/Integration/VideosApiTests.swift @@ -13,7 +13,7 @@ internal class UploadTestCase: XCTestCase { try XCTSkipIf(Parameters.apiKey == "INTEGRATION_TESTS_API_KEY", "Can't get API key") ApiVideoClient.apiKey = Parameters.apiKey ApiVideoClient.basePath = Environment.sandbox.rawValue - try? ApiVideoClient.setApplicationName(applicationName: "client-integration-tests", applicationVersion: nil) + try? ApiVideoClient.setApplicationName(name: "client-integration-tests", version: "0") continueAfterFailure = false } diff --git a/project.yml b/project.yml index f47a321..2625a6b 100644 --- a/project.yml +++ b/project.yml @@ -7,7 +7,7 @@ targets: sources: [Sources] info: path: ./Info.plist - version: 1.0.4 + version: 1.0.5 settings: APPLICATION_EXTENSION_API_ONLY: true scheme: {}