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

Request response parameters #19

Merged
merged 40 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d750a54
initial method to accept request paramaters and body
sudeepkunhis Jan 13, 2025
55676f0
formatting changes
sudeepkunhis Jan 13, 2025
26ad0b2
Merge branch 'main' into request-response-parameters
sudeepkunhis Jan 13, 2025
adbe41b
Merge branch 'main' into request-response-parameters
sudeepkunhis Jan 13, 2025
37191a6
solved body error
sudeepkunhis Jan 13, 2025
2802da9
Merge branch 'request-response-parameters' of https://github.com/ONSd…
sudeepkunhis Jan 13, 2025
876a0b9
build docker image
sudeepkunhis Jan 15, 2025
2fc0782
test client fixture
sudeepkunhis Jan 15, 2025
5a68a92
basic post endpoint
sudeepkunhis Jan 15, 2025
f67a535
test post endpoint
sudeepkunhis Jan 15, 2025
7c287e8
logging configuration
sudeepkunhis Jan 21, 2025
aedae1b
formatting changes
sudeepkunhis Jan 21, 2025
abd6c85
return type in logging config
sudeepkunhis Jan 21, 2025
3cd4707
formatting changes
sudeepkunhis Jan 21, 2025
025257e
log level for local docker container
sudeepkunhis Jan 21, 2025
4aa49c7
review changes
sudeepkunhis Jan 22, 2025
cd00591
formatting changes
sudeepkunhis Jan 22, 2025
7df3a3f
removed hello world
sudeepkunhis Jan 22, 2025
347b2af
renamed post method
sudeepkunhis Jan 22, 2025
add83ff
Merge branch 'main' into request-response-parameters
sudeepkunhis Jan 22, 2025
c5b9fe8
docker compose changes
sudeepkunhis Jan 23, 2025
7fabedf
added docstrings
sudeepkunhis Jan 23, 2025
b682734
cleared ignore warnings
sudeepkunhis Jan 23, 2025
9ad7a56
init files
sudeepkunhis Jan 23, 2025
f7fcafb
stable version
sudeepkunhis Jan 23, 2025
a988033
make file changes
sudeepkunhis Jan 24, 2025
885b612
revert make file
sudeepkunhis Jan 24, 2025
b05a970
stable version
sudeepkunhis Jan 24, 2025
79641a5
review changes
sudeepkunhis Jan 27, 2025
926901d
moved tests folder and solved PYTHONPATH problem
sudeepkunhis Jan 28, 2025
1ae9b6c
removed D100 warning
sudeepkunhis Jan 28, 2025
1086eea
renamed working directory
sudeepkunhis Jan 30, 2025
b62824e
Update README.md
sudeepkunhis Jan 31, 2025
812852c
updated logging config
sudeepkunhis Jan 31, 2025
6af672a
unit tests for logging
sudeepkunhis Jan 31, 2025
8cb52fa
unit tests for logging
sudeepkunhis Jan 31, 2025
b9097a9
Update README.md
sudeepkunhis Jan 31, 2025
0b82b90
removed docker-compose and updated make commands
sudeepkunhis Jan 31, 2025
d71e5c1
Merge branch 'request-response-parameters' of https://github.com/ONSd…
sudeepkunhis Jan 31, 2025
567d550
removed app directory
sudeepkunhis Feb 3, 2025
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
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM python:3.12.6
FROM python:3.12.6-slim

liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
WORKDIR /app
WORKDIR /root

liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
COPY pyproject.toml poetry.lock /app/
COPY pyproject.toml poetry.lock /root/

RUN pip install --no-cache-dir poetry==1.8.4 && \
poetry config virtualenvs.create false && \
poetry install --no-root --no-dev

COPY . /app
COPY eq_cir_converter_service eq_cir_converter_service

liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "5010"]
CMD ["uvicorn", "eq_cir_converter_service.app.main:app", "--host", "0.0.0.0", "--port", "5010"]
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ lint: ## Run all linters (black/ruff/pylint/mypy).

.PHONY: test
test: ## Run the tests and check coverage.
poetry run pytest -n auto --cov=src --cov-report term-missing --cov-fail-under=100
poetry run pytest -n auto --cov=eq_cir_converter_service --cov-report term-missing --cov-fail-under=100

.PHONY: mypy
mypy: ## Run mypy.
poetry run mypy src
poetry run mypy -p eq_cir_converter_service

.PHONY: install
install: ## Install the dependencies excluding dev.
Expand All @@ -51,16 +51,16 @@ megalint: ## Run the mega-linter.

.PHONY: run
run: ## Start the local application.
poetry run uvicorn src.main:app --reload --port 5010
poetry run uvicorn eq_cir_converter_service.app.main:app --reload --port 5010

.PHONY: docker-build
docker-build: ## Build the docker image.
docker build -t cir-converter-service .

