This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
#if canImport(FoundationNetworking)
|
#if os(Linux)
|
||||||
import FoundationNetworking
|
import FoundationNetworking
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -19,15 +19,15 @@ public enum NetworkError: Error {
|
|||||||
extension NetworkError: LocalizedError {
|
extension NetworkError: LocalizedError {
|
||||||
public var errorDescription: String? {
|
public var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
return NSLocalizedString("Unable to perform request", comment: "badRequestError")
|
return NSLocalizedString("Unable to perform request", comment: "badRequestError")
|
||||||
case .serverError(let errorMessage):
|
case .serverError(let errorMessage):
|
||||||
print(errorMessage)
|
print(errorMessage)
|
||||||
return NSLocalizedString(errorMessage, comment: "serverError")
|
return NSLocalizedString(errorMessage, comment: "serverError")
|
||||||
case .decodingError:
|
case .decodingError:
|
||||||
return NSLocalizedString("Unable to decode successfully", comment: "decodingError")
|
return NSLocalizedString("Unable to decode successfully", comment: "decodingError")
|
||||||
case .invalidResponse:
|
case .invalidResponse:
|
||||||
return NSLocalizedString("Invalid response", comment: "invalidResponse")
|
return NSLocalizedString("Invalid response", comment: "invalidResponse")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,12 @@ public enum HTTPMethod {
|
|||||||
|
|
||||||
var name: String {
|
var name: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .get:
|
case .get:
|
||||||
return "GET"
|
return "GET"
|
||||||
case .post:
|
case .post:
|
||||||
return "POST"
|
return "POST"
|
||||||
case .delete:
|
case .delete:
|
||||||
return "DELETE"
|
return "DELETE"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,9 +82,9 @@ public struct Hermes {
|
|||||||
var request = URLRequest(url: resource.url)
|
var request = URLRequest(url: resource.url)
|
||||||
|
|
||||||
switch resource.method {
|
switch resource.method {
|
||||||
case .get(let queryItems):
|
case .get(let queryItems):
|
||||||
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
||||||
components?.queryItems = queryItems
|
components?.queryItems = queryItems
|
||||||
guard let url = components?.url else {
|
guard let url = components?.url else {
|
||||||
throw NetworkError.badRequest
|
throw NetworkError.badRequest
|
||||||
}
|
}
|
||||||
@@ -99,11 +99,20 @@ public struct Hermes {
|
|||||||
request.httpMethod = resource.method.name
|
request.httpMethod = resource.method.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(Linux)
|
||||||
|
for header in defaultHeaders {
|
||||||
|
request.addValue(header.value, forHTTPHeaderField: header.key)
|
||||||
|
}
|
||||||
|
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
|
#else
|
||||||
let configuration = URLSessionConfiguration.default
|
let configuration = URLSessionConfiguration.default
|
||||||
configuration.httpAdditionalHeaders = defaultHeaders
|
configuration.httpAdditionalHeaders = defaultHeaders
|
||||||
|
|
||||||
let session = URLSession(configuration: configuration)
|
let session = URLSession(configuration: configuration)
|
||||||
|
|
||||||
let (data, response) = try await session.data(for: request)
|
let (data, response) = try await session.data(for: request)
|
||||||
|
#endif
|
||||||
|
|
||||||
guard let _ = response as? HTTPURLResponse else {
|
guard let _ = response as? HTTPURLResponse else {
|
||||||
throw NetworkError.invalidResponse
|
throw NetworkError.invalidResponse
|
||||||
|
|||||||
Reference in New Issue
Block a user