generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
110 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from flask import request | ||
import app.validators.request_validator as request_validator | ||
|
||
|
||
def batch(): | ||
json_data = request.json or {} | ||
valid_headers, error_message = request_validator.verify_headers(dict(request.headers)) | ||
|
||
if not valid_headers: | ||
return {"status": error_message}, 401 | ||
|
||
if not request_validator.verify_signature(dict(request.headers), json_data): | ||
return {"status": "Invalid signature"}, 403 | ||
|
||
valid_body, error_message = request_validator.verify_body(json_data) | ||
|
||
if not valid_body: | ||
return {"status": error_message}, 422 | ||
|
||
return {"status": "success"}, 200 |
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,31 @@ | ||
import app.utils.access_token as access_token | ||
import logging | ||
import os | ||
import requests | ||
import uuid | ||
|
||
|
||
def notify(body: dict) -> dict: | ||
response = requests.post(url(), json=body, headers=headers()) | ||
|
||
logging.info(f"Response from Notify API {url()}: {response.status_code}") | ||
|
||
if response.status_code == 201: | ||
logging.debug(response.text) | ||
else: | ||
logging.error(response.text) | ||
|
||
return response.json() | ||
|
||
|
||
def headers() -> dict: | ||
return { | ||
"content-type": "application/vnd.api+json", | ||
"accept": "application/vnd.api+json", | ||
"x-correlation-id": str(uuid.uuid4()), | ||
"authorization": "Bearer " + access_token.get_token(), | ||
} | ||
|
||
|
||
def url() -> str: | ||
return f"{os.getenv("NOTIFY_API_URL")}/comms/v1/message-batches" |
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,26 @@ | ||
from sqlalchemy import engine, Session | ||
import database.models as models | ||
|
||
|
||
def save_message_batch(batch_reference: str, data: dict, response: dict, status: str) -> tuple[bool, str]: | ||
batch = models.MessageBatch( | ||
batch_id=response["data"]["id"], | ||
batch_reference=batch_reference, | ||
details=data, | ||
response=response, | ||
status=status, | ||
) | ||
with Session(engine) as session: | ||
session.add(batch) | ||
session.flush() | ||
|
||
|
||
for message_data in data["attributes"]["messages"]: | ||
models.Message( | ||
batch_id=batch.id, | ||
details=message_data, | ||
message_id=message_data["id"], | ||
message_reference=message_data["messageReference"], | ||
nhs_number=message_data["nhs_number"], | ||
recipient_id=message_data["recipient_id"], | ||
) |
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