Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@
"python.analysis.extraPaths": [
"./gateway-api/stubs"
],
"python-envs.defaultEnvManager": "ms-python.python:pyenv",
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ Environment variables control whether stubs are used in place of the real PDS, S
| Variable | Description |
| --- | --- |
| `PDS_URL` | The URL for the PDS FHIR API; set as `stub` to use development stub. |
| `PDS_API_TOKEN`| Leave unset in development environment. |
| `PDS_API_SECRET`| Leave unset in development environment. |
| `PDS_API_KID`| Leave unset in development environment. |
| `PDS_API_TOKEN` | Leave unset in development environment. |
| `PDS_API_SECRET` | Leave unset in development environment. |
| `PDS_API_KID` | Leave unset in development environment. |
| `SDS_URL` | The URL for the SDS FHIR API; set as `stub` to use development stub. |
| `SDS_API_TOKEN`| Leave unset in development environment. |
| `SDS_API_TOKEN` | Leave unset in development environment. |
| `PROVIDER_URL` | The URL for the GP Provider; set as `stub` to use development stub. |
| `CDG_DEBUG` | `true`, return additional debug information when the call to the GP provider returns an error. |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: localInt
variables:
- name: base_url
value: http://localhost:5000
- name: nhs_number
value: "9658218865"
Comment thread
davidhamill1-nhs marked this conversation as resolved.
Outdated
- name: from_ods
value: A20047
5 changes: 4 additions & 1 deletion gateway-api/src/gateway_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def configure_app(app: Flask) -> None:
"FLASK_PORT": get_env_var("FLASK_PORT", int),
"PDS_URL": get_env_var("PDS_URL", str),
"SDS_URL": get_env_var("SDS_URL", str),
"SDS_API_TOKEN": get_env_var("SDS_API_TOKEN", str),
}
app.config.update(config)

Expand Down Expand Up @@ -108,7 +109,9 @@ def get_structured_record() -> Response:
try:
get_structured_record_request = GetStructuredRecordRequest(request)
controller = Controller(
pds_base_url=app.config["PDS_URL"], sds_base_url=app.config["SDS_URL"]
pds_base_url=app.config["PDS_URL"],
sds_base_url=app.config["SDS_URL"],
sds_api_key=app.config["SDS_API_TOKEN"],
)
provider_response = controller.run(request=get_structured_record_request)
response.add_provider_response(provider_response)
Expand Down
3 changes: 3 additions & 0 deletions gateway-api/src/gateway_api/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ def __init__(
self,
pds_base_url: str,
sds_base_url: str,
sds_api_key: str,
timeout: int = 10,
) -> None:
"""
Create a controller instance.
"""
self.pds_base_url = pds_base_url
self.sds_base_url = sds_base_url
self.sds_api_key = sds_api_key
self.timeout = timeout
self.gp_provider_client = None

Expand Down Expand Up @@ -194,6 +196,7 @@ def _get_sds_details(
# SDS: Get provider details (ASID + endpoint) for provider ODS
sds = SdsClient(
base_url=self.sds_base_url,
api_key=self.sds_api_key,
timeout=self.timeout,
)

Expand Down
7 changes: 2 additions & 5 deletions gateway-api/src/gateway_api/sds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from gateway_api.sds.client import SdsClient
from gateway_api.sds.client import SdsClient, get
from gateway_api.sds.search_results import SdsSearchResults

__all__ = [
"SdsClient",
"SdsSearchResults",
]
__all__ = ["SdsClient", "SdsSearchResults", "get"]
44 changes: 19 additions & 25 deletions gateway-api/src/gateway_api/sds/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from fhir import Resource
from fhir.constants import FHIRSystem
from fhir.r4 import Bundle, Device, Endpoint
from requests import HTTPError
from requests import HTTPError, Response
from requests import get as external_sds_get
from stubs import SdsFhirApiStub

from gateway_api.common.error import SdsRequestFailedError
from gateway_api.get_structured_record import (
Expand All @@ -25,17 +27,21 @@
)
from gateway_api.sds.search_results import SdsSearchResults

# TODO [GPCAPIM-359]: Once stub servers/containers made for PDS, SDS and provider
# we should remove the SDS_URL environment variable and just
# use the stub client
STUB_SDS = os.environ["SDS_URL"].lower() == "stub"
if not STUB_SDS:
from requests import get
else:
from stubs import SdsFhirApiStub

sds = SdsFhirApiStub()
get = sds.get # type: ignore
def get(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm interested in what other people think about this pattern, but I think it's nicer than the conditional imports. We're not planning on moving to separately deployment mocks any time soon, so I think a little wrapper function is useful. Not technically part of the scope of this ticket though, so I can revert and discuss separately if we want to.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Six of one ... for me.

url: str,
headers: dict[str, str],
params: dict[str, str],
timeout: int,
) -> Response:
STUB_SDS = os.environ["SDS_URL"].lower() == "stub"
if not STUB_SDS:
return external_sds_get(url, headers=headers, params=params, timeout=timeout)
else:
return SdsFhirApiStub().get(
url, headers=headers, params=params, timeout=timeout
)


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,12 +89,13 @@ class SdsClient:
def __init__(
self,
base_url: str,
api_key: str,
timeout: int = 10,
service_interaction_id: str | None = None,
) -> None:
self.base_url = base_url.rstrip("/")
self.timeout = timeout
self.api_key = self._get_api_key()
self.api_key = api_key
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This fits better with the env var pattern established in the previous ticket. If we try to read the config from the "app" variable in app.py we get circular imports


if service_interaction_id is not None:
self.service_interaction_id = service_interaction_id
Expand Down Expand Up @@ -169,19 +176,6 @@ def get_org_details(

return SdsSearchResults(asid=asid, endpoint=endpoint_url)

@staticmethod
def _get_api_key() -> str:
"""
Retrieve the API key to use for SDS requests.

This is a placeholder at present because we don't have a real API key.
Ultimately it will probably obtain the key from AWS secrets
"""

# TODO [GPCAPIM-366]: Obtain key from AWS secrets
# DO NOT PUT A REAL KEY HERE, IT WILL BE VISIBLE ON GITHUB
return "test_api_key_DO_NOT_REPLACE_HERE"

def _query_sds(
self,
ods_code: str,
Expand Down
Loading
Loading