Skip to content

Commit 7c00e28

Browse files
authored
refactor(core): Add support for useragent to add platform info (#722)
1 parent 78035c8 commit 7c00e28

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

AmplifyPlugins/Core/AWSPluginsCore/ServiceConfiguration/AmplifyAWSServiceConfiguration.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import Foundation
99
import AWSCore
1010

1111
public class AmplifyAWSServiceConfiguration: AWSServiceConfiguration {
12+
private static let version = "1.1.0"
13+
1214
override public class func baseUserAgent() -> String! {
1315
//TODO: Retrieve this version from a centralized location:
1416
//https://github.com/aws-amplify/amplify-ios/issues/276
15-
let version = "1.1.0"
16-
let sdkName = "amplify-iOS"
17+
let platformInfo = AmplifyAWSServiceConfiguration.platformInformation()
1718
let systemName = UIDevice.current.systemName.replacingOccurrences(of: " ", with: "-")
1819
let systemVersion = UIDevice.current.systemVersion
1920
let localeIdentifier = Locale.current.identifier
20-
return "\(sdkName)/\(version) \(systemName)/\(systemVersion) \(localeIdentifier)"
21+
return "\(platformInfo) \(systemName)/\(systemVersion) \(localeIdentifier)"
2122
}
2223

2324
override public var userAgent: String {
@@ -41,3 +42,22 @@ public class AmplifyAWSServiceConfiguration: AWSServiceConfiguration {
4142
super.init(region: regionType, credentialsProvider: nil)
4243
}
4344
}
45+
46+
extension AmplifyAWSServiceConfiguration {
47+
48+
static var platformMapping: [Platform: String] = [:]
49+
50+
public static func addUserAgentPlatform(_ platform: Platform, version: String) {
51+
platformMapping[platform] = version
52+
}
53+
54+
public enum Platform: String {
55+
case flutter = "amplify-flutter"
56+
}
57+
58+
static func platformInformation() -> String {
59+
var platformTokens = platformMapping.map { "\($0.rawValue)/\($1)" }
60+
platformTokens.append("amplify-iOS/\(version)")
61+
return platformTokens.joined(separator: " ")
62+
}
63+
}

AmplifyPlugins/Core/AWSPluginsCoreTests/ServiceConfiguration/AmplifyAWSServiceConfigurationTests.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ import XCTest
1010

1111
class AmplifyAWSServiceConfigurationTests: XCTestCase {
1212
let credentialProvider = AWSAuthService().getCredentialsProvider()
13+
14+
override func tearDown() {
15+
AmplifyAWSServiceConfiguration.platformMapping = [:]
16+
}
17+
18+
/// Test initiating AmplifyAWSServiceConfiguration
19+
///
20+
/// - Given: Amplify library
21+
/// - When:
22+
/// - I call AmplifyAWSServiceConfiguration with credential provider
23+
/// - Then:
24+
/// - AmplifyAWSServiceConfiguration should be configured properly
25+
///
1326
func testInstantiation() {
1427
let currentSystemName = UIDevice.current.systemName.replacingOccurrences(of: " ", with: "-")
1528
let currentSystemVersion = UIDevice.current.systemVersion
1629
let expectedLocale = Locale.current.identifier
1730
let expectedSystem = "\(currentSystemName)/\(currentSystemVersion)"
1831

19-
let configuration = AmplifyAWSServiceConfiguration(region: .USEast1,
20-
credentialsProvider: credentialProvider)
32+
let configuration = AmplifyAWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)
2133

2234
XCTAssertNotNil(configuration.userAgent)
2335
let userAgentParts = configuration.userAgent.components(separatedBy: " ")
@@ -26,4 +38,30 @@ class AmplifyAWSServiceConfigurationTests: XCTestCase {
2638
XCTAssertEqual(expectedSystem, userAgentParts[1])
2739
XCTAssertEqual(expectedLocale, userAgentParts[2])
2840
}
41+
42+
/// Test adding a new platform to AmplifyAWSServiceConfiguration
43+
///
44+
/// - Given: Amplify library
45+
/// - When:
46+
/// - I add a new platform to the AmplifyAWSServiceConfiguration
47+
/// - Then:
48+
/// - AmplifyAWSServiceConfiguration should be configured properly with the new platform added.
49+
///
50+
func testAddNewPlatform() {
51+
AmplifyAWSServiceConfiguration.addUserAgentPlatform(.flutter, version: "1.1")
52+
let currentSystemName = UIDevice.current.systemName.replacingOccurrences(of: " ", with: "-")
53+
let currentSystemVersion = UIDevice.current.systemVersion
54+
let expectedLocale = Locale.current.identifier
55+
let expectedSystem = "\(currentSystemName)/\(currentSystemVersion)"
56+
let expectedPlatform = "\(AmplifyAWSServiceConfiguration.Platform.flutter.rawValue)/1.1"
57+
let configuration = AmplifyAWSServiceConfiguration()
58+
59+
XCTAssertNotNil(configuration.userAgent)
60+
let userAgentParts = configuration.userAgent.components(separatedBy: " ")
61+
XCTAssertEqual(4, userAgentParts.count)
62+
XCTAssertEqual(expectedPlatform, userAgentParts[0])
63+
XCTAssert(userAgentParts[1].starts(with: "amplify-iOS/"))
64+
XCTAssertEqual(expectedSystem, userAgentParts[2])
65+
XCTAssertEqual(expectedLocale, userAgentParts[3])
66+
}
2967
}

0 commit comments

Comments
 (0)