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(versions) #61: VersionDetails based on registered OcpiModuleServers #64

Merged
merged 4 commits into from
Jan 17, 2024
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,31 @@
package com.izivia.ocpi.toolkit.common

import com.izivia.ocpi.toolkit.modules.versions.domain.Endpoint
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer

abstract class OcpiSelfRegisteringModuleServer(
private val ocpiVersion: VersionNumber,
private val moduleID: ModuleID,
private val interfaceRole: InterfaceRole,
private val versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiModuleServer(basePathOverride ?: "/${ocpiVersion.value}/${moduleID.name}") {

protected abstract suspend fun doRegisterOn(transportServer: TransportServer)

override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository?.addEndpoint(
ocpiVersion,
Endpoint(
identifier = moduleID,
role = interfaceRole,
url = "${transportServer.baseUrl()}$basePath"
)
)
doRegisterOn(transportServer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ package com.izivia.ocpi.toolkit.modules.credentials
import com.izivia.ocpi.toolkit.common.*
import com.izivia.ocpi.toolkit.modules.credentials.domain.Credentials
import com.izivia.ocpi.toolkit.modules.credentials.services.CredentialsServerService
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod

class CredentialsServer(
private val service: CredentialsServerService,
basePath: String = "/2.2.1/credentials"
) : OcpiModuleServer(basePath) {
override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.credentials,
// role irrelevant for module credentials, but docs suggest using SENDER
interfaceRole = InterfaceRole.SENDER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun doRegisterOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ class CredentialsClientService(
.let {
it.data ?: throw OcpiResponseException(it.status_code, it.status_message ?: "unknown")
}
val availableClientVersions = clientVersionsRepository.getVersions()
val availableClientVersionNumbers = clientVersionsRepository.getVersions()

// Get available versions and pick latest mutual
val latestMutualVersion = availableServerVersions
.sortedByDescending { clientVersion -> parseVersionNumber(clientVersion.version)!!.index }
.firstOrNull { serverVersion ->
availableClientVersions
.any { clientVersion -> serverVersion.version == clientVersion.version }
availableClientVersionNumbers
.any { clientVersionNumber -> serverVersion.version == clientVersionNumber.value }
}
?: throw OcpiServerUnsupportedVersionException(
"Could not find mutual version with partner $serverVersionsEndpointUrl"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.izivia.ocpi.toolkit.modules.locations

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment
import java.time.Instant

/**
* Receives calls from a CPO
* @property transportServer
*/
class LocationsCpoServer(
private val service: LocationsCpoInterface,
basePath: String = "/2.2.1/locations"
) : OcpiModuleServer(basePath) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.locations,
interfaceRole = InterfaceRole.SENDER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun registerOn(transportServer: TransportServer) {
override suspend fun doRegisterOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.izivia.ocpi.toolkit.modules.locations

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.common.mapper
import com.izivia.ocpi.toolkit.modules.locations.domain.*
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment

/**
* Receives calls from a CPO
* @property transportServer
*/
class LocationsEmspServer(
private val service: LocationsEmspInterface,
basePath: String = "/2.2.1/locations"
) : OcpiModuleServer(basePath) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.locations,
interfaceRole = InterfaceRole.RECEIVER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun registerOn(transportServer: TransportServer) {
override suspend fun doRegisterOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments + listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package com.izivia.ocpi.toolkit.modules.sessions

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.common.mapper
import com.izivia.ocpi.toolkit.modules.sessions.domain.ChargingPreferences
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.FixedPathSegment
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment
import java.time.Instant

/**
* Receives calls from a CPO
* @property transportServer
*/
class SessionsCpoServer(
private val service: SessionsCpoInterface,
basePath: String = "/2.2.1/sessions"
) : OcpiModuleServer(basePath) {
override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.sessions,
interfaceRole = InterfaceRole.SENDER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun doRegisterOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package com.izivia.ocpi.toolkit.modules.sessions

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.common.mapper
import com.izivia.ocpi.toolkit.modules.sessions.domain.Session
import com.izivia.ocpi.toolkit.modules.sessions.domain.SessionPartial
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment

/**
* Receives calls from a CPO
* @property transportServer
*/
class SessionsEmspServer(
private val service: SessionsEmspInterface,
basePath: String = "/2.2.1/sessions"
) : OcpiModuleServer(basePath) {
override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.sessions,
interfaceRole = InterfaceRole.RECEIVER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun doRegisterOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments + listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package com.izivia.ocpi.toolkit.modules.tokens

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.common.mapper
import com.izivia.ocpi.toolkit.modules.tokens.domain.Token
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenPartial
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenType
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment

/**
* Receives calls from a CPO
* @property transportServer
*/
class TokensCpoServer(
private val service: TokensCpoInterface,
basePath: String = "/2.2.1/tokens"
) : OcpiModuleServer(basePath) {
override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.tokens,
interfaceRole = InterfaceRole.SENDER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun doRegisterOn(transportServer: TransportServer) {
// Get Method
transportServer.handle(
method = HttpMethod.GET,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package com.izivia.ocpi.toolkit.modules.tokens

import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.OcpiSelfRegisteringModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.common.mapper
import com.izivia.ocpi.toolkit.modules.tokens.domain.LocationReferences
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenType
import com.izivia.ocpi.toolkit.modules.versions.domain.InterfaceRole
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID
import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.modules.versions.repositories.MutableVersionsRepository
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.FixedPathSegment
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment
import java.time.Instant

/**
* Receives calls from a CPO
* @property transportServer
*/
class TokensEmspServer(
private val service: TokensEmspInterface,
basePath: String = "/2.2.1/tokens"
) : OcpiModuleServer(basePath) {
override suspend fun registerOn(transportServer: TransportServer) {
versionsRepository: MutableVersionsRepository? = null,
basePathOverride: String? = null
) : OcpiSelfRegisteringModuleServer(
ocpiVersion = VersionNumber.V2_2_1,
moduleID = ModuleID.tokens,
interfaceRole = InterfaceRole.RECEIVER,
versionsRepository = versionsRepository,
basePathOverride = basePathOverride
) {

override suspend fun doRegisterOn(transportServer: TransportServer) {
// GET Token
transportServer.handle(
method = HttpMethod.GET,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ import com.izivia.ocpi.toolkit.common.OcpiModuleServer
import com.izivia.ocpi.toolkit.common.httpResponse
import com.izivia.ocpi.toolkit.modules.versions.services.VersionsService
import com.izivia.ocpi.toolkit.transport.TransportServer
import com.izivia.ocpi.toolkit.transport.domain.FixedPathSegment
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.VariablePathSegment

class VersionsServer(
private val service: VersionsService,
basePath: String = "/versions"
) : OcpiModuleServer(basePath) {
private val service: VersionsService
) : OcpiModuleServer("") {

override suspend fun registerOn(transportServer: TransportServer) {
transportServer.handle(
method = HttpMethod.GET,
path = basePathSegments
path = listOf(FixedPathSegment(service.versionsBasePath))
) { req ->
req.httpResponse {
service.getVersions()
}
}

transportServer.handle(
method = HttpMethod.GET,
path = listOf(
FixedPathSegment(service.versionDetailsBasePath),
VariablePathSegment("versionNumber")
)
) { req ->
req.httpResponse {
service.getVersionDetails(
versionNumber = req.pathParams["versionNumber"]!!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonarcloud was complaining about that. I marked this issue as won't fix because handle implementation has the responsability to make sure that specified pathParams exist

)
}
}
}
}
Loading