Skip to content

Commit

Permalink
fix: add a default endpoint to the provider (#68)
Browse files Browse the repository at this point in the history
* add GLOBAL as default provider endpoint

* rename client to offline for clarity

---------

Co-authored-by: Nicklas Lundin <[email protected]>
  • Loading branch information
nickybondarenko and nicklasl authored Jan 12, 2024
1 parent 9bdcb3e commit 9d8d173
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public struct ConfidenceClientOptions {
) {
self.credentials = credentials
self.timeout = timeout ?? 10.0
self.region = region ?? .europe
self.region = region ?? .global
}
}

Expand All @@ -186,9 +186,10 @@ public enum ConfidenceClientCredentials {
}
}

public enum ConfidenceRegion: String {
case europe = "eu"
case usa = "us"
public enum ConfidenceRegion {
case global
case europe
case usa
}

struct Sdk: Codable {
Expand Down
13 changes: 8 additions & 5 deletions Sources/ConfidenceProvider/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ final class NetworkClient: HttpClient {
private let region: ConfidenceRegion

private var baseUrl: String {
let region = region.rawValue
let domain = "confidence.dev"
let resolveRoute = "/v1/flags"

return "https://resolver.\(region).\(domain)\(resolveRoute)"
switch region {
case .global:
return "https://resolver.confidence.dev/v1/flags"
case .europe:
return "https://resolver.eu.confidence.dev/v1/flags"
case .usa:
return "https://resolver.us.confidence.dev/v1/flags"
}
}

init(
Expand Down
16 changes: 8 additions & 8 deletions Tests/ConfidenceProviderTests/FlagApplierWithRetriesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class FlagApplierWithRetriesTest: XCTestCase {

func testApply_multipleApplyCalls_batchTriggered() async throws {
// Given flag applier with http client that is offline
let httpClient = HttpClientMock(testMode: .error)
let httpClient = HttpClientMock(testMode: .offline)
let networkExpectation = self.expectation(description: "Waiting for batch trigger")
networkExpectation.expectedFulfillmentCount = 2
httpClient.expectation = networkExpectation
Expand Down Expand Up @@ -237,7 +237,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApply_multipleApplyCalls_sentSet() async throws {
// Given flag applier with http client that is offline
let cacheDataInteractor = CacheDataInteractor(cacheData: .empty())
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let networkExpectation = self.expectation(description: "Waiting for network call to complete")
networkExpectation.expectedFulfillmentCount = 2
offlineClient.expectation = networkExpectation
Expand Down Expand Up @@ -336,7 +336,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApply_previoslyStoredData_doesNotCleanAfterSendingFailure() throws {
// Given offline http client
// And storage that has previosly stored data (100 records, same token)
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let prefilledStorage = StorageMock()
let prefilledCache = try CacheDataUtility.prefilledCacheData(applyEventCount: 100)
try prefilledStorage.save(data: prefilledCache)
Expand All @@ -359,7 +359,7 @@ class FlagApplierWithRetriesTest: XCTestCase {

func testApplyOffline_storesOnDisk() async throws {
// Given offline http client and flag applier
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let applier = FlagApplierWithRetries(
httpClient: offlineClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)
Expand Down Expand Up @@ -388,7 +388,7 @@ class FlagApplierWithRetriesTest: XCTestCase {

func testApplyOffline_storesOnDisk_multipleTokens() async throws {
// Given offline http client and flag applier
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let applier = FlagApplierWithRetries(
httpClient: offlineClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)
Expand Down Expand Up @@ -416,7 +416,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApplyOffline_previoslyStoredData_storesOnDisk() async throws {
// Given flag applier set up with offline http client
// And storage that has previously stored 1 record
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let data = CacheData(resolveToken: "token0", flagName: "flag1", applyTime: Date(timeIntervalSince1970: 1000))
let prefilledStorage = try StorageMock(data: data)

Expand Down Expand Up @@ -453,7 +453,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApplyOffline_previoslyStoredData_100records() async throws {
// Given flag applier set up with offline http client
// And storage that has previously stored 100 records with different tokens
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let prefilledStorage = StorageMock()
let prefilledCache = try CacheDataUtility.prefilledCacheData(resolveEventCount: 100)
try prefilledStorage.save(data: prefilledCache)
Expand All @@ -477,7 +477,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApplyOffline_100applyCalls_sameToken() async throws {
// Given flag applier set up with offline http client
// And storage that has previously stored 100 records with same token
let offlineClient = HttpClientMock(testMode: .error)
let offlineClient = HttpClientMock(testMode: .offline)
let networkExpectation = self.expectation(description: "Waiting for networkRequest to be completed")

// Since we don't fail other requests when one request is failing
Expand Down
4 changes: 2 additions & 2 deletions Tests/ConfidenceProviderTests/Helpers/HttpClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class HttpClientMock: HttpClient {
enum TestMode {
case success
case failFirstChunk
case error
case offline
}

init(testMode: TestMode = .success) {
Expand Down Expand Up @@ -63,7 +63,7 @@ final class HttpClientMock: HttpClient {
} else {
return HttpClientResponse(response: HTTPURLResponse())
}
case .error:
case .offline:
throw HttpClientError.invalidResponse
}
}
Expand Down

0 comments on commit 9d8d173

Please sign in to comment.