37 lines
1016 B
Swift
37 lines
1016 B
Swift
//
|
|
// MakeURLRequestUseCase.swift
|
|
// APIService
|
|
//
|
|
// Created by Javier Cicchelli on 04/12/2022.
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct MakeURLRequestUseCase {
|
|
func callAsFunction(endpoint: some Endpoint) throws -> URLRequest {
|
|
var urlComponents = URLComponents()
|
|
|
|
urlComponents.scheme = endpoint.scheme
|
|
urlComponents.host = endpoint.host
|
|
urlComponents.port = endpoint.port
|
|
urlComponents.path = endpoint.path
|
|
|
|
guard let url = urlComponents.url else { throw MakeURLRequestError.urlNotCreated }
|
|
|
|
var urlRequest = URLRequest(url: url)
|
|
|
|
urlRequest.httpMethod = endpoint.method.rawValue
|
|
urlRequest.httpBody = endpoint.body
|
|
urlRequest.allHTTPHeaderFields = endpoint.authorizationHeader.merging(endpoint.headers) { first, _ in first }
|
|
|
|
return urlRequest
|
|
}
|
|
}
|
|
|
|
// MARK: - Errors
|
|
|
|
enum MakeURLRequestError: Error {
|
|
case urlNotCreated
|
|
}
|