From b073009a663974c78a1dfe9f5e220e639dac4fbc Mon Sep 17 00:00:00 2001 From: Mariano Daniel Gobea Alcoba Date: Mon, 20 May 2024 18:25:14 -0300 Subject: [PATCH] :sparkles: Database in progress - It's ok --- .gitignore | 289 ++++++++++++++++++ .idea/.gitignore | 8 + .idea/fuegoQuasarProject.iml | 10 + .idea/inspectionProfiles/Project_Default.xml | 15 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .pre-commit-config.yaml | 4 + LICENSE | 0 README.md | 0 main.py | 127 ++++++++ model/__init__.py | 0 model/data_class.py | 25 ++ model/enum_class.py | 8 + model/request_response.py | 11 + repository/__init__.py | 0 repository/database.py | 10 + requirements.txt | 37 +++ services/__init__.py | 0 services/satellites_services.py | 39 +++ test_main.http | 11 + 22 files changed, 621 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/fuegoQuasarProject.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .pre-commit-config.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.py create mode 100644 model/__init__.py create mode 100644 model/data_class.py create mode 100644 model/enum_class.py create mode 100644 model/request_response.py create mode 100644 repository/__init__.py create mode 100644 repository/database.py create mode 100644 requirements.txt create mode 100644 services/__init__.py create mode 100644 services/satellites_services.py create mode 100644 test_main.http diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4167464 --- /dev/null +++ b/.gitignore @@ -0,0 +1,289 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python,pycharm +# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +# End of https://www.toptal.com/developers/gitignore/api/python,pycharm \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/fuegoQuasarProject.iml b/.idea/fuegoQuasarProject.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/fuegoQuasarProject.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..50772e9 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f02a8b8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a32d5c6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6d2c76d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,4 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: [] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..34abcf2 --- /dev/null +++ b/main.py @@ -0,0 +1,127 @@ +from fastapi import FastAPI, Body, Response, Path + +from fastapi.responses import JSONResponse + +from model.request_response import Request +from model.data_class import Coordinates, Satellite +from services.satellites_services import SatelliteService + +from typing import Annotated, List + +app = FastAPI() + + +@app.get("/") +async def root(): + return {"message": "Hello World"} + + +@app.post("/topsecret", response_model=None) +async def top_secret( + request: Annotated[Request, Body( + ..., + title="List of satellites with the information of the distance and the message that they sent.", + description="List of satellites with the information of the distance and the message that they sent.", + openapi_examples={ + "normal": { + "summary": "Example of a normal request", + "value": { + "satellites": [ + { + "name": "kenobi", + "distance": 0, + "coordinates": { + "x": 15, + "y": 50 + }, + "message": ["Hola", "", "esto", "", "", "mensaje", ""] + }, + { + "name": "skywalker", + "distance": 0, + "coordinates": { + "x": 65, + "y": 90 + }, + "message": ["", "soldado", "esto", "", "", "mensaje", ""] + }, + { + "name": "sato", + "distance": 0, + "coordinates": { + "x": 23, + "y": 180 + }, + "message": ["", "soldado", "", "es", "un", "", ""] + } + ] + } + + } + } + )] +) -> Coordinates | Response: + """ + This endpoint receives a list of satellites with the information of the distance and the message that they sent. + It returns the coordinates of the point where the message was sent. + + :param request: Request model with the list of satellites. + :return: Coordinates model with the coordinates of the point where the message was sent. + """ + try: + satellites: List[Satellite] = request.satellites + for satellite in satellites: + SatelliteService.add_satellite(satellite) + + coordinates: Coordinates = SatelliteService.get_coordinates(satellites) + return coordinates + except Exception as e: + return JSONResponse(status_code=404, content={"message ": str(e)}) + + +@app.put("/topsecret_split/{satellite_name}", response_model=None) +async def top_secret_split( + satellite_name: Annotated[str, Path()], + satellite: Annotated[Satellite, Body( + ..., + title="Satellite with the information of the distance, coordinates and the message that it sent.", + description="Satellite with the information of the distance, coordinates and the message that it sent." + )] +) -> Satellite | Response: + """ + This endpoint receives a satellite with the information of the distance, coordinates and the message that it sent. + It updates the information of the satellite in the database. + + :param satellite: Satellite model with the information of the distance, coordinates and the message that it sent. + :param satellite_name: Name of the satellite. + :return: Satellite model with the information of the distance, coordinates and the message that it sent. + """ + try: + if satellite_name != satellite.name: + return JSONResponse(status_code=400, content={"message ": "The name of the satellite is not the same in " + "the path and in the body."}) + SatelliteService.update_satellite(satellite) + return satellite + except Exception as e: + return JSONResponse(status_code=404, content={"message ": str(e)}) + + +@app.get("/topsecret_split/{satellite_name}", response_model=None) +async def get_satellite( + satellite_name: Annotated[str, Path()], +) -> Satellite | Response: + """ + This endpoint receives a satellite name and returns the information of the satellite. + + :param satellite_name: Name of the satellite. + :return: Satellite model with the information of the distance, coordinates and the message that it sent + """ + try: + satellite: Satellite = SatelliteService.get_satellite(satellite_name) + return satellite + except Exception as e: + return JSONResponse(status_code=404, content={"message ": str(e)}) + + + + diff --git a/model/__init__.py b/model/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/model/data_class.py b/model/data_class.py new file mode 100644 index 0000000..527701a --- /dev/null +++ b/model/data_class.py @@ -0,0 +1,25 @@ +from pydantic import BaseModel + +from .enum_class import SatelliteName + + +class Coordinates(BaseModel): + """Coordinates model.""" + x: float + y: float + + +class Satellite(BaseModel): + name: SatelliteName + distance: float = 0.0 + coordinates: Coordinates + message: list[str] = [] + + def set_distance(self, distance: float) -> None: + self.distance = distance + + def set_coordinates(self, coordinates: Coordinates) -> None: + self.coordinates = coordinates + + def set_message(self, message: list[str]) -> None: + self.message = message diff --git a/model/enum_class.py b/model/enum_class.py new file mode 100644 index 0000000..aa77874 --- /dev/null +++ b/model/enum_class.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class SatelliteName(str, Enum): + """Satellite name enum.""" + KENOBI = "kenobi" + SKYWALKER = "skywalker" + SATO = "sato" diff --git a/model/request_response.py b/model/request_response.py new file mode 100644 index 0000000..dbe6986 --- /dev/null +++ b/model/request_response.py @@ -0,0 +1,11 @@ +from pydantic import BaseModel + +from .data_class import Satellite + + +class Request(BaseModel): + """Request model.""" + satellites: list[Satellite] + + def set_satellites(self, satellites: list[Satellite]) -> None: + self.satellites = satellites diff --git a/repository/__init__.py b/repository/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/repository/database.py b/repository/database.py new file mode 100644 index 0000000..cb15ff8 --- /dev/null +++ b/repository/database.py @@ -0,0 +1,10 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +SQLALCHEMY_DATABASE_URL = "sqlite:///./satellites.db" + +engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..684efd9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,37 @@ +annotated-types==0.6.0 +anyio==4.3.0 +certifi==2024.2.2 +click==8.1.7 +dnspython==2.6.1 +email_validator==2.1.1 +fastapi==0.111.0 +fastapi-cli==0.0.4 +FastAPI-SQLAlchemy==0.2.1 +h11==0.14.0 +httpcore==1.0.5 +httptools==0.6.1 +httpx==0.27.0 +idna==3.7 +Jinja2==3.1.4 +markdown-it-py==3.0.0 +MarkupSafe==2.1.5 +mdurl==0.1.2 +orjson==3.10.3 +pydantic==2.7.1 +pydantic_core==2.18.2 +Pygments==2.18.0 +python-dotenv==1.0.1 +python-multipart==0.0.9 +PyYAML==6.0.1 +rich==13.7.1 +shellingham==1.5.4 +sniffio==1.3.1 +SQLAlchemy==2.0.30 +starlette==0.37.2 +typer==0.12.3 +typing_extensions==4.11.0 +ujson==5.10.0 +uvicorn==0.29.0 +uvloop==0.19.0 +watchfiles==0.21.0 +websockets==12.0 diff --git a/services/__init__.py b/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/services/satellites_services.py b/services/satellites_services.py new file mode 100644 index 0000000..504500c --- /dev/null +++ b/services/satellites_services.py @@ -0,0 +1,39 @@ +from model.data_class import Satellite, Coordinates + + +class SatelliteService(object): + satellites: list[Satellite] = [] + + @classmethod + def add_satellite(cls, satellite: Satellite) -> None: + cls.satellites.append(satellite) + + @classmethod + def update_satellite(cls, satellite: Satellite) -> None: + for i, s in enumerate(cls.satellites): + if s.name == satellite.name: + cls.satellites[i] = satellite + break + + @classmethod + def get_satellite(cls, satellite_name: str) -> Satellite: + for satellite in cls.satellites: + if satellite.name == satellite_name: + return satellite + raise Exception("Satellite not found") + + @classmethod + def get_satellites(cls) -> list[Satellite]: + return cls.satellites + + @classmethod + def get_coordinates(cls, satellites: list[Satellite]) -> Coordinates: + return Coordinates(x=0.0, y=0.0) + + @classmethod + def get_message(cls, satellites: list[Satellite]) -> str: + return "This is a message" + + @classmethod + def get_distance(cls, satellites: list[Satellite]) -> float: + return 100.0 diff --git a/test_main.http b/test_main.http new file mode 100644 index 0000000..a2d81a9 --- /dev/null +++ b/test_main.http @@ -0,0 +1,11 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/ +Accept: application/json + +### + +GET http://127.0.0.1:8000/hello/User +Accept: application/json + +###