Skip to content

Commit

Permalink
feat: Whole response result
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jan 24, 2024
1 parent ae03a37 commit 6989d94
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/InfomaniakCore/Networking/ApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ open class ApiFetcher {
}

open func perform<T: Decodable>(request: DataRequest,
decoder: JSONDecoder = ApiFetcher.decoder) async throws -> (data: T, responseAt: Int?) {
decoder: JSONDecoder = ApiFetcher.decoder) async throws -> (data: T, response: ApiResponse<T>) {
let validatedRequest = request.validate(statusCode: ApiFetcher.handledHttpStatus)
let response = await validatedRequest.serializingDecodable(ApiResponse<T>.self,
automaticallyCancelling: true,
Expand All @@ -147,9 +147,9 @@ open class ApiFetcher {
}

open func handleApiResponse<T: Decodable>(_ response: ApiResponse<T>,
responseStatusCode: Int) throws -> (data: T, responseAt: Int?) {
responseStatusCode: Int) throws -> (data: T, response: ApiResponse<T>) {
if let responseData = response.data {
return (responseData, response.responseAt)
return (responseData, response)
} else if let apiError = response.error {
throw InfomaniakError.apiError(apiError)
} else {
Expand Down
3 changes: 3 additions & 0 deletions Sources/InfomaniakCore/Networking/ApiResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ open class ApiResponse<ResponseContent: Decodable>: Decodable {
public let itemsPerPage: Int?
public let responseAt: Int?
public let cursor: String?
public let hasMore: Bool

enum CodingKeys: String, CodingKey {
case result
Expand All @@ -48,6 +49,7 @@ open class ApiResponse<ResponseContent: Decodable>: Decodable {
case itemsPerPage = "items_per_page"
case responseAt = "response_at"
case cursor
case hasMore = "has_more"
}

public required init(from decoder: Decoder) throws {
Expand All @@ -67,5 +69,6 @@ open class ApiResponse<ResponseContent: Decodable>: Decodable {
itemsPerPage = try container.decodeIfPresent(Int.self, forKey: .itemsPerPage)
responseAt = try container.decodeIfPresent(Int.self, forKey: .responseAt)
cursor = try container.decodeIfPresent(String.self, forKey: .cursor)
hasMore = try container.decodeIfPresent(Bool.self, forKey: .hasMore) ?? false
}
}

0 comments on commit 6989d94

Please sign in to comment.