.PHONY: docker-compose-up
docker-compose-up: ## Start the docker container using docker-compose.
docker-compose up -d
.PHONY: docker-run
docker-run: ## Run the docker container using the built image.
docker run -d -p 5010:5010 --name eq-cir-converter-service cir-converter-service

.PHONY: docker-compose-down
docker-compose-down: ## Stop the docker container using docker-compose.
docker-compose down
.PHONY: docker-stop-remove
docker-stop-remove: ## Stop and remove the docker container.
docker stop eq-cir-converter-service && docker rm eq-cir-converter-service
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ Ensure you have the following installed:
make install
```

3. Run the application
3. Set the LOG_LEVEL to INFO, DEBUG, WARN, ERROR (If the log level is not set, it will be set to INFO by default.)

```bash
export LOG_LEVEL=INFO
```

4. Run the application

```bash
make run
Expand Down
7 changes: 0 additions & 7 deletions docker-compose.yml

This file was deleted.

File renamed without changes.
Empty file.
16 changes: 16 additions & 0 deletions eq_cir_converter_service/app/config/logging_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Configure the logging level for the application."""
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved

import logging
import os


def get_log_level() -> int:
"""Get the logging level from the LOG_LEVEL environment variable, or use the default value of INFO."""
log_level = os.environ.get("LOG_LEVEL", "INFO")
return int(getattr(logging, log_level, logging.INFO))


logging.basicConfig(
level=get_log_level(),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
9 changes: 9 additions & 0 deletions eq_cir_converter_service/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""This module is the entry point of the FastAPI application."""

import fastapi

from eq_cir_converter_service.app.routers import schema_router

app = fastapi.FastAPI()

app.include_router(schema_router.router)
Empty file.
42 changes: 42 additions & 0 deletions eq_cir_converter_service/app/routers/schema_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""This module contains the FastAPI router for the schema conversion endpoint."""

from fastapi import APIRouter

from eq_cir_converter_service.app.config.logging_config import logging

router = APIRouter()

logger = logging.getLogger(__name__)

"""The POST endpoint to convert the CIR schema from one version to another."""


@router.post(
"/schema",
response_model=dict,
)
async def convert_schema(
current_version: str,
target_version: str,
schema: dict,
) -> dict:
"""Convert the CIR schema from one version to another.

Request query parameters:
- current_version: The current version of the schema.
- target_version: The target version of the schema.

Request body:
- schema: The schema to convert.

Returns:
- dict: The converted schema.
"""
logger.info("Posting the cir schema...")

logger.debug("Received current version %s and target version %s", current_version, target_version)
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Input body: %s", schema)

# TO DO: Implement the logic to convert the schema from one version to another

return schema
9 changes: 2 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,10 @@ select = [
]

ignore = [
# Conflicts with google docstring style
"D205",
# Allow missing docstring, remove to enforce docstrings across the board
"D100", "D101", "D102", "D103", "D104", "D105", "D106", "D107",
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
# Disable line length check as it is handled by black
# :TODO: Remove E501 when ruff supports all black rules
"D104",
# Remove E501 when ruff supports all black rules
"E501",
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
# indentation contains tabs
"W191",
]

[tool.ruff.lint.pydocstyle]
Expand Down
9 changes: 0 additions & 9 deletions src/main.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/unit/config/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Unit tests for logging configuration."""

import logging
import os

from eq_cir_converter_service.app.config import logging_config


def test_get_log_level():
"""Test that the logging level is set to INFO by default."""
os.environ["LOG_LEVEL"] = ""
log_level = logging_config.get_log_level()
assert log_level == logging.INFO


def test_get_log_level_custom():
"""Test that the logging level is set to DEBUG when the LOG_LEVEL environment variable is set to DEBUG."""
os.environ["LOG_LEVEL"] = "DEBUG"
log_level = logging_config.get_log_level()
assert log_level == logging.DEBUG
18 changes: 12 additions & 6 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# import pytest
"""Configuration for unit tests."""

from collections.abc import Generator

# @pytest.fixture()
# def calculator():
# # Create a new instance of the Calculator class for each test session.
# yield Calculator()
# # Clean up after the test session is complete.
import pytest
from fastapi.testclient import TestClient

import eq_cir_converter_service.app.main as app


@pytest.fixture
def test_client() -> Generator[TestClient, None, None]:
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved
"""General client for hitting endpoints in tests."""
yield TestClient(app.app)
16 changes: 16 additions & 0 deletions tests/unit/routers/test_schema_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""This module contains the unit tests for the schema router."""

from fastapi import status
from fastapi.testclient import TestClient

"""Test the schema router with valid JSON."""
liamtoozer marked this conversation as resolved.
Show resolved Hide resolved


def test_schema_router_with_valid_json(test_client: TestClient) -> None:
"""Test the post schema method with valid JSON."""
response = test_client.post(
"/schema?current_version=1&target_version=2",
json={"valid_json": "valid_json"},
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["valid_json"] == "valid_json"
12 changes: 0 additions & 12 deletions tests/unit/test_main.py

This file was deleted.

Loading