Skip to content

Commit

Permalink
Merge pull request #33 from apivideo/feature/origin_sdk_header
Browse files Browse the repository at this point in the history
feat(ios): add origin sdk field support
  • Loading branch information
bot-api-video authored Jul 5, 2022
2 parents aa92cea + 4f82bfb commit f5958e0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions ApiVideoClient.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]' }
s.license = { :type => 'MIT' }
s.homepage = 'https://docs.api.video'
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
66 changes: 36 additions & 30 deletions Sources/APIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApiVideoClient/Integration/VideosApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Expand Down

0 comments on commit f5958e0

Please sign in to comment.