diff --git a/EmployeeDirectory/Services/NetworkService.swift b/EmployeeDirectory/Services/NetworkService.swift index cddd430..1ed750d 100644 --- a/EmployeeDirectory/Services/NetworkService.swift +++ b/EmployeeDirectory/Services/NetworkService.swift @@ -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(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(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 {