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 Dec 5, 2023
1 parent 80eb641 commit 60313af
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 @@ -129,7 +129,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 response = await request.serializingDecodable(ApiResponse<T>.self,
automaticallyCancelling: true,
decoder: decoder).response
Expand All @@ -138,9 +138,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 60313af

Please sign in to comment.