Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common) #86: Add the ability to use the Link header for getting next pages from paginated responses #94

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.izivia.ocpi.toolkit.common

import com.izivia.ocpi.toolkit.modules.credentials.repositories.PartnerRepository
import com.izivia.ocpi.toolkit.transport.TransportClientBuilder
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.HttpRequest

suspend inline fun <reified T> getNextPage(
transportClientBuilder: TransportClientBuilder,
serverVersionsEndpointUrl: String,
partnerRepository: PartnerRepository,
previousResponse: OcpiResponseBody<SearchResult<T>>
): OcpiResponseBody<SearchResult<T>>? =
previousResponse.data?.nextPageUrl?.let { nextPageUrl ->
with(transportClientBuilder.build(nextPageUrl)) {
send(
HttpRequest(
method = HttpMethod.GET
)
.withRequiredHeaders(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
)
.parsePaginatedBody(previousResponse.data.offset)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class LocationsEmspClient(
.parsePaginatedBody(offset)
}

suspend fun getLocationsNextPage(
previousResponse: OcpiResponseBody<SearchResult<Location>>
): OcpiResponseBody<SearchResult<Location>>? = getNextPage(
transportClientBuilder = transportClientBuilder,
serverVersionsEndpointUrl = serverVersionsEndpointUrl,
partnerRepository = partnerRepository,
previousResponse = previousResponse
)

override suspend fun getLocation(locationId: CiString): OcpiResponseBody<Location?> = with(buildTransport()) {
send(
HttpRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class SessionsEmspClient(
.parsePaginatedBody(offset)
}

suspend fun getSessionsNextPage(
previousResponse: OcpiResponseBody<SearchResult<Session>>
): OcpiResponseBody<SearchResult<Session>>? = getNextPage(
transportClientBuilder = transportClientBuilder,
serverVersionsEndpointUrl = serverVersionsEndpointUrl,
partnerRepository = partnerRepository,
previousResponse = previousResponse
)

override suspend fun putChargingPreferences(
sessionId: CiString,
chargingPreferences: ChargingPreferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class TokensCpoClient(
.parsePaginatedBody(offset)
}

suspend fun getTokensNextPage(
previousResponse: OcpiResponseBody<SearchResult<Token>>
): OcpiResponseBody<SearchResult<Token>>? = getNextPage(
transportClientBuilder = transportClientBuilder,
serverVersionsEndpointUrl = serverVersionsEndpointUrl,
partnerRepository = partnerRepository,
previousResponse = previousResponse
)

override suspend fun postToken(
tokenUid: CiString,
type: TokenType?,
Expand Down
Loading