Skip to content

Commit

Permalink
refactor!: decrease API surface (#156)
Browse files Browse the repository at this point in the history
refactor: decrease API surface
  • Loading branch information
nicklasl authored Jul 5, 2024
1 parent 49aa011 commit 917743b
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 158 deletions.
4 changes: 2 additions & 2 deletions Sources/Confidence/ConfidenceClientOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct ConfidenceClientOptions {
struct ConfidenceClientOptions {
public var credentials: ConfidenceClientCredentials
public var region: ConfidenceRegion
public var initializationStrategy: InitializationStrategy
Expand All @@ -16,7 +16,7 @@ public struct ConfidenceClientOptions {
}
}

public enum ConfidenceClientCredentials {
enum ConfidenceClientCredentials {
case clientSecret(secret: String)

public func getSecret() -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/ConfidenceMetadata.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct ConfidenceMetadata {
struct ConfidenceMetadata {
public var name: String
public var version: String

Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/DefaultStorage.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/FlagPath.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions Sources/Confidence/Http/HttpClient.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation

public typealias HttpClientResult<T> = Result<HttpClientResponse<T>, Error>
typealias HttpClientResult<T> = Result<HttpClientResponse<T>, Error>

public protocol HttpClient {
internal protocol HttpClient {
func post<T: Decodable>(path: String, data: Encodable) async throws -> HttpClientResult<T>
}

public struct HttpClientResponse<T> {
struct HttpClientResponse<T> {
public init(decodedData: T? = nil, decodedError: HttpError? = nil, response: HTTPURLResponse) {
self.decodedData = decodedData
self.decodedError = decodedError
Expand All @@ -17,7 +17,7 @@ public struct HttpClientResponse<T> {
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
Expand All @@ -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")")

Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions Sources/Confidence/Http/Retry.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum Retry {
enum Retry {
case none
case exponential(maxBackoff: TimeInterval, maxAttempts: UInt)

Expand All @@ -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
Expand All @@ -40,7 +40,7 @@ public class ExponentialBackoffRetryHandler: RetryHandler {
}
}

public class NoneRetryHandler: RetryHandler {
class NoneRetryHandler: RetryHandler {
public func retryIn() -> TimeInterval? {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/NetowrkValue.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/RemoteResolveConfidenceClient.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Confidence/TypeMapper.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum TypeMapper {
enum TypeMapper {
static internal func convert(structure: ConfidenceStruct) -> NetworkStruct {
return NetworkStruct(fields: structure.compactMapValues(convert))
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/ConfidenceTests/Helpers/HttpClientMock.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Foundation
import Confidence
import XCTest

@testable import Confidence

final class HttpClientMock: HttpClient {
var testMode: TestMode
var postCallCounter = 0
Expand Down
136 changes: 0 additions & 136 deletions api/Confidence_public_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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<T>(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<T: Decodable>(\n path: String,\n data: Encodable\n) async throws -> HttpClientResult<T>"
}
]
},
{
"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"
}
]
}
]

0 comments on commit 917743b

Please sign in to comment.