Slide 62
Slide 62 text
func getReviews(
sort: ReviewSortOrder = .dateDescending,
after previousPage: URL? = nil
) async throws
-
>
ReviewsResponse {
let request = APIEndpoint.v1.apps.id(self.id).customerReviews
.get(
parameters: .init(
sort: [sort.apiSort],
fieldsCustomerReviews: [.body, .createdDate, .rating, .response, .reviewerNickname, .territory, .title],
fieldsCustomerReviewResponses: [.review, .lastModifiedDate, .responseBody,.state],
limit: 100,
include: [.response]
)
)
let reviews: CustomerReviewsResponse
if let previousPage {
let urlComponents = URLComponents(url: previousPage, resolvingAgainstBaseURL: true)
let paginatedRequest = Request(
path: urlComponents
?.
path
??
"",
method: request.method,
query: urlComponents
? .
queryItems
? .
map { ($0.name, $0.value) },
id: request.id
? ?
"paginated_customer_reviews"
)
reviews = try await provider.request(paginatedRequest)
} else {
reviews = try await provider.request(request)
}
let customerReviews = reviews.data.map { reviewData in
let response = reviews.included
?.
first { $0.id
==
reviewData.relationships
?.
response
?.
data
?.
id }
return Review(review: reviewData, response: response)
}
let nextPageURL: URL? = {
guard let nextPageString = reviews.links.next else { return nil }
return URL(string: nextPageString)
}()
return ReviewsResponse(reviews: customerReviews, total: reviews.meta
?.
paging.total, nextPage: nextPageURL)
}