From f30b5b5928290ff71d49bebb8358ed9f2bfd9e45 Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Tue, 3 Dec 2024 12:43:45 +0100 Subject: [PATCH 1/6] api implementation --- api/server.py | 88 ++++++++++++++++++++++++++++++++ requirements.txt | 2 + sdm_versions_manager/config.json | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 api/server.py diff --git a/api/server.py b/api/server.py new file mode 100644 index 0000000..2c43c2a --- /dev/null +++ b/api/server.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +## +# Copyright 2024 FIWARE Foundation, e.V. +# +# This file is part of SDM Quality Testing +# +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + + +from fastapi import FastAPI, HTTPException, Request, status +from dotenv import dotenv_values +from pymongo import MongoClient +from contextlib import asynccontextmanager + + +config = dotenv_values(dotenv_path="../.env") + + +# Define a lifespan context manager +@asynccontextmanager +async def lifespan(app): + # Startup logic + app.mongodb_client = MongoClient(config["MONGO_URI"]) + app.database = app.mongodb_client[config["DB_NAME"]] + print("Connected to the MongoDB database!") + + # Yield control back to the application + yield + + # Shutdown logic + app.mongodb_client.close() + print("Disconnected from the MongoDB database!") + + +app = FastAPI(lifespan=lifespan, title="SDM versions manager") + +@app.get("/") +async def root(): + return {"message: This is the SDM version manager api."} + + +@app.get("/data-model/{name}/versions", response_description="Get all the versions of a data model", status_code=status.HTTP_200_OK) +def list_datamodel_versions(name: str): + collection = app.database["versions"] + + # Querying the database for all documents of the specified data model name + data_model_list = list(collection.find({"dataModel": name})) + if not data_model_list: + raise HTTPException(status_code=404, detail="Data model not found in the database") + + # Extracting only the version numbers from the documents + versions = [model["version"] for model in data_model_list] + + return versions + + +@app.get("/datamodel/{name}/version/{version}", response_description="Get the schema URL of a data model at a particular version", status_code=status.HTTP_200_OK) +def get_schema(name: str, version: str): + collection = app.database["versions"] + + # Querying the database for the specific data model and version + result = collection.find_one({"dataModel": name, "version": version}) + + if result is None: + raise HTTPException(status_code=404, detail="Data model version not found in the database") + + # Prepare the response dictionary with version and schemaUrl + response_data = { + "version": result["version"], + "schemaUrl": result["SchemaUrl"], + } + + return response_data + diff --git a/requirements.txt b/requirements.txt index 2de900b..8076c05 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ requests==2.32.3 pymongo==4.8.0 python-dotenv==1.0.1 +fastapi==0.115.5 +uvicorn==0.32.1 \ No newline at end of file diff --git a/sdm_versions_manager/config.json b/sdm_versions_manager/config.json index 9651cad..8f75044 100644 --- a/sdm_versions_manager/config.json +++ b/sdm_versions_manager/config.json @@ -16,7 +16,7 @@ ["WaterQuality", "WaterQualityObserved"], ["WaterQuality", "WaterQualityPredicted"], ["WaterConsumption", "WaterConsumptionObserved"], - ["SatelliteImagery", "EOSatelliteImagery "], + ["SatelliteImagery", "EOSatelliteImagery"], ["CallComplaints", "Complaint"] ] } From 547bf3361b60a161db5739bba8b69e305c8ec86d Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Tue, 3 Dec 2024 15:23:25 +0100 Subject: [PATCH 2/6] API documentation --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++++- main.py | 1 - 2 files changed, 46 insertions(+), 2 deletions(-) delete mode 100644 main.py diff --git a/README.md b/README.md index 24a87de..917afb8 100644 --- a/README.md +++ b/README.md @@ -161,4 +161,49 @@ python version_manager.py - Provides clear logging of version changes and database updates. **Note**: -The version_manager.py script is designed to work in conjunction with the initial database population performed by initial_population.py. Ensure that you have run the initial population script before using the version manager. \ No newline at end of file +The version_manager.py script is designed to work in conjunction with the initial database population performed by initial_population.py. Ensure that you have run the initial population script before using the version manager. + +## APIs documentation + +A server has been implemented based on FastAPI to allow users to interact with the database with "read only access". the API allows users to retrieve versions of a specified data model and obtain the schema URL for a particular version. + +To start the server, run the collowing command: + +```bash +cd api +python -m uvicorn server:app --reload +``` + +### API endpoints + +**1. List Smart Data Model Version** + +This endpoint retrieves all the versions associated with a specified data model. + +```` +GET /data-model/{name}/versions +```` + +**Parameters** + +- name (path parameter): The name of the data model for which you want to retrieve the versions. + +**Responses** +- 200 OK: Returns a list of version numbers for the specified data model. +- 404 Not Found: If no data model with the specified name exists in the database. + + +**2. Get Schema URL by Version** + +This endpoint retrieves the schema URL for a specific version of a given data model. +```` +GET /datamodel/{name}/version/{version} +```` + +**Parameters** +- name (path parameter): The name of the data model. +- version (path parameter): The version number of the data model. + +**Responses** +- 200 OK: Returns a JSON object containing the version number and its corresponding schema URL. +- 404 Not Found: If the specified data model version does not exist in the database. \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index 7846877..0000000 --- a/main.py +++ /dev/null @@ -1 +0,0 @@ -# This module can serve as a command-line interface (CLI) for users of the WDME to interact with the functionality of the package. From dd5a879ac21a0d742dc636e5d16c19f4590bcae1 Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Wed, 4 Dec 2024 17:05:07 +0100 Subject: [PATCH 3/6] minor fixes --- README.md | 2 +- api/server.py | 4 ++-- sdm_versions_manager/version_manager.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 917afb8..83d466e 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ python -m uvicorn server:app --reload This endpoint retrieves all the versions associated with a specified data model. ```` -GET /data-model/{name}/versions +GET /datamodel/{name}/versions ```` **Parameters** diff --git a/api/server.py b/api/server.py index 2c43c2a..65f1126 100644 --- a/api/server.py +++ b/api/server.py @@ -53,7 +53,7 @@ async def root(): return {"message: This is the SDM version manager api."} -@app.get("/data-model/{name}/versions", response_description="Get all the versions of a data model", status_code=status.HTTP_200_OK) +@app.get("/datamodel/{name}/versions", response_description="Get all the versions of a data model", status_code=status.HTTP_200_OK) def list_datamodel_versions(name: str): collection = app.database["versions"] @@ -81,7 +81,7 @@ def get_schema(name: str, version: str): # Prepare the response dictionary with version and schemaUrl response_data = { "version": result["version"], - "schemaUrl": result["SchemaUrl"], + "schemaUrl": result["schemaUrl"], } return response_data diff --git a/sdm_versions_manager/version_manager.py b/sdm_versions_manager/version_manager.py index ae11ec3..8c8ce23 100644 --- a/sdm_versions_manager/version_manager.py +++ b/sdm_versions_manager/version_manager.py @@ -113,7 +113,7 @@ def fetch_latest_versions(data_model_list): "subject": subject, "dataModel": data_model, "version": current_version, - "SchemaUrl": f"https://github.com/smart-data-models/dataModel.{subject}/blob/{commit_hash}/{data_model}/schema.json", + "schemaUrl": f"https://github.com/smart-data-models/dataModel.{subject}/blob/{commit_hash}/{data_model}/schema.json", "commitHash": commit_hash, "commitDate": commit_date }) From e8791893ab90e1aa1826c7439ac77dd186dac0e4 Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Fri, 6 Dec 2024 15:42:51 +0100 Subject: [PATCH 4/6] minor updates --- api/server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/server.py b/api/server.py index 65f1126..821569b 100644 --- a/api/server.py +++ b/api/server.py @@ -21,7 +21,7 @@ ## -from fastapi import FastAPI, HTTPException, Request, status +from fastapi import FastAPI, HTTPException, status from dotenv import dotenv_values from pymongo import MongoClient from contextlib import asynccontextmanager @@ -48,7 +48,7 @@ async def lifespan(app): app = FastAPI(lifespan=lifespan, title="SDM versions manager") -@app.get("/") +@app.get("/info") async def root(): return {"message: This is the SDM version manager api."} @@ -68,7 +68,7 @@ def list_datamodel_versions(name: str): return versions -@app.get("/datamodel/{name}/version/{version}", response_description="Get the schema URL of a data model at a particular version", status_code=status.HTTP_200_OK) +@app.get("/datamodel/{name}/versions/{version}", response_description="Get the schema URL of a data model at a particular version", status_code=status.HTTP_200_OK) def get_schema(name: str, version: str): collection = app.database["versions"] From 1b9d7beb483a8282046a4656e94a72bc05de9330 Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Fri, 6 Dec 2024 15:43:00 +0100 Subject: [PATCH 5/6] minor fix --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8076c05..bea7cdd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ requests==2.32.3 pymongo==4.8.0 python-dotenv==1.0.1 fastapi==0.115.5 -uvicorn==0.32.1 \ No newline at end of file +uvicorn==0.32.1 From 152d95370226a81091d573222a3815774af13493 Mon Sep 17 00:00:00 2001 From: "rihab.feki" Date: Fri, 6 Dec 2024 15:43:26 +0100 Subject: [PATCH 6/6] added examples api request and responses --- README.md | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 83d466e..a82a378 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ The version_manager.py script is designed to work in conjunction with the initia A server has been implemented based on FastAPI to allow users to interact with the database with "read only access". the API allows users to retrieve versions of a specified data model and obtain the schema URL for a particular version. -To start the server, run the collowing command: +To start the server, run the following command: ```bash cd api @@ -176,7 +176,7 @@ python -m uvicorn server:app --reload ### API endpoints -**1. List Smart Data Model Version** +**1. List Smart Data Model Versions** This endpoint retrieves all the versions associated with a specified data model. @@ -186,24 +186,59 @@ GET /datamodel/{name}/versions **Parameters** -- name (path parameter): The name of the data model for which you want to retrieve the versions. +- name (path parameter): The name of the data model (Entity Type) for which you want to retrieve the versions (e.g., WaterObserved). **Responses** + - 200 OK: Returns a list of version numbers for the specified data model. - 404 Not Found: If no data model with the specified name exists in the database. +**Example request URL** + +```shell +http://127.0.0.1:8000/datamodel/WaterObserved/versions' +``` + +**Example response** + +````shell +[ + "0.0.4", + "0.0.3", + "0.0.2", + "0.0.1" +] +```` + **2. Get Schema URL by Version** This endpoint retrieves the schema URL for a specific version of a given data model. ```` -GET /datamodel/{name}/version/{version} +GET /datamodel/{name}/versions/{version} ```` **Parameters** -- name (path parameter): The name of the data model. -- version (path parameter): The version number of the data model. + +- name (path parameter): The name of the data model (Entity Type) for which you want to retrieve the versions (e.g., WeatherForecast). +- version (path parameter): The version number of the data model (e.g., 0.3.4). **Responses** + - 200 OK: Returns a JSON object containing the version number and its corresponding schema URL. -- 404 Not Found: If the specified data model version does not exist in the database. \ No newline at end of file +- 404 Not Found: If the specified data model version does not exist in the database. + +**Example request URL** + +``` +http://127.0.0.1:8000/datamodel/WaterObserved/versions/0.0.4 +``` + +**Example response** + +```shell +{ + "version": "0.0.4", + "schemaUrl": "https://raw.githubusercontent.com/smart-data-models/dataModel.Environment/6735c895688276b924b64ab27c18547222c05219/WaterObserved/schema.json" +} +``` \ No newline at end of file