From a97e9c23229d496bae5cadad5aae3c84627a7119 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Mon, 18 Nov 2024 11:14:28 -0500 Subject: [PATCH 1/2] refactored `KlaviyoRequest` to use URLComponents --- Sources/KlaviyoCore/KlaviyoEnvironment.swift | 4 ++-- .../Networking/KlaviyoEndpoint.swift | 2 ++ .../Networking/KlaviyoRequest.swift | 18 +++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Sources/KlaviyoCore/KlaviyoEnvironment.swift b/Sources/KlaviyoCore/KlaviyoEnvironment.swift index 8ce19146..01b5ca68 100644 --- a/Sources/KlaviyoCore/KlaviyoEnvironment.swift +++ b/Sources/KlaviyoCore/KlaviyoEnvironment.swift @@ -68,7 +68,7 @@ public struct KlaviyoEnvironment { sdkVersion = SDKVersion } - static let productionHost = "https://a.klaviyo.com" + static let productionHost = "a.klaviyo.com" public static let encoder = { () -> JSONEncoder in let encoder = JSONEncoder() encoder.dateEncodingStrategy = .iso8601 @@ -81,7 +81,7 @@ public struct KlaviyoEnvironment { return decoder }() - private static let reachabilityService = Reachability(hostname: URL(string: productionHost)!.host!) + private static let reachabilityService = Reachability(hostname: productionHost) public var archiverClient: ArchiverClient public var fileClient: FileClient diff --git a/Sources/KlaviyoCore/Networking/KlaviyoEndpoint.swift b/Sources/KlaviyoCore/Networking/KlaviyoEndpoint.swift index 974b749f..b2ffe537 100644 --- a/Sources/KlaviyoCore/Networking/KlaviyoEndpoint.swift +++ b/Sources/KlaviyoCore/Networking/KlaviyoEndpoint.swift @@ -15,6 +15,8 @@ public enum KlaviyoEndpoint: Equatable, Codable { case registerPushToken(PushTokenPayload) case unregisterPushToken(UnregisterPushTokenPayload) + var httpScheme: String { "https" } + var httpMethod: RequestMethod { switch self { case .createProfile, .createEvent, .registerPushToken, .unregisterPushToken: diff --git a/Sources/KlaviyoCore/Networking/KlaviyoRequest.swift b/Sources/KlaviyoCore/Networking/KlaviyoRequest.swift index d6fd695c..e9aeb116 100644 --- a/Sources/KlaviyoCore/Networking/KlaviyoRequest.swift +++ b/Sources/KlaviyoCore/Networking/KlaviyoRequest.swift @@ -39,12 +39,16 @@ public struct KlaviyoRequest: Equatable, Codable { } var url: URL? { - switch endpoint { - case .createProfile, .createEvent, .registerPushToken, .unregisterPushToken: - if !environment.apiURL().isEmpty { - return URL(string: "\(environment.apiURL())\(endpoint.path)?company_id=\(apiKey)") - } - return nil - } + guard !environment.apiURL().isEmpty else { return nil } + + var components = URLComponents() + components.scheme = endpoint.httpScheme + components.host = environment.apiURL() + components.path = endpoint.path + components.queryItems = [ + URLQueryItem(name: "company_id", value: apiKey) + ] + + return components.url } } From 95ebf3b31c96f1e4215980d3962a2195e9e28a41 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Mon, 18 Nov 2024 11:22:17 -0500 Subject: [PATCH 2/2] fixed unit tests --- .../KlaviyoAPITests/testSuccessfulResponseWithEvent.1.txt | 4 ++-- .../KlaviyoAPITests/testSuccessfulResponseWithProfile.1.txt | 4 ++-- .../testSuccessfulResponseWithStoreToken.1.txt | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithEvent.1.txt b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithEvent.1.txt index fafe69a5..2c164299 100644 --- a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithEvent.1.txt +++ b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithEvent.1.txt @@ -1,6 +1,6 @@ -▿ dead_beef/client/events/?company_id=foo +▿ https://dead_beef/client/events/?company_id=foo ▿ url: Optional - - some: dead_beef/client/events/?company_id=foo + - some: https://dead_beef/client/events/?company_id=foo - cachePolicy: 0 - timeoutInterval: 60.0 - mainDocumentURL: Optional.none diff --git a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithProfile.1.txt b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithProfile.1.txt index 9563cd57..21358c1a 100644 --- a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithProfile.1.txt +++ b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithProfile.1.txt @@ -1,6 +1,6 @@ -▿ dead_beef/client/profiles/?company_id=foo +▿ https://dead_beef/client/profiles/?company_id=foo ▿ url: Optional - - some: dead_beef/client/profiles/?company_id=foo + - some: https://dead_beef/client/profiles/?company_id=foo - cachePolicy: 0 - timeoutInterval: 60.0 - mainDocumentURL: Optional.none diff --git a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithStoreToken.1.txt b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithStoreToken.1.txt index b80d173f..2eb0c615 100644 --- a/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithStoreToken.1.txt +++ b/Tests/KlaviyoCoreTests/__Snapshots__/KlaviyoAPITests/testSuccessfulResponseWithStoreToken.1.txt @@ -1,6 +1,6 @@ -▿ dead_beef/client/push-tokens/?company_id=foo +▿ https://dead_beef/client/push-tokens/?company_id=foo ▿ url: Optional - - some: dead_beef/client/push-tokens/?company_id=foo + - some: https://dead_beef/client/push-tokens/?company_id=foo - cachePolicy: 0 - timeoutInterval: 60.0 - mainDocumentURL: Optional.none