This repository was archived by the owner on Aug 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Fastapi #68
Merged
Merged
Fastapi #68
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e7c339
fastapi: update adapter return types
DanielElisenberg a27a870
fastapi: update dependencies
DanielElisenberg 912c09f
fastapi: update config logging
DanielElisenberg dfa4699
fastapi: update api layer
DanielElisenberg 6390ee5
fastapi: update tests
DanielElisenberg 709787a
fastapi: update dockerfile and logging config
DanielElisenberg fd6f585
fastapi: lint
DanielElisenberg ae2745c
fastapi: lint
DanielElisenberg 0497f15
fastapi: lint
DanielElisenberg 02fc6fe
fastapi: fix unintentional removal
DanielElisenberg 64c71c2
fastapi: old style error responses
DanielElisenberg 9fa9d4e
fastapi: don't messagepack responses
DanielElisenberg cd4e632
fastapi: remove messagepack dependency
DanielElisenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,100 +1,80 @@ | ||
| import logging | ||
|
|
||
| from flask import Blueprint, jsonify, request | ||
| from fastapi import APIRouter, Depends | ||
|
|
||
| from metadata_service.api.request_models import NameParam, MetadataQuery | ||
| from metadata_service.api.response_models import metadata_response | ||
| from metadata_service.domain import metadata | ||
| from metadata_service.domain.version import get_version_from_string | ||
|
|
||
| logger = logging.getLogger() | ||
| metadata_api = Blueprint("metadata_api", __name__) | ||
| metadata_router = APIRouter() | ||
|
|
||
|
|
||
| @metadata_api.get("/metadata/data-store") | ||
| def get_data_store(): | ||
| @metadata_router.get("/metadata/data-store") | ||
| def get_data_store(response=Depends(metadata_response)): | ||
| logger.info("GET /metadata/data-store") | ||
| return response(metadata.find_all_datastore_versions()) | ||
|
|
||
| response = jsonify(metadata.find_all_datastore_versions()) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
|
|
||
|
|
||
| @metadata_api.get("/metadata/data-structures/status") | ||
| def get_data_structure_current_status(): | ||
| validated_query = NameParam(**request.args) | ||
| @metadata_router.get("/metadata/data-structures/status") | ||
| def get_data_structure_current_status(validated_query: NameParam = Depends()): | ||
| logger.info( | ||
| f"GET /metadata/data-structures/status with name = {validated_query.names}" | ||
| ) | ||
| response = jsonify( | ||
| metadata.find_current_data_structure_status( | ||
| validated_query.get_names_as_list() | ||
| ) | ||
| return metadata.find_current_data_structure_status( | ||
| validated_query.get_names_as_list() | ||
| ) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
|
|
||
|
|
||
| @metadata_api.post("/metadata/data-structures/status") | ||
| def get_data_structure_current_status_as_post(): | ||
| validated_body = NameParam(**request.json) | ||
|
|
||
| @metadata_router.post("/metadata/data-structures/status") | ||
| def get_data_structure_current_status_as_post(validated_body: NameParam): | ||
| logger.info( | ||
| f"POST /metadata/data-structures/status with name = {validated_body.names}" | ||
| ) | ||
| response = jsonify( | ||
| metadata.find_current_data_structure_status( | ||
| validated_body.get_names_as_list() | ||
| ) | ||
| return metadata.find_current_data_structure_status( | ||
| validated_body.get_names_as_list() | ||
| ) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
|
|
||
|
|
||
| @metadata_api.get("/metadata/data-structures") | ||
| def get_data_structures(): | ||
| validated_query = MetadataQuery(**request.args) | ||
| @metadata_router.get("/metadata/data-structures") | ||
| def get_data_structures( | ||
| validated_query: MetadataQuery = Depends(), | ||
| response=Depends(metadata_response), | ||
| ): | ||
| validated_query.include_attributes = True | ||
| logger.info(f"GET /metadata/data-structures with query: {validated_query}") | ||
|
|
||
| response = jsonify( | ||
| return response( | ||
| metadata.find_data_structures( | ||
| validated_query.names, | ||
| validated_query.names_as_list(), | ||
| get_version_from_string(validated_query.version), | ||
| validated_query.include_attributes, | ||
| validated_query.skip_code_lists, | ||
| ) | ||
| ) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
|
|
||
|
|
||
| @metadata_api.get("/metadata/all-data-structures") | ||
| @metadata_router.get("/metadata/all-data-structures") | ||
| def get_all_data_structures_ever(): | ||
| logger.info("GET /metadata/all-data-structures") | ||
|
|
||
| response = jsonify(metadata.find_all_data_structures_ever()) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
| return metadata.find_all_data_structures_ever() | ||
|
|
||
|
|
||
| @metadata_api.get("/metadata/all") | ||
| def get_all_metadata(): | ||
| validated_query = MetadataQuery(**request.args) | ||
| @metadata_router.get("/metadata/all") | ||
| def get_all_metadata( | ||
| validated_query: MetadataQuery = Depends(), | ||
| respond=Depends(metadata_response), | ||
| ): | ||
| logger.info(f"GET /metadata/all with version: {validated_query.version}") | ||
|
|
||
| response = jsonify( | ||
| return respond( | ||
| metadata.find_all_metadata( | ||
| get_version_from_string(validated_query.version), | ||
| validated_query.skip_code_lists, | ||
| ) | ||
| ) | ||
| response.headers.set("content-language", "no") | ||
| return response | ||
|
|
||
|
|
||
| @metadata_api.get("/languages") | ||
| @metadata_router.get("/languages") | ||
| def get_languages(): | ||
| logger.info("GET /languages") | ||
|
|
||
| response = jsonify(metadata.find_languages()) | ||
| return response | ||
| return metadata.find_languages() |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| from flask import Blueprint | ||
| from fastapi import APIRouter | ||
|
|
||
|
|
||
| observability = Blueprint("observability", __name__) | ||
| observability_router = APIRouter() | ||
|
|
||
|
|
||
| @observability.get("/health/alive") | ||
| def alive(): | ||
| @observability_router.get("/health/alive") | ||
| async def alive(): | ||
| return "I'm alive!" | ||
|
|
||
|
|
||
| @observability.get("/health/ready") | ||
| def ready(): | ||
| @observability_router.get("/health/ready") | ||
| async def ready(): | ||
| return "I'm ready!" |
This file contains hidden or 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 hidden or 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,30 @@ | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
| from fastapi import Request | ||
| from fastapi.responses import Response, JSONResponse | ||
| import msgpack | ||
|
|
||
|
|
||
| class MsgPackResponse(Response): | ||
DanielElisenberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| media_type = "application/x-msgpack" | ||
|
|
||
| def render(self, content) -> bytes: | ||
| compressed_response = msgpack.packb(content, use_bin_type=True) | ||
| return compressed_response or b"" | ||
|
|
||
|
|
||
| def metadata_response( | ||
| request: Request, | ||
| ) -> Callable[[Any], JSONResponse | MsgPackResponse]: | ||
| """ | ||
| Returns a response function that compresses the response | ||
| if the accept header in the request is applicaion/x-msgpack | ||
| """ | ||
|
|
||
| def respond(content): | ||
| accept = request.headers.get("accept", "") | ||
| if "application/x-msgpack" in accept: | ||
| return MsgPackResponse(content) | ||
| return JSONResponse(content) | ||
|
|
||
| return respond | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.