Skip to content

Commit

Permalink
feat(Insights): add user-agent extension (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavFitz committed Feb 12, 2023
1 parent 27ab59c commit 42b5d08
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "apple/swift-log" ~> 1.4
github "algolia/algoliasearch-client-swift" ~> 8.15
github "algolia/algoliasearch-client-swift" ~> 8.17
github "algolia/instantsearch-telemetry-native" ~> 0.1.3
4 changes: 2 additions & 2 deletions InstantSearch.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Pod::Spec.new do |s|

s.subspec "Insights" do |ss|
ss.source_files = 'Sources/InstantSearchInsights/**/*.{swift}'
ss.dependency 'AlgoliaSearchClient', '~> 8.15'
ss.dependency 'AlgoliaSearchClient', '~> 8.17'
ss.ios.deployment_target = '9.0'
ss.osx.deployment_target = '10.10'
ss.watchos.deployment_target = '2.0'
Expand All @@ -24,7 +24,7 @@ Pod::Spec.new do |s|

s.subspec "Core" do |ss|
ss.source_files = 'Sources/InstantSearchCore/**/*.{swift}'
ss.dependency 'AlgoliaSearchClient', '~> 8.15'
ss.dependency 'AlgoliaSearchClient', '~> 8.17'
ss.dependency 'InstantSearch/Insights'
ss.dependency 'InstantSearchTelemetry', '~> 0.1.3'
ss.ios.deployment_target = '9.0'
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let package = Package(
dependencies: [
.package(name: "AlgoliaSearchClient",
url: "https://github.com/algolia/algoliasearch-client-swift",
from: "8.15.0"),
from: "8.17.0"),
.package(name: "InstantSearchTelemetry",
url: "https://github.com/algolia/instantsearch-telemetry-native",
from: "0.1.3")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// UserAgentSetter.swift
// CoreUserAgentSetter.swift
//
//
// Created by Vladislav Fitc on 16/06/2020.
//

import Foundation

struct UserAgentSetter {
struct CoreUserAgentSetter {
/// Add the library's version to the client's user agents, if not already present.
static let set = Self()

Expand Down
2 changes: 1 addition & 1 deletion Sources/InstantSearchCore/Searcher/Searcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public protocol ErrorObservable {
extension Searcher {
/// Add the library's version to the client's user agents, if not already present.
func updateClientUserAgents() {
_ = UserAgentSetter.set
_ = CoreUserAgentSetter.set
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// InsightsUserAgentSetter.swift
//
//
// Created by Vladislav Fitc on 03/02/2023.
//

import AlgoliaSearchClient
import Foundation

struct InsightsUserAgentSetter {
/// Add the library's version to the client's user agents, if not already present.
static let set = Self()

init() {
UserAgentController.append(UserAgent(title: "Algolia insights for iOS", version: Version.current.description))
}
}
1 change: 1 addition & 0 deletions Sources/InstantSearchInsights/Logic/Insights.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public class Insights {
userToken: UserToken? = .none,
generateTimestamps: Bool = true,
region: Region? = region) -> Insights {
_ = InsightsUserAgentSetter.set
let logger = Logger(label: "Insights (\(appId.rawValue))")
logger.info("application registered")
let insights = Insights(applicationID: appId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import InstantSearchTelemetry

public func reload() {
if let hitsSource = hitsSource, hitsSource.numberOfHits() > 0 {
self.hits = hitsSource.hits
hits = hitsSource.hits
} else {
self.hits = []
hits = []
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OnlineTestCase: XCTestCase {
throw Error.missingCredentials
}

_ = UserAgentSetter.set
_ = CoreUserAgentSetter.set

client = SearchClient(appID: credentials.applicationID, apiKey: credentials.apiKey)

Expand Down
19 changes: 19 additions & 0 deletions Tests/InstantSearchInsightsTests/Unit/UserAgentTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UserAgentTest.swift
//
//
// Created by Vladislav Fitc on 03/02/2023.
//

import AlgoliaSearchClient
@testable import InstantSearchInsights
import XCTest

class UserAgentTest: XCTestCase {
func testUserAgentUniqueness() {
Insights.register(appId: "a", apiKey: "a")
Insights.register(appId: "b", apiKey: "b")
Insights.register(appId: "c", apiKey: "c")
XCTAssertEqual(UserAgentController.extensions.filter { $0.userAgentExtension.contains("Algolia insights for iOS") }.count, 1)
}
}

0 comments on commit 42b5d08

Please sign in to comment.