Skip to content

Commit 3fd73c6

Browse files
committed
Refactor client to separate methods for creating and sending a request
1 parent 8f04fb1 commit 3fd73c6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Sources/Replicate/Client.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,19 @@ public class Client {
649649
params = ["cursor": "\(cursor)"]
650650
}
651651

652-
return try await fetch(method, path, params: params)
652+
let request = try createRequest(method: method, path: path, params: params)
653+
return try await sendRequest(request)
653654
}
654655

655656
private func fetch<T: Decodable>(_ method: Method,
656657
_ path: String,
657658
params: [String: Value]? = nil)
658659
async throws -> T {
660+
let request = try createRequest(method: method, path: path, params: params)
661+
return try await sendRequest(request)
662+
}
663+
664+
private func createRequest(method: Method, path: String, params: [String: Value]? = nil) throws -> URLRequest {
659665
var urlComponents = URLComponents(string: self.baseURLString.appending(path))
660666
var httpBody: Data? = nil
661667

@@ -673,6 +679,10 @@ public class Client {
673679
let encoder = JSONEncoder()
674680
httpBody = try encoder.encode(params)
675681
}
682+
case .query:
683+
if let params, let queryString = params["query"] {
684+
httpBody = queryString.description.data(using: .utf8)
685+
}
676686
}
677687

678688
guard let url = urlComponents?.url else {
@@ -696,6 +706,10 @@ public class Client {
696706
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
697707
}
698708

709+
return request
710+
}
711+
712+
private func sendRequest<T: Decodable>(_ request: URLRequest) async throws -> T {
699713
let (data, response) = try await session.data(for: request)
700714

701715
let decoder = JSONDecoder()

0 commit comments

Comments
 (0)