This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,396 additions
and
306 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Debug Tests", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "${file}", | ||
"purpose": ["debug-test"], | ||
"console": "integratedTerminal", | ||
"justMyCode": false | ||
} | ||
] | ||
} |
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,5 @@ | ||
{ | ||
"editor.quickSuggestions": { | ||
"comments": true | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Empty file.
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 was deleted.
Oops, something went wrong.
148 changes: 148 additions & 0 deletions
148
src/meteringpoints_api/endpoints/create_meteringpoint_relations.py
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,148 @@ | ||
from typing import List, Optional | ||
from dataclasses import dataclass | ||
|
||
from origin.api import Endpoint, Context | ||
from origin.models.meteringpoints import MeteringPoint | ||
from meteringpoints_shared.config import DATASYNC_BASE_URL | ||
from meteringpoints_shared.datasync_httpclient import DataSyncHttpClient | ||
|
||
|
||
class CreateMeteringPointRelations(Endpoint): | ||
"""Returns details about a single MeteringPoint.""" | ||
|
||
@dataclass | ||
class Request: | ||
"""TODO.""" | ||
|
||
tin: Optional[str] | ||
ssn: Optional[str] = None | ||
|
||
@dataclass | ||
class Response: | ||
"""TODO.""" | ||
|
||
success: bool | ||
|
||
def handle_request( | ||
self, | ||
request: Request, | ||
context: Context, | ||
) -> Response: | ||
"""Handle HTTP request.""" | ||
|
||
http_client = DataSyncHttpClient( | ||
base_url=DATASYNC_BASE_URL, | ||
internal_token=context.internal_token_encoded, | ||
) | ||
|
||
try: | ||
meteringpoint_ids = get_meteringpoint_ids( | ||
datasync_http_client=http_client, | ||
request=request, | ||
) | ||
|
||
create_meteringpoint_relationships( | ||
datasync_http_client=http_client, | ||
request=request, | ||
meteringpoint_ids=meteringpoint_ids, | ||
) | ||
|
||
except Exception as error: | ||
print(f'failed to create relationship: {error}') | ||
return self.Response( | ||
success=False, | ||
) | ||
|
||
return self.Response( | ||
success=True, | ||
) | ||
|
||
# --- Helper functions ------------------------------------- | ||
|
||
|
||
def get_meteringpoint_ids( | ||
datasync_http_client: DataSyncHttpClient, | ||
request: CreateMeteringPointRelations.Request | ||
) -> List[str]: | ||
""" | ||
Return list of meteringpoint ids, belonging to user. | ||
:param datasync_http_client: Datasync http client | ||
:type datasync_http_client: DataSyncHttpClient | ||
:param request: Request object | ||
:type request: CreateMeteringPointRelations.Request | ||
:raises Exception: Raises exception on any kind of error | ||
:return: List of meteringpoint ids | ||
:rtype: List[str] | ||
""" | ||
meteringpoints: List[str] = [] | ||
|
||
if request.tin: | ||
meteringpoints = datasync_http_client.get_meteringpoints_by_tin( | ||
request.tin | ||
) | ||
elif request.ssn: | ||
# IMPLEMENT THIS | ||
print("httpClient.get_meteringpoints_by_ssn() not implemented") | ||
pass | ||
else: | ||
raise Exception( | ||
"Either request.tin or request.ssn needs to be defined" | ||
) | ||
|
||
meteringpoint_ids = [MeteringPoint(gsrn=mp) for mp in meteringpoints] | ||
|
||
if not meteringpoint_ids: | ||
raise Exception("No meteringpoints found for user") | ||
|
||
return meteringpoint_ids | ||
|
||
|
||
def create_meteringpoint_relationships( | ||
datasync_http_client: DataSyncHttpClient, | ||
request: CreateMeteringPointRelations.Request, | ||
meteringpoint_ids: List[str] | ||
) -> bool: | ||
""" | ||
Create relationships between user and provided meteringpoint. | ||
Calls datasync service to create relationships between meterinpoints | ||
and user. | ||
:param datasync_http_client: DataSync http client | ||
:type datasync_http_client: DataSyncHttpClient | ||
:param request: Request object | ||
:type request: CreateMeteringPointRelations.Request | ||
:param meteringpoint_ids: Meteringpoint ids(gsrn) | ||
:type meteringpoint_ids: List[str] | ||
:raises Exception: Raised on any error | ||
:return: True if successful False on error | ||
:rtype: bool | ||
""" | ||
if request.tin: | ||
name_id = request.tin | ||
elif request.ssn: | ||
name_id = request.ssn | ||
else: | ||
raise Exception( | ||
"Either request.tin or request.ssn needs to be defined" | ||
) | ||
|
||
meteringpoint_relationship_results = datasync_http_client.\ | ||
create_meteringpoint_relationships( | ||
name_id=name_id, | ||
meteringpoint_ids=meteringpoint_ids, | ||
) | ||
|
||
if len(meteringpoint_relationship_results.failed_relationships) != 0: | ||
fail_count = len( | ||
meteringpoint_relationship_results.failed_relationships | ||
) | ||
mp_count = len(meteringpoint_ids) | ||
|
||
raise Exception( | ||
f'Failed to create {fail_count}/{mp_count} relationships' | ||
) | ||
|
||
return True |
49 changes: 49 additions & 0 deletions
49
src/meteringpoints_api/endpoints/get_meteringpoint_details.py
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,49 @@ | ||
# Standard Library | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
# First party | ||
from meteringpoints_shared.db import db | ||
from meteringpoints_shared.queries import ( | ||
MeteringPointQuery, | ||
) | ||
from origin.api import Context, Endpoint | ||
from origin.models.meteringpoints import ( | ||
MeteringPoint, | ||
) | ||
|
||
|
||
class GetMeteringPointDetails(Endpoint): | ||
"""Returns details about a single MeteringPoint.""" | ||
|
||
@dataclass | ||
class Request: | ||
"""TODO.""" | ||
|
||
gsrn: str | ||
|
||
@dataclass | ||
class Response: | ||
"""TODO.""" | ||
|
||
success: bool | ||
meteringpoint: Optional[MeteringPoint] | ||
|
||
@db.session() | ||
def handle_request( | ||
self, | ||
request: Request, | ||
context: Context, | ||
session: db.Session, | ||
) -> Response: | ||
"""Handle HTTP request.""" | ||
|
||
meteringpoint = MeteringPointQuery(session) \ | ||
.is_accessible_by(context.token.subject) \ | ||
.has_gsrn(request.gsrn) \ | ||
.one_or_none() | ||
|
||
return self.Response( | ||
success=meteringpoint is not None, | ||
meteringpoint=meteringpoint, | ||
) |
Oops, something went wrong.