Skip to content

Commit

Permalink
Make NetworkRequest Equatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Twigz committed Feb 22, 2023
1 parent f59d005 commit bf1a3f9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/Networking/NetworkController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension NetworkController: NetworkRequestPerformer {

// MARK: - NetworkRequestPerformer

@discardableResult public func send(_ request: NetworkRequest, requestBehaviors: [RequestBehavior] = [], completion: ((Result<NetworkResponse, NetworkError>) -> Void)? = nil) -> URLSessionDataTask {
@discardableResult public func send(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior] = [], completion: ((Result<NetworkResponse, NetworkError>) -> Void)? = nil) -> URLSessionDataTask {
let behaviors = defaultRequestBehaviors + requestBehaviors

let urlRequest = makeFinalizedRequest(fromOriginalRequest: request.urlRequest, behaviors: behaviors)
Expand All @@ -73,7 +73,7 @@ extension NetworkController: NetworkRequestPerformer {
}

@available(iOS 13.0, *)
@discardableResult public func send(_ request: NetworkRequest, requestBehaviors: [RequestBehavior] = []) -> AnyPublisher<NetworkResponse, NetworkError> {
@discardableResult public func send(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior] = []) -> AnyPublisher<NetworkResponse, NetworkError> {
let behaviors = defaultRequestBehaviors + requestBehaviors
let urlRequest = makeFinalizedRequest(fromOriginalRequest: request.urlRequest, behaviors: behaviors)

Expand Down
8 changes: 6 additions & 2 deletions Sources/Networking/NetworkRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// A protocol that defines the parameters that make up a request.
public protocol NetworkRequest {
public protocol NetworkRequest: Equatable {

/// The generated `URLRequest` to use for making network requests. Defaults to a url request built using the receiver’s properties.
var urlRequest: URLRequest { get }
Expand Down Expand Up @@ -37,7 +37,7 @@ public protocol NetworkRequest {
}

/// Represents a collection of possible HTTP status codes.
public enum HTTPStatusCodes {
public enum HTTPStatusCodes: Equatable {

/// All status codes.
case all
Expand Down Expand Up @@ -102,4 +102,8 @@ public extension NetworkRequest {
var successHTTPStatusCodes: HTTPStatusCodes {
return .ranges([200...299])
}

static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.urlRequest == rhs.urlRequest && lhs.successHTTPStatusCodes == rhs.successHTTPStatusCodes
}
}
2 changes: 1 addition & 1 deletion Sources/Networking/NetworkRequestPerformer+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension NetworkRequestPerformer {
/// - decoder: The JSON decoder to use when decoding the data.
/// - completion: A completion closure that is called when the request has been completed.
/// - Returns: The `URLSessionDataTask` used to send the request. The implementation must call `resume()` on the task before returning.
@discardableResult public func send<ResponseType: Decodable>(_ request: NetworkRequest, requestBehaviors: [RequestBehavior] = [], decoder: JSONDecoder = JSONDecoder(), completion: ((Result<ResponseType, NetworkError>) -> Void)? = nil) -> URLSessionDataTask {
@discardableResult public func send<ResponseType: Decodable>(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior] = [], decoder: JSONDecoder = JSONDecoder(), completion: ((Result<ResponseType, NetworkError>) -> Void)? = nil) -> URLSessionDataTask {
send(request, requestBehaviors: requestBehaviors) { result in
switch result {
case let .success(response):
Expand Down
4 changes: 2 additions & 2 deletions Sources/Networking/NetworkRequestPerformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public protocol NetworkRequestPerformer {
/// - requestBehaviors: The behaviors to apply to the given request.
/// - completion: A completion closure that is called when the request has been completed.
/// - Returns: The `URLSessionDataTask` used to send the request. The implementation must call `resume()` on the task before returning.
@discardableResult func send(_ request: NetworkRequest, requestBehaviors: [RequestBehavior], completion: ((Result<NetworkResponse, NetworkError>) -> Void)?) -> URLSessionDataTask
@discardableResult func send(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior], completion: ((Result<NetworkResponse, NetworkError>) -> Void)?) -> URLSessionDataTask

/// Returns a publisher that can be subscribed to, that performs the given request with the given behaviors.
/// - Parameters:
/// - request: The request to perform.
/// - requestBehaviors: The behaviors to apply to the given request.
/// - Returns: Returns a publisher that can be subscribed to, that performs the given request with the given behaviors.
@available(iOS 13.0, *)
@discardableResult func send(_ request: NetworkRequest, requestBehaviors: [RequestBehavior]) -> AnyPublisher<NetworkResponse, NetworkError>
@discardableResult func send(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior]) -> AnyPublisher<NetworkResponse, NetworkError>
}
2 changes: 1 addition & 1 deletion Sources/Networking/NetworkRequestStateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public final class NetworkRequestStateController {
/// - Parameters:
/// - request: The request to send.
/// - requestBehaviors: Additional behaviors to append to the request.
public func send(request: NetworkRequest, requestBehaviors: [RequestBehavior] = []) {
public func send(request: any NetworkRequest, requestBehaviors: [RequestBehavior] = []) {
requestStatePublisher.send(.inProgress)

requestPerformer.send(request, requestBehaviors: requestBehaviors)
Expand Down

0 comments on commit bf1a3f9

Please sign in to comment.