Skip to content

Commit

Permalink
feat: add custom headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang1992 committed Feb 17, 2025
1 parent 4a589b9 commit a7db4cb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Sources/FalClient/Client+Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ public enum ClientCredentials: CustomStringConvertible {
public struct ClientConfig {
public let credentials: ClientCredentials
public let requestProxy: String?
public let customHeaders: [String: String]?

init(credentials: ClientCredentials = .fromEnv, requestProxy: String? = nil) {
init(credentials: ClientCredentials = .fromEnv, requestProxy: String? = nil, customHeaders: [String: String]? = nil) {
self.credentials = credentials
self.requestProxy = requestProxy
self.customHeaders = customHeaders
}
}
5 changes: 5 additions & 0 deletions Sources/FalClient/Client+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ extension Client {
}

var request = URLRequest(url: url)
if let header = config.customHeaders {
for (key, value) in header {
request.setValue(value, forHTTPHeaderField: key)
}
}
request.httpMethod = options.httpMethod.rawValue.uppercased()
request.setValue("application/json", forHTTPHeaderField: "accept")
request.setValue("application/json", forHTTPHeaderField: "content-type")
Expand Down
4 changes: 2 additions & 2 deletions Sources/FalClient/FalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public struct FalClient: Client {
}

public extension FalClient {
static func withProxy(_ url: String) -> Client {
FalClient(config: ClientConfig(requestProxy: url))
static func withProxy(_ url: String, headers: [String: String]? = nil) -> Client {
FalClient(config: ClientConfig(requestProxy: url, customHeaders: headers))
}

static func withCredentials(_ credentials: ClientCredentials) -> Client {
Expand Down

0 comments on commit a7db4cb

Please sign in to comment.