Skip to content

Commit

Permalink
Merge pull request #238 from klaviyo/ab/CHNL-14651/use-urlcomponents
Browse files Browse the repository at this point in the history
[CHNL-14651] refactor KlaviyoRequest to use URLComponents
  • Loading branch information
ab1470 authored Nov 20, 2024
2 parents 6bcd9e3 + 1fcc230 commit 33e0088
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Sources/KlaviyoCore/KlaviyoEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Sources/KlaviyoCore/Networking/KlaviyoEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 11 additions & 7 deletions Sources/KlaviyoCore/Networking/KlaviyoRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
▿ dead_beef/client/events/?company_id=foo
https://dead_beef/client/events/?company_id=foo
▿ url: Optional<URL>
- some: dead_beef/client/events/?company_id=foo
- some: https://dead_beef/client/events/?company_id=foo
- cachePolicy: 0
- timeoutInterval: 60.0
- mainDocumentURL: Optional<URL>.none
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
▿ dead_beef/client/profiles/?company_id=foo
https://dead_beef/client/profiles/?company_id=foo
▿ url: Optional<URL>
- some: dead_beef/client/profiles/?company_id=foo
- some: https://dead_beef/client/profiles/?company_id=foo
- cachePolicy: 0
- timeoutInterval: 60.0
- mainDocumentURL: Optional<URL>.none
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
▿ dead_beef/client/push-tokens/?company_id=foo
https://dead_beef/client/push-tokens/?company_id=foo
▿ url: Optional<URL>
- 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<URL>.none
Expand Down

0 comments on commit 33e0088

Please sign in to comment.