added new networkservice for urlrequest
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
801830e5b8
commit
4ae060597c
@ -49,14 +49,24 @@ public class NetworkService {
|
||||
/// - Throws: A `ServiceError` for network, decoding, or unexpected errors.
|
||||
/// - Returns: The decoded object of the specified type.
|
||||
public func fetchData<T: Decodable>(from endpoint: String, as type: T.Type) async throws -> T {
|
||||
//ensure a valid URL
|
||||
guard let url = URL(string: endpoint) else {
|
||||
throw NetworkServiceError.invalidURL
|
||||
}
|
||||
|
||||
return try await fetchData(with: URLRequest(url: url), as: type)
|
||||
}
|
||||
|
||||
/// Fetches data using a URLRequest and decodes it into a generic Decodable type.
|
||||
/// - Parameters:
|
||||
/// - request: The URLRequest to execute.
|
||||
/// - type: The type to decode the data into.
|
||||
/// - Throws: A `NetworkServiceError` for network, decoding, or unexpected errors.
|
||||
/// - Returns: The decoded object of the specified type.
|
||||
public func fetchData<T: Decodable>(with request: URLRequest, as type: T.Type) async throws -> T {
|
||||
do {
|
||||
//ensure a valid URL
|
||||
guard let url = URL(string: endpoint) else {
|
||||
throw NetworkServiceError.invalidURL
|
||||
}
|
||||
|
||||
// Perform network request
|
||||
let (data, response) = try await URLSession.shared.data(for: URLRequest(url: url))
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
// Validate HTTP response
|
||||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user