Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
silkodenis committed Aug 12, 2024
1 parent cf7711b commit 6f84089
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
15 changes: 5 additions & 10 deletions Sources/CombineNetworking/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,21 @@ public final class HTTPClient {
/// A session conforming to `HTTPSession` for handling requests.
private let session: HTTPSession

/// A dispatch queue on which the results will be received.
/// This queue determines where the completion of the HTTP request and the processing of the response will occur.
/// By default, it is set to the main queue.
private let queue: DispatchQueue

/// Initializes a new HTTPClient with the given JSON decoder and session.
/// - Parameters:
/// - jsonDecoder: A `JSONDecoder` to use for decoding the response data.
/// - session: An `HTTPSession` for sending requests and receiving responses.
/// - queue: A `DispatchQueue` on which to receive the results. Defaults to the main queue.
public init(jsonDecoder: JSONDecoder, session: HTTPSession, queue: DispatchQueue = .main) {
public init(jsonDecoder: JSONDecoder, session: HTTPSession) {
self.decoder = jsonDecoder
self.session = session
self.queue = queue
}

/// Executes a request and decodes the response.
/// - Parameter request: The `URLRequest` to execute.
/// - Parameters:
/// - request: The `URLRequest` to execute.
/// - queue: A `DispatchQueue` on which to receive the results. Defaults to the main queue.
/// - Returns: A publisher that emits the decoded response or an error.
public func execute<T: Decodable>(_ request: URLRequest) -> AnyPublisher<T, Error> {
public func execute<T: Decodable>(_ request: URLRequest, queue: DispatchQueue = .main) -> AnyPublisher<T, Error> {
return session
.dataTask(for: request)
.tryMap { data, response in
Expand Down
4 changes: 1 addition & 3 deletions Sources/CombineNetworking/HTTPRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ public final class HTTPRequestBuilder<T: HTTPEndpoint> {
}
}

// MARK: - Private

/// Constructs a URLRequest with provided endpoint and data.
/// - Parameters:
/// - endpoint: The endpoint containing all necessary information to build the URL.
/// - data: Optional Codable data to be included as the HTTP body.
/// - Throws: An error if the URL cannot be constructed or the data cannot be encoded.
/// - Returns: A configured URLRequest ready to be executed.
private func buildRequest(for endpoint: T, with data: Codable? = nil) throws -> URLRequest {
public func buildRequest(for endpoint: T, with data: Codable? = nil) throws -> URLRequest {
let url = endpoint.baseURL.appendingPathComponent(endpoint.path)

guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)?
Expand Down

0 comments on commit 6f84089

Please sign in to comment.