Created
January 9, 2024 01:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class NetworkManager: NetworkManagerCore { | |
var config: NetworkConfig = .init(baseUrl: "") | |
/// Send your request without model paramaters | |
/// - Parameters: | |
/// - path: NetworkPath value | |
/// - method: HTTPMethod value | |
/// - type: Decoded model | |
/// - Returns: Success encoded model or | |
func send<T: Decodable>(path: NetworkPath, method: HTTPMethod, type: T.Type) async -> Result<T?, Error> { | |
let requestUrl = "\(config.baseUrl)/\(path.rawValue)" | |
let response = await AF.request(requestUrl, method: method).validate() | |
.serializingDecodable(T.self) | |
.response | |
guard let value = response.value else { | |
return .failure(response.error ?? CustomError.networkError) | |
} | |
return .success(value) | |
} | |
func sendWithData<T: Decodable>(path: NetworkPath, body: Encodable, method: HTTPMethod, type: T.Type) async -> Result<T?, Error> { | |
let requestUrl = "\(config.baseUrl)/\(path.rawValue)" | |
let response = await AF.request(requestUrl, method: method, parameters: body, encoder: JSONParameterEncoder.default) | |
.validate() | |
.serializingDecodable(T.self) | |
.response | |
guard let value = response.value else { | |
return .failure(response.error ?? CustomError.networkError) | |
} | |
return .success(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment