Skip to content

Commit

Permalink
Merge pull request #133 from IZIVIA/feat/make-url-not-unique
Browse files Browse the repository at this point in the history
feat: rename partnerUrl parameter to partnerId in search method to allow the usage of another field as identifier
  • Loading branch information
lilgallon authored Sep 13, 2024
2 parents e9e5062 + 478385c commit 04783ea
Show file tree
Hide file tree
Showing 27 changed files with 291 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,24 @@ fun authorizationHeader(token: String): Pair<String, String> = Header.AUTHORIZAT
* Creates the authorization header by taking the client token (or the token A if allowed) in the partner repository
*
* @receiver PlatformRepository used to retrieve tokens
* @param partnerUrl partner /versions url
* @param partnerId partner identifier
* @param allowTokenA only true when called on versions / credentials module
* @return Pair<String, String>
*/
suspend fun PartnerRepository.buildAuthorizationHeader(
partnerUrl: String,
partnerId: String,
allowTokenA: Boolean = false
): Pair<String, String> =
if (allowTokenA) {
getCredentialsClientToken(partnerUrl = partnerUrl)
?: getCredentialsTokenA(partnerUrl = partnerUrl)
getCredentialsClientToken(partnerId = partnerId)
?: getCredentialsTokenA(partnerId = partnerId)
?: throw throw OcpiClientUnknownTokenException(
"Could not find token A or client token associated with partner $partnerUrl"
"Could not find token A or client token associated with partner $partnerId"
)
} else {
getCredentialsClientToken(partnerUrl = partnerUrl)
getCredentialsClientToken(partnerId = partnerId)
?: throw throw OcpiClientUnknownTokenException(
"Could not find client token associated with partner $partnerUrl"
"Could not find client token associated with partner $partnerId"
)
}
.let { token -> authorizationHeader(token = token) }
Expand All @@ -123,18 +123,18 @@ suspend fun PartnerRepository.buildAuthorizationHeader(
* repository.
*
* @param partnerRepository use to retrieve tokens
* @param partnerUrl partner /versions url
* @param partnerId partner identifier
* @param allowTokenA only true when called on versions / credentials module
*/
suspend fun HttpRequest.authenticate(
partnerRepository: PartnerRepository,
partnerUrl: String,
partnerId: String,
allowTokenA: Boolean = false
): AuthenticatedHttpRequest =
withHeaders(
headers = headers.plus(
partnerRepository.buildAuthorizationHeader(
partnerUrl = partnerUrl,
partnerId = partnerId,
allowTokenA = allowTokenA
)
)
Expand Down Expand Up @@ -381,11 +381,11 @@ suspend fun PartnerRepository.checkToken(

suspend fun TransportClientBuilder.buildFor(
module: ModuleID,
partnerUrl: String,
partnerId: String,
partnerRepository: PartnerRepository
): TransportClient =
partnerRepository
.getEndpoints(partnerUrl = partnerUrl)
.getEndpoints(partnerId = partnerId)
.find { it.identifier == module }
?.let { build(baseUrl = it.url) }
?: throw OcpiToolkitUnknownEndpointException(endpointName = module.name)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.izivia.ocpi.toolkit.transport.domain.HttpRequest

suspend inline fun <reified T> getNextPage(
transportClientBuilder: TransportClientBuilder,
serverVersionsEndpointUrl: String,
partnerId: String,
partnerRepository: PartnerRepository,
previousResponse: OcpiResponseBody<SearchResult<T>>
): OcpiResponseBody<SearchResult<T>>? =
Expand All @@ -21,7 +21,7 @@ suspend inline fun <reified T> getNextPage(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parsePaginatedBody(previousResponse.data.offset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import com.izivia.ocpi.toolkit.transport.domain.HttpRequest
/**
* Sends calls to an eMSP server
* @property transportClientBuilder used to build transport client
* @property serverVersionsEndpointUrl used to know which partner to communicate with
* @property partnerId used to know which partner to communicate with
* @property partnerRepository used to get information about the partner (endpoint, token)
*/
class CdrsCpoClient(
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsEndpointUrl: String,
private val partnerId: String,
private val partnerRepository: PartnerRepository
) : CdrsEmspInterface<URL> {
private suspend fun buildTransport(): TransportClient = transportClientBuilder
.buildFor(
module = ModuleID.cdrs,
partnerUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository
)

Expand All @@ -37,7 +37,7 @@ class CdrsCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand All @@ -52,7 +52,7 @@ class CdrsCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import java.time.Instant

class CdrsEmspClient(
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsEndpointUrl: String,
private val partnerId: String,
private val partnerRepository: PartnerRepository
) : CdrsCpoInterface {
private suspend fun buildTransport(): TransportClient = transportClientBuilder
.buildFor(
module = ModuleID.cdrs,
partnerUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository
)

Expand All @@ -42,7 +42,7 @@ class CdrsEmspClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parsePaginatedBody(offset)
}
Expand All @@ -51,7 +51,7 @@ class CdrsEmspClient(
previousResponse: OcpiResponseBody<SearchResult<Cdr>>
): OcpiResponseBody<SearchResult<Cdr>>? = getNextPage(
transportClientBuilder = transportClientBuilder,
serverVersionsEndpointUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository,
previousResponse = previousResponse
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import com.izivia.ocpi.toolkit.transport.domain.HttpStatus
* Send calls to the SCSP
*
* @property transportClientBuilder used to build transport client
* @property serverVersionsEndpointUrl used to know which partner to communicate with
* @property partnerId used to know which partner to communicate with
* @property partnerRepository used to get information about the partner (endpoint, token)
* @property callbackBaseUrl used to build the callback URL sent to the other partner
*/
class ChargingProfilesCpoClient(
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsEndpointUrl: String,
private val partnerId: String,
private val partnerRepository: PartnerRepository,
private val callbackBaseUrl: String
) {
Expand All @@ -35,7 +35,7 @@ class ChargingProfilesCpoClient(
private suspend fun buildTransport(): TransportClient = transportClientBuilder
.buildFor(
module = ModuleID.chargingprofiles,
partnerUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository
)

Expand All @@ -53,7 +53,7 @@ class ChargingProfilesCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand All @@ -75,7 +75,7 @@ class ChargingProfilesCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand All @@ -97,7 +97,7 @@ class ChargingProfilesCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand All @@ -119,7 +119,7 @@ class ChargingProfilesCpoClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import com.izivia.ocpi.toolkit.transport.domain.HttpStatus

class ChargingProfilesScspClient(
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsEndpointUrl: String,
private val partnerId: String,
private val partnerRepository: PartnerRepository,
private val callbackBaseUrl: URL
) {

private suspend fun buildTransport(): TransportClient = transportClientBuilder
.buildFor(
module = ModuleID.chargingprofiles,
partnerUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository
)

Expand All @@ -46,7 +46,7 @@ class ChargingProfilesScspClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand Down Expand Up @@ -75,7 +75,7 @@ class ChargingProfilesScspClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK && it.status != HttpStatus.CREATED) {
Expand All @@ -102,7 +102,7 @@ class ChargingProfilesScspClient(
requestId = generateRequestId(),
correlationId = generateCorrelationId()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.also {
if (it.status != HttpStatus.OK) throw HttpException(it.status, "status should be ${HttpStatus.OK}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommandCpoClient(
) {
suspend fun postCommandCallback(
commandResult: CommandResult,
partnerUrl: String,
partnerId: String,
responseUrl: String
): OcpiResponseBody<Any> =
transportClientBuilder
Expand All @@ -28,6 +28,6 @@ class CommandCpoClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = partnerUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
).parseBody()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import java.time.Instant

class CommandEmspClient(
private val transportClientBuilder: TransportClientBuilder,
private val serverVersionsEndpointUrl: String,
private val partnerId: String,
private val partnerRepository: PartnerRepository,
private val callbackBaseUrl: String
) {

private suspend fun buildTransport(): TransportClient = transportClientBuilder
.buildFor(
module = ModuleID.commands,
partnerUrl = serverVersionsEndpointUrl,
partnerId = partnerId,
partnerRepository = partnerRepository
)

Expand Down Expand Up @@ -52,7 +52,7 @@ class CommandEmspClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand All @@ -74,7 +74,7 @@ class CommandEmspClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand Down Expand Up @@ -108,7 +108,7 @@ class CommandEmspClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand All @@ -130,7 +130,7 @@ class CommandEmspClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand Down Expand Up @@ -159,7 +159,7 @@ class CommandEmspClient(
requestId = generateUUIDv4Token(),
correlationId = generateUUIDv4Token()
)
.authenticate(partnerRepository = partnerRepository, partnerUrl = serverVersionsEndpointUrl)
.authenticate(partnerRepository = partnerRepository, partnerId = partnerId)
)
.parseBody()
}
Expand Down
Loading

0 comments on commit 04783ea

Please sign in to comment.