Skip to content
Snippets Groups Projects
Commit 62872a42 authored by Christian Boettcher's avatar Christian Boettcher
Browse files

start with settings support, add DELETE requests

parent 0ea21ddd
No related branches found
No related tags found
1 merge request!1apiserver based on fastAPI
from .settings import Settings
\ No newline at end of file
DATACATALOG_APISERVER_HOST="0.0.0.0",
DATACATALOG_APISERVER_PORT=80
DATACATALOG_APISERVER_JSON_STORAGE_PATH="./app/data"
\ No newline at end of file
from pydantic import BaseSettings
DEFAULT_JSON_FILEPATH: str = "./app/data"
## Additional Settings can be made available by adding them as properties to this class
# At launch they will be read from environment variables (case-INsensitive)
class Settings(BaseSettings):
datacatalog_apiserver_host: str = "0.0.0.0"
datacatalog_apiserver_port: int = 80
datacatalog_apiserver_json_storage_path: str = DEFAULT_JSON_FILEPATH
class Config:
env_file = "config.env"
\ No newline at end of file
...@@ -7,18 +7,22 @@ from fastapi import HTTPException ...@@ -7,18 +7,22 @@ from fastapi import HTTPException
from pydantic import BaseModel from pydantic import BaseModel
from .storage import JsonFileStorageAdapter from .storage import JsonFileStorageAdapter
from .storage import AbstractLocationDataStorageAdapter from .storage import AbstractLocationDataStorageAdapter
from .storage import LocationData from .storage import LocationData
from .storage import LocationDataType from .storage import LocationDataType
from .config import Settings
from enum import Enum from enum import Enum
settings = Settings()
app = FastAPI( app = FastAPI(
title="API-Server for the Data Catalogue" title="API-Server for the Data Catalogue"
) )
adapter: AbstractLocationDataStorageAdapter = JsonFileStorageAdapter() adapter: AbstractLocationDataStorageAdapter = JsonFileStorageAdapter(settings)
#### A NOTE ON IDS #### A NOTE ON IDS
# the id of a dataset is not yet defined, it could be simply generated, it could be based on some hash of the metadata or simple be the name, which would then need to be enforced to be unique # the id of a dataset is not yet defined, it could be simply generated, it could be based on some hash of the metadata or simple be the name, which would then need to be enforced to be unique
...@@ -53,7 +57,17 @@ def get_specific_dataset(location_data_type : LocationDataType, dataset_id: str) ...@@ -53,7 +57,17 @@ def get_specific_dataset(location_data_type : LocationDataType, dataset_id: str)
# update the information about a specific dataset, identified by id # update the information about a specific dataset, identified by id
@app.put("/{location_data_type}/{dataset_id}") @app.put("/{location_data_type}/{dataset_id}")
def update_specific_dataset(location_data_type : LocationDataType, dataset_id: str, dataset : LocationData): def update_specific_dataset(location_data_type : LocationDataType, dataset_id: str, dataset : LocationData):
usr: str = "testuser"
try: try:
return adapter.updateDetails(location_data_type, dataset_id, dataset, usr) return adapter.updateDetails(location_data_type, dataset_id, dataset, usr)
except FileNotFoundError: except FileNotFoundError:
raise HTTPException(status_code=404, detail='The provided id does not exist for this datatype.') raise HTTPException(status_code=404, detail='The provided id does not exist for this datatype.')
# delete a specific dataset
@app.delete("/{location_data_type}/{dataset_id}")
def delete_specific_dataset(location_data_type : LocationDataType, dataset_id: str):
usr: str = "testuser"
try:
return adapter.delete(location_data_type, dataset_id, usr)
except FileNotFoundError:
raise HTTPException(status_code=404, detail='The provided id does not exist for this datatype.')
\ No newline at end of file
...@@ -4,9 +4,9 @@ import uuid ...@@ -4,9 +4,9 @@ import uuid
from .LocationStorage import AbstractLocationDataStorageAdapter, LocationData, LocationDataType from .LocationStorage import AbstractLocationDataStorageAdapter, LocationData, LocationDataType
from typing import List from apiserver.config import Settings
DEFAULT_JSON_FILEPATH: str = "./app/data" from typing import List
class StoredData: class StoredData:
actualData: LocationData actualData: LocationData
...@@ -23,9 +23,9 @@ class StoredData: ...@@ -23,9 +23,9 @@ class StoredData:
class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter): class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter):
data_dir: str data_dir: str
def __init__(self, data_directory: str = DEFAULT_JSON_FILEPATH): def __init__(self, settings: Settings):
AbstractLocationDataStorageAdapter.__init__(self) AbstractLocationDataStorageAdapter.__init__(self)
self.data_dir = data_directory self.data_dir = settings.datacatalog_apiserver_json_storage_path
if not (os.path.exists(self.data_dir) and os.path.isdir(self.data_dir)): if not (os.path.exists(self.data_dir) and os.path.isdir(self.data_dir)):
raise Exception('Data Directory \"' + self.data_dir + '\" does not exist.') raise Exception('Data Directory \"' + self.data_dir + '\" does not exist.')
...@@ -77,17 +77,20 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter): ...@@ -77,17 +77,20 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter):
raise FileNotFoundError('The requested Object does not exist.') raise FileNotFoundError('The requested Object does not exist.')
toStore = StoredData() toStore = StoredData()
toStore.actualData = data
# get permissions from old file # get permissions from old file
with open(fullpath) as file: with open(fullpath) as file:
data = json.load(file) old_data = json.load(file)
toStore.users = data['users'] toStore.users = old_data['users']
toStore.actualData = data
with open(fullpath, 'w') as file: with open(fullpath, 'w') as file:
json.dump(toStore.toDict(), file) json.dump(toStore.toDict(), file)
return {id : data} return {id : data}
def delete(self, type:LocationDataType, id:str, usr: str):
pass
def getOwner(self, type: LocationDataType, id: str): def getOwner(self, type: LocationDataType, id: str):
raise NotImplementedError() raise NotImplementedError()
......
...@@ -43,6 +43,9 @@ class AbstractLocationDataStorageAdapter: ...@@ -43,6 +43,9 @@ class AbstractLocationDataStorageAdapter:
def updateDetails(self, type:LocationDataType, id:str, data: LocationData, usr: str): def updateDetails(self, type:LocationDataType, id:str, data: LocationData, usr: str):
raise NotImplementedError() raise NotImplementedError()
def delete(self, type:LocationDataType, id:str, usr: str):
raise NotImplementedError()
# return the owner of the requested object; if multiple owners are set, return them is a list # return the owner of the requested object; if multiple owners are set, return them is a list
def getOwner(self, type: LocationDataType, id: str): def getOwner(self, type: LocationDataType, id: str):
raise NotImplementedError() raise NotImplementedError()
......
...@@ -2,3 +2,4 @@ fastapi==0.63.0 ...@@ -2,3 +2,4 @@ fastapi==0.63.0
pytest==6.2.4 pytest==6.2.4
requests==2.25.1 requests==2.25.1
uvicorn==0.13.4 uvicorn==0.13.4
python-dotenv==0.17.1
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment