-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from IZIVIA/IDEV-130
Tokens module
- Loading branch information
Showing
68 changed files
with
4,542 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...it-2.1.1-gireve/src/main/kotlin/com/izivia/ocpi/toolkit/modules/tokens/TokensCpoClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.izivia.ocpi.toolkit.modules.tokens | ||
|
||
import com.izivia.ocpi.toolkit.common.* | ||
import com.izivia.ocpi.toolkit.modules.credentials.repositories.PlatformRepository | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.AuthorizationInfo | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.LocationReferences | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.Token | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenType | ||
import com.izivia.ocpi.toolkit.modules.versions.domain.ModuleID | ||
import com.izivia.ocpi.toolkit.transport.TransportClient | ||
import com.izivia.ocpi.toolkit.transport.TransportClientBuilder | ||
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod | ||
import com.izivia.ocpi.toolkit.transport.domain.HttpRequest | ||
import java.time.Instant | ||
|
||
class TokensCpoClient( | ||
private val transportClientBuilder: TransportClientBuilder, | ||
private val serverVersionsEndpointUrl: String, | ||
private val platformRepository: PlatformRepository | ||
) : TokensEmspInterface { | ||
|
||
private fun buildTransport(): TransportClient = transportClientBuilder | ||
.buildFor( | ||
module = ModuleID.tokens, | ||
platform = serverVersionsEndpointUrl, | ||
platformRepository = platformRepository | ||
) | ||
|
||
override fun getTokens( | ||
dateFrom: Instant?, | ||
dateTo: Instant?, | ||
offset: Int, | ||
limit: Int?, | ||
countryCode: String?, | ||
partyId: String? | ||
): OcpiResponseBody<SearchResult<Token>> = | ||
buildTransport() | ||
.send( | ||
HttpRequest( | ||
method = HttpMethod.GET, | ||
queryParams = listOfNotNull( | ||
dateFrom?.let { "date_from" to dateFrom.toString() }, | ||
dateTo?.let { "date_to" to dateTo.toString() }, | ||
"offset" to offset.toString(), | ||
limit?.let { "limit" to limit.toString() }, | ||
countryCode?.let { "ocpi-to-country-code" to countryCode }, | ||
partyId?.let { "ocpi-to-party-id" to partyId } | ||
).toMap() | ||
).authenticate(platformRepository = platformRepository, baseUrl = serverVersionsEndpointUrl) | ||
) | ||
.parsePaginatedBody(offset) | ||
|
||
override fun getToken(tokenUid: String, tokenType: TokenType): OcpiResponseBody<Token?> = | ||
buildTransport() | ||
.send( | ||
HttpRequest( | ||
method = HttpMethod.GET, | ||
path = "/$tokenUid", | ||
queryParams = mapOf("type" to tokenType.name), | ||
).authenticate(platformRepository = platformRepository, baseUrl = serverVersionsEndpointUrl) | ||
) | ||
.parseBody() | ||
|
||
override fun postToken( | ||
tokenUid: String, | ||
tokenType: TokenType, | ||
locationReferences: LocationReferences | ||
): OcpiResponseBody<AuthorizationInfo> = | ||
buildTransport() | ||
.send( | ||
HttpRequest( | ||
method = HttpMethod.POST, | ||
path = "/$tokenUid/authorize", | ||
queryParams = mapOf("type" to tokenType.name), | ||
body = locationReferences.run(mapper::writeValueAsString) | ||
).authenticate(platformRepository = platformRepository, baseUrl = serverVersionsEndpointUrl) | ||
) | ||
.parseBody() | ||
} |
66 changes: 66 additions & 0 deletions
66
...2.1.1-gireve/src/main/kotlin/com/izivia/ocpi/toolkit/modules/tokens/TokensCpoInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.izivia.ocpi.toolkit.modules.tokens | ||
|
||
import com.izivia.ocpi.toolkit.common.OcpiResponseBody | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.Token | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenPartial | ||
|
||
/** | ||
* With this interface the eMSP can push the Token information to the CPO. Tokens is a client owned object, so the | ||
* end-points need to contain the required extra fields: {party_id} and {country_code}. Example endpoint structure: | ||
* /ocpi/cpo/2.0/tokens/{country_code}/{party_id}/{token_uid} | ||
* | ||
* - GET: Retrieve a Token as it is stored in the CPO system. | ||
* - POST: n/a | ||
* - PUT: Push new/updated Token object to the CPO. | ||
* - PATCH: Notify the CPO of partial updates to a Token. | ||
* - DELETE: n/a, (Use PUT, Tokens cannot be removed). | ||
*/ | ||
interface TokensCpoInterface { | ||
|
||
/** | ||
* If the eMSP wants to check the status of a Token in the CPO system it might GET the object from the CPO system | ||
* for validation purposes. The eMSP is the owner of the objects, so it would be illogical if the CPO system had a | ||
* different status or was missing an object. | ||
* | ||
* @param countryCode (max-length 2) Country code of the eMSP requesting this GET from the CPO system. | ||
* @param partyId (max-length 3) Party ID (Provider ID) of the eMSP requesting this GET from the CPO system. | ||
* @param tokenUid (max-length 36) Token.uid of the Token object to retrieve. | ||
* @return The requested Token object. | ||
*/ | ||
fun getToken( | ||
countryCode: String, | ||
partyId: String, | ||
tokenUid: String | ||
): OcpiResponseBody<Token?> | ||
|
||
/** | ||
* New or updated Token objects are pushed from the eMSP to the CPO. | ||
* | ||
* @param countryCode (max-length 2) Country code of the eMSP sending this PUT request to the CPO system. | ||
* @param partyId (max-length 3) Party ID (Provider ID) of the eMSP sending this PUT request to the CPO system. | ||
* @param tokenUid (max-length 36) Token.uid of the (new) Token object (to replace). | ||
* @param token New or updated Token object. | ||
*/ | ||
fun putToken( | ||
countryCode: String, | ||
partyId: String, | ||
tokenUid: String, | ||
token: Token | ||
): OcpiResponseBody<Token> | ||
|
||
/** | ||
* Same as the PUT method, but only the fields/objects that have to be updated have to be present, other | ||
* fields/objects that are not specified are considered unchanged. | ||
* | ||
* @param countryCode (max-length 2) Country code of the eMSP sending this PUT request to the CPO system. | ||
* @param partyId (max-length 3) Party ID (Provider ID) of the eMSP sending this PUT request to the CPO system. | ||
* @param tokenUid (max-length 36) Token.uid of the (new) Token object (to replace). | ||
* @param token New or updated Token object. | ||
*/ | ||
fun patchToken( | ||
countryCode: String, | ||
partyId: String, | ||
tokenUid: String, | ||
token: TokenPartial | ||
): OcpiResponseBody<Token> | ||
} |
82 changes: 82 additions & 0 deletions
82
...it-2.1.1-gireve/src/main/kotlin/com/izivia/ocpi/toolkit/modules/tokens/TokensCpoServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.izivia.ocpi.toolkit.modules.tokens | ||
|
||
import com.izivia.ocpi.toolkit.common.httpResponse | ||
import com.izivia.ocpi.toolkit.common.mapper | ||
import com.izivia.ocpi.toolkit.common.tokenFilter | ||
import com.izivia.ocpi.toolkit.modules.credentials.repositories.PlatformRepository | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.Token | ||
import com.izivia.ocpi.toolkit.modules.tokens.domain.TokenPartial | ||
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 TokensCpoServer( | ||
private val transportServer: TransportServer, | ||
private val platformRepository: PlatformRepository, | ||
private val service: TokensCpoInterface, | ||
basePath: List<FixedPathSegment> = listOf( | ||
FixedPathSegment("/2.1.1/tokens") | ||
) | ||
) { | ||
init { | ||
transportServer.handle( | ||
method = HttpMethod.GET, | ||
path = basePath + listOf( | ||
VariablePathSegment("countryCode"), | ||
VariablePathSegment("partyId"), | ||
VariablePathSegment("tokenUid") | ||
), | ||
filters = listOf(platformRepository::tokenFilter) | ||
) { req -> | ||
req.httpResponse { | ||
service | ||
.getToken( | ||
countryCode = req.pathParams["countryCode"]!!, | ||
partyId = req.pathParams["partyId"]!!, | ||
tokenUid = req.pathParams["tokenUid"]!! | ||
) | ||
} | ||
} | ||
|
||
transportServer.handle( | ||
method = HttpMethod.PUT, | ||
path = basePath + listOf( | ||
VariablePathSegment("countryCode"), | ||
VariablePathSegment("partyId"), | ||
VariablePathSegment("tokenUid") | ||
), | ||
filters = listOf(platformRepository::tokenFilter) | ||
) { req -> | ||
req.httpResponse { | ||
service | ||
.putToken( | ||
countryCode = req.pathParams["countryCode"]!!, | ||
partyId = req.pathParams["partyId"]!!, | ||
tokenUid = req.pathParams["tokenUid"]!!, | ||
token = mapper.readValue(req.body, Token::class.java) | ||
) | ||
} | ||
} | ||
|
||
transportServer.handle( | ||
method = HttpMethod.PATCH, | ||
path = basePath + listOf( | ||
VariablePathSegment("countryCode"), | ||
VariablePathSegment("partyId"), | ||
VariablePathSegment("tokenUid") | ||
), | ||
filters = listOf(platformRepository::tokenFilter) | ||
) { req -> | ||
req.httpResponse { | ||
service | ||
.patchToken( | ||
countryCode = req.pathParams["countryCode"]!!, | ||
partyId = req.pathParams["partyId"]!!, | ||
tokenUid = req.pathParams["tokenUid"]!!, | ||
token = mapper.readValue(req.body!!, TokenPartial::class.java) | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.