Skip to content

Commit 445f003

Browse files
author
Clément Le Provost
authored
Add the OS version in the User-Agent header (#112)
1 parent e0db7c6 commit 445f003

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

Source/Client.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ public let ErrorDomain = "AlgoliaSearch"
146146
@objc public init(appID: String, apiKey: String) {
147147
self.appID = appID
148148
self.apiKey = apiKey
149-
let version = NSBundle(forClass: self.dynamicType).infoDictionary!["CFBundleShortVersionString"] as! String
150-
self.userAgents = [ LibraryVersion(name: "Algolia for Swift", version: version) ]
151149

152150
// Initialize hosts to their default values.
153151
//
@@ -166,6 +164,7 @@ public let ErrorDomain = "AlgoliaSearch"
166164
writeHosts = [ "\(appID).algolia.net" ] + fallbackHosts
167165

168166
// WARNING: Those headers cannot be changed for the lifetime of the session.
167+
// Other headers are likely to change during the lifetime of the session: they will be passed at every request.
169168
let fixedHTTPHeaders = [
170169
"X-Algolia-Application-Id": self.appID
171170
]
@@ -175,7 +174,22 @@ public let ErrorDomain = "AlgoliaSearch"
175174

176175
super.init()
177176

178-
// Other headers are likely to change during the lifetime of the session: they will be passed for every request.
177+
// Add this library's version to the user agents.
178+
let version = NSBundle(forClass: self.dynamicType).infoDictionary!["CFBundleShortVersionString"] as! String
179+
self.userAgents = [ LibraryVersion(name: "Algolia for Swift", version: version) ]
180+
181+
// Add the operating system's version to the user agents.
182+
if #available(iOS 8.0, OSX 10.0, tvOS 9.0, *) {
183+
let osVersion = NSProcessInfo.processInfo().operatingSystemVersion
184+
var osVersionString = "\(osVersion.majorVersion).\(osVersion.minorVersion)"
185+
if osVersion.patchVersion != 0 {
186+
osVersionString += ".\(osVersion.patchVersion)"
187+
}
188+
if let osName = getOSName() {
189+
self.userAgents.append(LibraryVersion(name: osName, version: osVersionString))
190+
}
191+
}
192+
179193
// WARNING: `didSet` not called during initialization => we need to update the headers manually here.
180194
updateHeadersFromAPIKey()
181195
updateHeadersFromUserAgents()

Source/Helpers.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ extension MutableCollectionType where Index == Int {
102102
}
103103
}
104104
}
105+
106+
// MARK: - Miscellaneous
107+
108+
/// Get the operating system's name.
109+
///
110+
/// - returns: The operating system's name, or nil if it could not be determined.
111+
///
112+
func getOSName() -> String? {
113+
#if os(iOS)
114+
return "iOS"
115+
#elseif os(OSX)
116+
return "macOS"
117+
#elseif os(tvOS)
118+
return "tvOS"
119+
#elseif os(watchOS)
120+
return "watchOS"
121+
#else
122+
return nil
123+
#endif
124+
}

Source/Network.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension NSURLSession: URLSession {
4242
}
4343

4444

45-
#if DEBUG
45+
#if os(iOS) && DEBUG
4646

4747
import CoreTelephony
4848
import SystemConfiguration

Tests/ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class ClientTests: XCTestCase {
406406

407407
func testUserAgentHeader() {
408408
// Test that the initial value of the header is correct.
409-
XCTAssert(client.headers["User-Agent"]?.rangeOfString("^Algolia for Swift \\([0-9.]+\\)$", options: .RegularExpressionSearch) != nil)
409+
XCTAssert(client.headers["User-Agent"]?.rangeOfString("^Algolia for Swift \\([0-9.]+\\); (iOS|macOS|tvOS) \\([0-9.]+\\)$", options: .RegularExpressionSearch) != nil)
410410

411411
// Test that changing the user agents results in a proper format.
412412
client.userAgents = [

0 commit comments

Comments
 (0)