Skip to content

Instantly share code, notes, and snippets.

@VB10
Created January 9, 2024 01:28
Show Gist options
  • Save VB10/8eba2795385c730454e02c6ed5fee87f to your computer and use it in GitHub Desktop.
Save VB10/8eba2795385c730454e02c6ed5fee87f to your computer and use it in GitHub Desktop.
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