Skip to content

Commit

Permalink
Merge pull request #94 from andacata/feat/86_use_link_header_for_pagi…
Browse files Browse the repository at this point in the history
…nation

feat(common) #86: Add the ability to use the Link header for getting next pages from paginated responses
  • Loading branch information
lilgallon authored Mar 25, 2024
2 parents 35bbea8 + bda0447 commit ecca198
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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

0 comments on commit ecca198

Please sign in to comment.