-
Notifications
You must be signed in to change notification settings - Fork 0
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
Create ChargingModuleRequestLib
#129
Merged
Merged
Conversation
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
https://eaflood.atlassian.net/browse/WATER-3903 Currently we have RequestLib which we use to make requests to external services. Having developed code which sends requests to the Charging Module we can see that there will be things we need to do which will be common to all CM requests -- for example, obtaining an access token, formatting the response in a particular way, etc. Before we go any further with developing more services which send requests to the CM we want to create a general ChargingModuleRequestLib which will do all of this for us, allowing our services to concentrate on what they actually need to do and not all the stuff surrounding the requests.
We want to be able to access our `getChargingModuleToken()` server method in order to get and cache a Cognito token. However this means we need to pass something around from one service to another until it reaches our lib -- not ideal given that the CM request could be several services deep (plus if we pass a token around it could potentially expire). We therefore implement a `GlobalHapiServerMethodsPlugin` which lets us access them globally as `HapiServerMethods`, eg. `global.HapiServerMethods.getChargingModuleToken()`
Cruikshanks
approved these changes
Feb 22, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StuAA78
added a commit
that referenced
this pull request
Feb 27, 2023
https://eaflood.atlassian.net/browse/WATER-3903 We [previously](#129) introduced `ChargingModuleRequestLib` to handle requests to the Charging Module -- obtaining a Cognito token, formatting the response, etc. This PR updates our existing code to use it. While doing this we realise that we need to make changes to `ChargingModuleRequestLib` to ensure it plays nicely with calling services. We therefore no longer manipulate the response -- for example, we previously overwrite it with the contents of the response body, meaning we lost things like http headers, status code etc. We now leave the body where it is. We also amend how `ChargingModuleCreateBillRunService` returns the created bill run to again remove manipulation of the response. The CM returns bill run data in a `billRun` object within its response; we previously moved the contents out so they weren't nested within this object but we now leave the `billRun` object alone. This means we don't need to make special allowances for things like error responses (ie. "if the response has no body then it is a network error so don't manipulate it, otherwise if it has a body and a bill run then it is a valid response so manipulate it, otherwise if it has a body but no bill run then it is an http error so don't manipulate it").
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
https://eaflood.atlassian.net/browse/WATER-3903
Currently we have
RequestLib
which we use to make requests to external services. Having developed code which sends requests to the Charging Module we can see that there will be things we need to do which will be common to all CM requests -- for example, obtaining an access token, formatting the response in a particular way, etc.Before we go any further with developing more services which send requests to the CM we want to create a general
ChargingModuleRequestLib
which will do all of this for us, allowing our services to concentrate on what they actually need to do and not all the stuff surrounding the requests.This PR implements
ChargingModuleRequestLib
and two initial methods,get
andpost
.As part of this, we want to access our server method
getChargingModuleToken()
to ensure that Cognito tokens are cached for all requests. In order to do this without passing theserver
object around from one service to another, we introduce a newGlobalHapiServerMethodsPlugin
, which adds the Hapi server methods object to the global object, allowing us to access them from anywhere; this means we can get a token by callingglobal.HapiServerMethods.getChargingModuleToken()
. In testing we confirmed that calling this and callingserver.methods.getChargingModuleToken()
gives the same token; however, for consistency we intend to useglobal.HapiServerMethods.getChargingModuleToken()
at all times.Note that existing code which sends requests to the CM will be updated in a separate PR.