6
6
7
7
import Foundation
8
8
enum ApiVideoClientError : Error {
9
- case invalidApplicationName
10
- case invalidApplicationVersion
11
- case missingApplicationName
9
+ case invalidName
10
+ case invalidVersion
12
11
}
13
12
14
13
public class ApiVideoClient {
15
-
16
14
public static var apiKey : String ? = nil
17
15
public static var basePath = " https://ws.api.video "
18
- internal static var customHeaders : [ String : String ] = [ " AV-Origin-Client " : " ios:1.0.4 " ]
16
+ internal static var customHeaders : [ String : String ] = [ " AV-Origin-Client " : " ios:1.0.5 " ]
19
17
private static var chunkSize : Int = 50 * 1024 * 1024
20
18
internal static var requestBuilderFactory : RequestBuilderFactory = AlamofireRequestBuilderFactory ( )
21
19
internal static var credential = ApiVideoCredential ( )
@@ -35,36 +33,44 @@ public class ApiVideoClient {
35
33
return ApiVideoClient . chunkSize
36
34
}
37
35
38
-
39
- public static func setApplicationName( applicationName: String , applicationVersion: String ? ) throws {
40
- if ( applicationName. isEmpty) {
41
- if ( applicationVersion != nil && !applicationVersion!. isEmpty) {
42
- throw ApiVideoClientError . missingApplicationName
43
- }
44
- ApiVideoClient . customHeaders [ " AV-Origin-App " ] = nil
45
- return
46
- }
47
-
48
- let pattern = #"^[\w\-]{1,50}$"#
36
+ static func isValid( pattern: String , field: String ) -> Bool {
49
37
let regex = try ! NSRegularExpression ( pattern: pattern, options: . anchorsMatchLines)
50
- let stringRange = NSRange ( location: 0 , length: applicationName . utf16. count)
51
- let matches = regex. matches ( in: applicationName , range: stringRange)
38
+ let stringRange = NSRange ( location: 0 , length: field . utf16. count)
39
+ let matches = regex. matches ( in: field , range: stringRange)
52
40
if ( matches. isEmpty) {
53
- throw ApiVideoClientError . invalidApplicationName
41
+ return false
42
+ } else {
43
+ return true
54
44
}
45
+ }
46
+
47
+ static func isValidVersion( version: String ) -> Bool {
48
+ let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"#
49
+ return isValid ( pattern: pattern, field: version)
50
+ }
55
51
56
- if ( applicationVersion != nil && !applicationVersion!. isEmpty) {
57
- let pattern2 = #"^[\w\-]{1,50}$"#
58
- let regex2 = try ! NSRegularExpression ( pattern: pattern2, options: . anchorsMatchLines)
59
- let stringRange2 = NSRange ( location: 0 , length: applicationVersion!. utf16. count)
60
- let matches2 = regex2. matches ( in: applicationVersion!, range: stringRange2)
61
- if ( matches2. isEmpty) {
62
- throw ApiVideoClientError . invalidApplicationVersion
63
- }
64
- ApiVideoClient . customHeaders [ " AV-Origin-App " ] = applicationName + " : " + applicationVersion!
65
- return
52
+ static func isValidName( name: String ) -> Bool {
53
+ let pattern = #"^[\w\-]{1,50}$"#
54
+ return isValid ( pattern: pattern, field: name)
55
+ }
56
+
57
+ static func setName( key: String , name: String , version: String ) throws {
58
+ if ( !isValidName( name: name) ) {
59
+ throw ApiVideoClientError . invalidName
60
+ }
61
+
62
+ if ( !isValidVersion( version: version) ) {
63
+ throw ApiVideoClientError . invalidVersion
66
64
}
67
- ApiVideoClient . customHeaders [ " AV-Origin-App " ] = applicationName
65
+ ApiVideoClient . customHeaders [ key] = name + " : " + version
66
+ }
67
+
68
+ public static func setSdkName( name: String , version: String ) throws {
69
+ try setName ( key: " AV-Origin-Sdk " , name: name, version: version)
70
+ }
71
+
72
+ public static func setApplicationName( name: String , version: String ) throws {
73
+ try setName ( key: " AV-Origin-App " , name: name, version: version)
68
74
}
69
75
70
76
}
0 commit comments