Skip to content

Commit

Permalink
Adds documentation and details for api
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Jan 20, 2024
1 parent 7ce07d2 commit 49f201d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 11 deletions.
75 changes: 64 additions & 11 deletions API/hdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@
async def create_hdx(
request: Request, hdx_data: dict, user_data: AuthUser = Depends(staff_required)
):
hdx_instance = HDX()
"""
Create a new HDX entry.
Args:
request (Request): The request object.
hdx_data (dict): Data for creating the HDX entry.
user_data (AuthUser): User authentication data.
Returns:
dict: Result of the HDX creation process.
"""
hdx_instance = HDX()
return hdx_instance.create_hdx(hdx_data)


Expand All @@ -34,32 +44,48 @@ async def read_hdx_list(
skip: int = 0,
limit: int = 10,
):
"""
Retrieve a list of HDX entries based on provided filters.
Args:
request (Request): The request object.
skip (int): Number of entries to skip.
limit (int): Maximum number of entries to retrieve.
Returns:
List[dict]: List of HDX entries.
"""
hdx_instance = HDX()
filters = {}
for key, values in request.query_params.items():
if key not in ["skip", "limit"]:
if key in ["iso_3", "queue", "meta", "hdx_upload", "cid"]:
# if isinstance(values, list):
# filters[f"{key} IN %s"] = tuple(values)
# continue
filters[f"{key} = %s"] = values
continue
# if isinstance(values, list):
# filters[f"dataset->>'{key}' IN %s"] = tuple(values)
# else:
filters[f"dataset->>'{key}' = %s"] = values

hdx_list = hdx_instance.get_hdx_list_with_filters(skip, limit, filters)

return hdx_list


@router.get("/{hdx_id}", response_model=dict)
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
async def read_hdx(request: Request, hdx_id: int):
hdx_instance = HDX()
"""
Retrieve a specific HDX entry by its ID.
Args:
request (Request): The request object.
hdx_id (int): ID of the HDX entry to retrieve.
Returns:
dict: Details of the requested HDX entry.
Raises:
HTTPException: If the HDX entry is not found.
"""
hdx_instance = HDX()
hdx = hdx_instance.get_hdx_by_id(hdx_id)
if hdx:
return hdx
Expand All @@ -75,8 +101,22 @@ async def update_hdx(
hdx_data: dict,
user_data: AuthUser = Depends(staff_required),
):
hdx_instance = HDX()
"""
Update an existing HDX entry.
Args:
request (Request): The request object.
hdx_id (int): ID of the HDX entry to update.
hdx_data (dict): Data for updating the HDX entry.
user_data (AuthUser): User authentication data.
Returns:
dict: Result of the HDX update process.
Raises:
HTTPException: If the HDX entry is not found.
"""
hdx_instance = HDX()
existing_hdx = hdx_instance.get_hdx_by_id(hdx_id)
if not existing_hdx:
raise HTTPException(status_code=404, detail="HDX not found")
Expand All @@ -90,8 +130,21 @@ async def update_hdx(
async def delete_hdx(
request: Request, hdx_id: int, user_data: AuthUser = Depends(admin_required)
):
hdx_instance = HDX()
"""
Delete an existing HDX entry.
Args:
request (Request): The request object.
hdx_id (int): ID of the HDX entry to delete.
user_data (AuthUser): User authentication data.
Returns:
dict: Result of the HDX deletion process.
Raises:
HTTPException: If the HDX entry is not found.
"""
hdx_instance = HDX()
existing_hdx = hdx_instance.get_hdx_by_id(hdx_id)
if not existing_hdx:
raise HTTPException(status_code=404, detail="HDX not found")
Expand Down
57 changes: 57 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,15 @@ def __init__(self) -> None:
self.con, self.cur = self.d_b.connect()

def create_hdx(self, hdx_data):
"""
Create a new HDX entry in the database.
Args:
hdx_data (dict): Data for creating the HDX entry.
Returns:
dict: Result of the HDX creation process.
"""
insert_query = sql.SQL(
"""
INSERT INTO public.hdx (iso_3, hdx_upload, dataset, queue, meta, categories, geometry)
Expand All @@ -1930,6 +1939,17 @@ def create_hdx(self, hdx_data):
def get_hdx_list_with_filters(
self, skip: int = 0, limit: int = 10, filters: dict = {}
):
"""
Retrieve a list of HDX entries based on provided filters.
Args:
skip (int): Number of entries to skip.
limit (int): Maximum number of entries to retrieve.
filters (dict): Filtering criteria.
Returns:
List[dict]: List of HDX entries.
"""
filter_conditions = []
filter_values = []

Expand All @@ -1952,6 +1972,18 @@ def get_hdx_list_with_filters(
return [orjson.loads(item[0]) for item in result]

def get_hdx_by_id(self, hdx_id: int):
"""
Retrieve a specific HDX entry by its ID.
Args:
hdx_id (int): ID of the HDX entry to retrieve.
Returns:
dict: Details of the requested HDX entry.
Raises:
HTTPException: If the HDX entry is not found.
"""
select_query = sql.SQL(
"""
SELECT ST_AsGeoJSON(c.*) FROM public.hdx
Expand All @@ -1966,6 +1998,19 @@ def get_hdx_by_id(self, hdx_id: int):
raise HTTPException(status_code=404, detail="Item not found")

def update_hdx(self, hdx_id: int, hdx_data):
"""
Update an existing HDX entry in the database.
Args:
hdx_id (int): ID of the HDX entry to update.
hdx_data (dict): Data for updating the HDX entry.
Returns:
dict: Result of the HDX update process.
Raises:
HTTPException: If the HDX entry is not found.
"""
update_query = sql.SQL(
"""
UPDATE public.hdx
Expand Down Expand Up @@ -1994,6 +2039,18 @@ def update_hdx(self, hdx_id: int, hdx_data):
raise HTTPException(status_code=404, detail="Item not found")

def delete_hdx(self, hdx_id: int):
"""
Delete an existing HDX entry from the database.
Args:
hdx_id (int): ID of the HDX entry to delete.
Returns:
dict: Result of the HDX deletion process.
Raises:
HTTPException: If the HDX entry is not found.
"""
delete_query = sql.SQL(
"""
DELETE FROM public.hdx
Expand Down

0 comments on commit 49f201d

Please sign in to comment.