From 917743b98df4b1bbae7caa7941540cf07e7b9316 Mon Sep 17 00:00:00 2001 From: Nicklas Lundin Date: Fri, 5 Jul 2024 13:15:49 +0200 Subject: [PATCH] refactor!: decrease API surface (#156) refactor: decrease API surface --- .../Confidence/ConfidenceClientOptions.swift | 4 +- Sources/Confidence/ConfidenceMetadata.swift | 2 +- Sources/Confidence/DefaultStorage.swift | 2 +- Sources/Confidence/FlagPath.swift | 4 +- Sources/Confidence/Http/HttpClient.swift | 12 +- Sources/Confidence/Http/NetworkClient.swift | 2 +- Sources/Confidence/Http/Retry.swift | 8 +- Sources/Confidence/NetowrkValue.swift | 4 +- .../RemoteResolveConfidenceClient.swift | 2 +- Sources/Confidence/TypeMapper.swift | 2 +- .../Helpers/HttpClientMock.swift | 3 +- api/Confidence_public_api.json | 136 ------------------ 12 files changed, 23 insertions(+), 158 deletions(-) diff --git a/Sources/Confidence/ConfidenceClientOptions.swift b/Sources/Confidence/ConfidenceClientOptions.swift index 5515c12d..7ddb17ee 100644 --- a/Sources/Confidence/ConfidenceClientOptions.swift +++ b/Sources/Confidence/ConfidenceClientOptions.swift @@ -1,6 +1,6 @@ import Foundation -public struct ConfidenceClientOptions { +struct ConfidenceClientOptions { public var credentials: ConfidenceClientCredentials public var region: ConfidenceRegion public var initializationStrategy: InitializationStrategy @@ -16,7 +16,7 @@ public struct ConfidenceClientOptions { } } -public enum ConfidenceClientCredentials { +enum ConfidenceClientCredentials { case clientSecret(secret: String) public func getSecret() -> String { diff --git a/Sources/Confidence/ConfidenceMetadata.swift b/Sources/Confidence/ConfidenceMetadata.swift index f2f8c880..0cf080b7 100644 --- a/Sources/Confidence/ConfidenceMetadata.swift +++ b/Sources/Confidence/ConfidenceMetadata.swift @@ -1,6 +1,6 @@ import Foundation -public struct ConfidenceMetadata { +struct ConfidenceMetadata { public var name: String public var version: String diff --git a/Sources/Confidence/DefaultStorage.swift b/Sources/Confidence/DefaultStorage.swift index 20a4406b..8ed1a57f 100644 --- a/Sources/Confidence/DefaultStorage.swift +++ b/Sources/Confidence/DefaultStorage.swift @@ -1,6 +1,6 @@ import Foundation -public class DefaultStorage: Storage { +class DefaultStorage: Storage { private let storageQueue = DispatchQueue(label: "com.confidence.storage") private let resolverCacheBundleId = "com.confidence.cache" private let filePath: String diff --git a/Sources/Confidence/FlagPath.swift b/Sources/Confidence/FlagPath.swift index 4d976573..9ae66b4f 100644 --- a/Sources/Confidence/FlagPath.swift +++ b/Sources/Confidence/FlagPath.swift @@ -1,10 +1,10 @@ import Foundation -public struct FlagPath { +struct FlagPath { var flag: String var path: [String] - public static func getPath(for path: String) throws -> FlagPath { + static func getPath(for path: String) throws -> FlagPath { let parts = path.components(separatedBy: ".") guard let flag = parts.first else { diff --git a/Sources/Confidence/Http/HttpClient.swift b/Sources/Confidence/Http/HttpClient.swift index 4d9b0c18..e6a800a4 100644 --- a/Sources/Confidence/Http/HttpClient.swift +++ b/Sources/Confidence/Http/HttpClient.swift @@ -1,12 +1,12 @@ import Foundation -public typealias HttpClientResult = Result, Error> +typealias HttpClientResult = Result, Error> -public protocol HttpClient { +internal protocol HttpClient { func post(path: String, data: Encodable) async throws -> HttpClientResult } -public struct HttpClientResponse { +struct HttpClientResponse { public init(decodedData: T? = nil, decodedError: HttpError? = nil, response: HTTPURLResponse) { self.decodedData = decodedData self.decodedError = decodedError @@ -17,7 +17,7 @@ public struct HttpClientResponse { public var response: HTTPURLResponse } -public struct HttpError: Codable { +struct HttpError: Codable { public init(code: Int, message: String, details: [String]) { self.code = code self.message = message @@ -28,13 +28,13 @@ public struct HttpError: Codable { public var details: [String] } -public enum HttpClientError: Error { +enum HttpClientError: Error { case invalidResponse case internalError } extension HTTPURLResponse { - public func mapStatusToError(error: HttpError?) -> ConfidenceError { + func mapStatusToError(error: HttpError?) -> ConfidenceError { let defaultError = ConfidenceError.internalError( message: "General error: \(error?.message ?? "Unknown error")") diff --git a/Sources/Confidence/Http/NetworkClient.swift b/Sources/Confidence/Http/NetworkClient.swift index 5f54bcf4..b9898a6f 100644 --- a/Sources/Confidence/Http/NetworkClient.swift +++ b/Sources/Confidence/Http/NetworkClient.swift @@ -1,6 +1,6 @@ import Foundation -final public class NetworkClient: HttpClient { +final class NetworkClient: HttpClient { private let headers: [String: String] private let retry: Retry private let session: URLSession diff --git a/Sources/Confidence/Http/Retry.swift b/Sources/Confidence/Http/Retry.swift index 5f1c6137..d3439e20 100644 --- a/Sources/Confidence/Http/Retry.swift +++ b/Sources/Confidence/Http/Retry.swift @@ -1,6 +1,6 @@ import Foundation -public enum Retry { +enum Retry { case none case exponential(maxBackoff: TimeInterval, maxAttempts: UInt) @@ -14,11 +14,11 @@ public enum Retry { } } -public protocol RetryHandler { +protocol RetryHandler { func retryIn() -> TimeInterval? } -public class ExponentialBackoffRetryHandler: RetryHandler { +class ExponentialBackoffRetryHandler: RetryHandler { private var currentAttempts: UInt = 0 private let maxBackoff: TimeInterval private let maxAttempts: UInt @@ -40,7 +40,7 @@ public class ExponentialBackoffRetryHandler: RetryHandler { } } -public class NoneRetryHandler: RetryHandler { +class NoneRetryHandler: RetryHandler { public func retryIn() -> TimeInterval? { return nil } diff --git a/Sources/Confidence/NetowrkValue.swift b/Sources/Confidence/NetowrkValue.swift index 97d52ca1..5e4e3422 100644 --- a/Sources/Confidence/NetowrkValue.swift +++ b/Sources/Confidence/NetowrkValue.swift @@ -1,13 +1,13 @@ import Foundation -public struct NetworkStruct: Equatable { +struct NetworkStruct: Equatable { public init(fields: [String: NetworkValue]) { self.fields = fields } public var fields: [String: NetworkValue] } -public enum NetworkValue: Equatable { +enum NetworkValue: Equatable { case null case string(String) case number(Double) diff --git a/Sources/Confidence/RemoteResolveConfidenceClient.swift b/Sources/Confidence/RemoteResolveConfidenceClient.swift index 7521f75a..b2f168cd 100644 --- a/Sources/Confidence/RemoteResolveConfidenceClient.swift +++ b/Sources/Confidence/RemoteResolveConfidenceClient.swift @@ -1,6 +1,6 @@ import Foundation -public class RemoteConfidenceResolveClient: ConfidenceResolveClient { +class RemoteConfidenceResolveClient: ConfidenceResolveClient { private let targetingKey = "targeting_key" private var options: ConfidenceClientOptions private let metadata: ConfidenceMetadata diff --git a/Sources/Confidence/TypeMapper.swift b/Sources/Confidence/TypeMapper.swift index 4e92bc3a..b3443291 100644 --- a/Sources/Confidence/TypeMapper.swift +++ b/Sources/Confidence/TypeMapper.swift @@ -1,6 +1,6 @@ import Foundation -public enum TypeMapper { +enum TypeMapper { static internal func convert(structure: ConfidenceStruct) -> NetworkStruct { return NetworkStruct(fields: structure.compactMapValues(convert)) } diff --git a/Tests/ConfidenceTests/Helpers/HttpClientMock.swift b/Tests/ConfidenceTests/Helpers/HttpClientMock.swift index 5e976e82..2eead808 100644 --- a/Tests/ConfidenceTests/Helpers/HttpClientMock.swift +++ b/Tests/ConfidenceTests/Helpers/HttpClientMock.swift @@ -1,7 +1,8 @@ import Foundation -import Confidence import XCTest +@testable import Confidence + final class HttpClientMock: HttpClient { var testMode: TestMode var postCallCounter = 0 diff --git a/api/Confidence_public_api.json b/api/Confidence_public_api.json index 787310e5..01f9bb32 100644 --- a/api/Confidence_public_api.json +++ b/api/Confidence_public_api.json @@ -106,33 +106,6 @@ } ] }, - { - "className": "ConfidenceClientOptions", - "apiFunctions": [ - { - "name": "init(credentials:region:initializationStrategy:)", - "declaration": "public init(\n credentials: ConfidenceClientCredentials,\n region: ConfidenceRegion? = nil,\n initializationStrategy: InitializationStrategy = .fetchAndActivate\n)" - } - ] - }, - { - "className": "ConfidenceClientCredentials", - "apiFunctions": [ - { - "name": "getSecret()", - "declaration": "public func getSecret() -> String" - } - ] - }, - { - "className": "ConfidenceMetadata", - "apiFunctions": [ - { - "name": "init(name:version:)", - "declaration": "public init(name: String, version: String)" - } - ] - }, { "className": "Event", "apiFunctions": [ @@ -267,114 +240,5 @@ "declaration": "public static func == (lhs: ConfidenceValue, rhs: ConfidenceValue) -> Bool" } ] - }, - { - "className": "DefaultStorage", - "apiFunctions": [ - { - "name": "init(filePath:)", - "declaration": "public init(filePath: String)" - }, - { - "name": "save(data:)", - "declaration": "public func save(data: Encodable) throws" - }, - { - "name": "load(defaultValue:)", - "declaration": "public func load(defaultValue: T) throws -> T where T: Decodable" - }, - { - "name": "clear()", - "declaration": "public func clear() throws" - }, - { - "name": "isEmpty()", - "declaration": "public func isEmpty() -> Bool" - }, - { - "name": "getConfigUrl()", - "declaration": "public func getConfigUrl() throws -> URL" - } - ] - }, - { - "className": "FlagPath", - "apiFunctions": [ - { - "name": "getPath(for:)", - "declaration": "public static func getPath(for path: String) throws -> FlagPath" - } - ] - }, - { - "className": "HttpClientResponse", - "apiFunctions": [ - { - "name": "init(decodedData:decodedError:response:)", - "declaration": "public init(decodedData: T? = nil, decodedError: HttpError? = nil, response: HTTPURLResponse)" - } - ] - }, - { - "className": "HttpError", - "apiFunctions": [ - { - "name": "init(code:message:details:)", - "declaration": "public init(code: Int, message: String, details: [String])" - } - ] - }, - { - "className": "NetworkClient", - "apiFunctions": [ - { - "name": "init(session:baseUrl:defaultHeaders:retry:)", - "declaration": "public init(\n session: URLSession? = nil,\n baseUrl: String,\n defaultHeaders: [String: String] = [:],\n retry: Retry = .none\n)" - }, - { - "name": "post(path:data:)", - "declaration": "public func post(\n path: String,\n data: Encodable\n) async throws -> HttpClientResult" - } - ] - }, - { - "className": "ExponentialBackoffRetryHandler", - "apiFunctions": [ - { - "name": "retryIn()", - "declaration": "public func retryIn() -> TimeInterval?" - } - ] - }, - { - "className": "NoneRetryHandler", - "apiFunctions": [ - { - "name": "retryIn()", - "declaration": "public func retryIn() -> TimeInterval?" - } - ] - }, - { - "className": "NetworkStruct", - "apiFunctions": [ - { - "name": "init(fields:)", - "declaration": "public init(fields: [String: NetworkValue])" - } - ] - }, - { - "className": "RemoteConfidenceResolveClient", - "apiFunctions": [ - { - "name": "resolve(flags:ctx:)", - "declaration": "public func resolve(flags: [String], ctx: ConfidenceStruct) async throws -> ResolvesResult" - }, - { - "name": "resolve(ctx:)", - "declaration": "public func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult" - } - ] } ] \ No newline at end of file