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

Create ChargingModuleRequestLib #129

Merged
merged 12 commits into from
Feb 22, 2023
Merged

Conversation

StuAA78
Copy link
Contributor

@StuAA78 StuAA78 commented Feb 21, 2023

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 and post.

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 the server object around from one service to another, we introduce a new GlobalHapiServerMethodsPlugin, 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 calling global.HapiServerMethods.getChargingModuleToken(). In testing we confirmed that calling this and calling server.methods.getChargingModuleToken() gives the same token; however, for consistency we intend to use global.HapiServerMethods.getChargingModuleToken() at all times.

Note that existing code which sends requests to the CM will be updated in a separate PR.

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.
@StuAA78 StuAA78 added the enhancement New feature or request label Feb 21, 2023
@StuAA78 StuAA78 self-assigned this Feb 21, 2023
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()`
@StuAA78 StuAA78 marked this pull request as ready for review February 22, 2023 14:47
Copy link
Member

@Cruikshanks Cruikshanks left a comment

Choose a reason for hiding this comment

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

@StuAA78 StuAA78 merged commit 5055ec2 into main Feb 22, 2023
@StuAA78 StuAA78 deleted the create-charging-module-request-lib branch February 22, 2023 15:45
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
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants