Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import Alamofire
- private let API_BASE_URL = "https://auth-test.ecp-share.com/v1"
- extension Session {
- func requestPayload<T: Decodable>(path: String,
- method: HTTPMethod = .get,
- parameters: Parameters? = nil,
- headers: HTTPHeaders? = nil) async -> Result<Array<T>, NetworkError> {
- do {
- let response = try await withCheckedThrowingContinuation { continuation in
- AF.request(
- API_BASE_URL + path,
- method: method,
- parameters: parameters,
- headers: headers
- )
- .responseData { response in
- switch(response.result) {
- case let .success(data):
- continuation.resume(returning: data)
- case let .failure(error):
- continuation.resume(throwing: error)
- }
- }
- }
- do {
- let apiResponse: APIResponse<T> = try JSONDecoder().decode(APIResponse<T>.self, from: response)
- guard let result = apiResponse.result else { return .failure(NetworkError())}
- return .success(result.items)
- }
- catch {
- let apiResponse: APIResponse<APIError> = try JSONDecoder().decode(APIResponse<APIError>.self, from: response)
- guard let errors = apiResponse.errors else { return .failure(NetworkError(description: "1"))}
- let error = errors.items[0]
- return .failure(
- NetworkError(
- // errorType: error.errorType,
- paramName: error.paramName,
- // obtainedValue: error.obtainedValue,
- description: error.description ?? "2"
- // details: error.details
- )
- )
- }
- }
- catch {
- debugPrint(error)
- return .failure(NetworkError(description: "3"))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement