added code for a network service
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
b2ce994cea
commit
12ff7b0e8a
@ -43,4 +43,24 @@ public class EmployeeService: EmployeeServiceProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
public class Networkservice {
|
||||
public static let shared = Networkservice()
|
||||
|
||||
public init(){}
|
||||
|
||||
public func fetch<T: Decodable>(endpoint: String, type: T.Type) async throws -> T {
|
||||
guard let url = URL(string: endpoint) else {
|
||||
throw URLError(.badURL)
|
||||
}
|
||||
|
||||
let (data, response) = try await URLSession.shared.data(from: url)
|
||||
|
||||
guard let httpResponse = response as? HTTPURLResponse,
|
||||
200..<300 ~= httpResponse.statusCode else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
|
||||
return try JSONDecoder().decode(T.self, from: data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user