Skip to content

Commit

Permalink
DH-4724/updating the endpoints to be RESTfull
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadrezaPourreza committed Sep 27, 2023
1 parent a7b86e7 commit c830f97
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 69 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,47 +319,48 @@ curl -X 'PATCH' \

#### adding database level instructions

You can add database level instructions to the context store manually from the `POST /api/v1/{db_connection_id}/instructions` endpoint
You can add database level instructions to the context store manually from the `POST /api/v1/instructions` endpoint
These instructions are passed directly to the engine and can be used to steer the engine to generate SQL that is more in line with your business logic.

```
curl -X 'POST' \
'<host>/api/v1/{db_connection_id}/instructions' \
'<host>/api/v1/instructions' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"instruction": "This is a database level instruction"
"db_connection_id": "db_connection_id"
}'
```

#### getting database level instructions

You can get database level instructions from the `GET /api/v1/{db_connection_id}/instructions` endpoint
You can get database level instructions from the `GET /api/v1/instructions` endpoint

```
curl -X 'GET' \
'<host>/api/v1/{db_connection_id}/instructions?page=1&limit=10' \
'<host>/api/v1/instructions?page=1&limit=10' \
-H 'accept: application/json'
```

#### deleting database level instructions

You can delete database level instructions from the `DELETE /api/v1/{db_connection_id}/instructions/{instruction_id}` endpoint
You can delete database level instructions from the `DELETE /api/v1/instructions/{instruction_id}` endpoint

```
curl -X 'DELETE' \
'<host>/api/v1/{db_connection_id}/instructions/{instruction_id}' \
'<host>/api/v1/instructions/{instruction_id}' \
-H 'accept: application/json'
```

#### updating database level instructions

You can update database level instructions from the `PATCH /api/v1/{db_connection_id}/instructions/{instruction_id}` endpoint
You can update database level instructions from the `PUT /api/v1/instructions/{instruction_id}` endpoint
Try different instructions to see how the engine generates SQL

```
curl -X 'PATCH' \
'<host>/api/v1/{db_connection_id}/instructions/{instruction_id}' \
curl -X 'PUT' \
'<host>/api/v1/instructions/{instruction_id}' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
Expand Down
10 changes: 5 additions & 5 deletions dataherald/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
QuestionRequest,
ScannerRequest,
TableDescriptionRequest,
UpdateInstruction,
UpdateQueryRequest,
)

Expand Down Expand Up @@ -102,25 +103,24 @@ def get_golden_records(self, page: int = 1, limit: int = 10) -> List[GoldenRecor

@abstractmethod
def add_instruction(
self, db_connection_id: str, instruction_request: InstructionRequest
self, instruction_request: InstructionRequest
) -> Instruction:
pass

@abstractmethod
def get_instructions(
self, db_connection_id: str, page: int = 1, limit: int = 10
self, db_connection_id: str = None, page: int = 1, limit: int = 10
) -> List[Instruction]:
pass

@abstractmethod
def delete_instruction(self, db_connection_id: str, instruction_id: str) -> dict:
def delete_instruction(self, instruction_id: str) -> dict:
pass

@abstractmethod
def update_instruction(
self,
db_connection_id: str,
instruction_id: str,
instruction_request: InstructionRequest,
instruction_request: UpdateInstruction,
) -> Instruction:
pass
40 changes: 22 additions & 18 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
QuestionRequest,
ScannerRequest,
TableDescriptionRequest,
UpdateInstruction,
UpdateQueryRequest,
)

Expand Down Expand Up @@ -318,47 +319,50 @@ def get_golden_records(self, page: int = 1, limit: int = 10) -> List[GoldenRecor

@override
def add_instruction(
self, db_connection_id: str, instruction_request: InstructionRequest
self, instruction_request: InstructionRequest
) -> Instruction:
instruction_repository = InstructionRepository(self.storage)
instruction = Instruction(
instruction=instruction_request.instruction,
db_connection_id=db_connection_id,
db_connection_id=instruction_request.db_connection_id,
)
return instruction_repository.insert(instruction)

@override
def get_instructions(
self, db_connection_id: str, page: int = 1, limit: int = 10
self, db_connection_id: str = None, page: int = 1, limit: int = 10
) -> List[Instruction]:
instruction_repository = InstructionRepository(self.storage)
return instruction_repository.find_by(
{"db_connection_id": db_connection_id},
page=page,
limit=limit,
)
if db_connection_id:
return instruction_repository.find_by(
{"db_connection_id": db_connection_id},
page=page,
limit=limit,
)
return instruction_repository.find_all()

@override
def delete_instruction(self, db_connection_id: str, instruction_id: str) -> dict:
def delete_instruction(self,instruction_id: str) -> dict:
instruction_repository = InstructionRepository(self.storage)
instruction = instruction_repository.find_by_id(instruction_id)
if instruction.db_connection_id != db_connection_id:
deleted = instruction_repository.delete_by_id(instruction_id)
if deleted == 0:
raise HTTPException(status_code=404, detail="Instruction not found")
instruction_repository.delete_by_id(instruction_id)
return {"status": "success"}

@override
def update_instruction(
self,
db_connection_id: str,
instruction_id: str,
instruction_request: InstructionRequest,
instruction_request: UpdateInstruction,
) -> Instruction:
instruction_repository = InstructionRepository(self.storage)
instruction = Instruction(
instruction = instruction_repository.find_by_id(instruction_id)
if not instruction:
raise HTTPException(status_code=404, detail="Instruction not found")
updated_instruction = Instruction(
id=instruction_id,
instruction=instruction_request.instruction,
db_connection_id=db_connection_id,
db_connection_id=instruction.db_connection_id,
)
instruction_repository.update(instruction)
return json.loads(json_util.dumps(instruction))
instruction_repository.update(updated_instruction)
return json.loads(json_util.dumps(updated_instruction))
26 changes: 13 additions & 13 deletions dataherald/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
QuestionRequest,
ScannerRequest,
TableDescriptionRequest,
UpdateInstruction,
UpdateQueryRequest,
)

Expand Down Expand Up @@ -137,30 +138,30 @@ def __init__(self, settings: Settings):
)

self.router.add_api_route(
"/api/v1/{db_connection_id}/instructions",
"/api/v1/instructions",
self.add_instruction,
methods=["POST"],
tags=["Instructions"],
)

self.router.add_api_route(
"/api/v1/{db_connection_id}/instructions",
"/api/v1/instructions",
self.get_instructions,
methods=["GET"],
tags=["Instructions"],
)

self.router.add_api_route(
"/api/v1/{db_connection_id}/instructions/{instruction_id}",
"/api/v1/instructions/{instruction_id}",
self.delete_instruction,
methods=["DELETE"],
tags=["Instructions"],
)

self.router.add_api_route(
"/api/v1/{db_connection_id}/instructions/{instruction_id}",
"/api/v1/instructions/{instruction_id}",
self.update_instruction,
methods=["PATCH"],
methods=["PUT"],
tags=["Instructions"],
)

Expand Down Expand Up @@ -261,11 +262,11 @@ def get_golden_records(self, page: int = 1, limit: int = 10) -> List[GoldenRecor
return self._api.get_golden_records(page, limit)

def add_instruction(
self, db_connection_id: str, instruction_request: InstructionRequest
self, instruction_request: InstructionRequest
) -> Instruction:
"""Adds an instruction"""
created_records = self._api.add_instruction(
db_connection_id, instruction_request
instruction_request
)

# Return a JSONResponse with status code 201 and the location header.
Expand All @@ -276,22 +277,21 @@ def add_instruction(
)

def get_instructions(
self, db_connection_id: str, page: int = 1, limit: int = 10
self, db_connection_id: str = "", page: int = 1, limit: int = 10
) -> List[Instruction]:
"""Gets instructions"""
return self._api.get_instructions(db_connection_id, page, limit)

def delete_instruction(self, db_connection_id: str, instruction_id: str) -> dict:
def delete_instruction(self, instruction_id: str) -> dict:
"""Deletes an instruction"""
return self._api.delete_instruction(db_connection_id, instruction_id)
return self._api.delete_instruction(instruction_id)

def update_instruction(
self,
db_connection_id: str,
instruction_id: str,
instruction_request: InstructionRequest,
instruction_request: UpdateInstruction,
) -> Instruction:
"""Updates an instruction"""
return self._api.update_instruction(
db_connection_id, instruction_id, instruction_request
instruction_id, instruction_request
)
4 changes: 3 additions & 1 deletion dataherald/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class NLQuery(BaseModel):
question: str
db_connection_id: str

class UpdateInstruction(BaseModel):
instruction: str

class InstructionRequest(BaseModel):
class InstructionRequest(DBConnectionValidation):
instruction: str


Expand Down
14 changes: 4 additions & 10 deletions docs/api.add_instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ To return an accurate response based on our your business rules, you can set som

Request this ``POST`` endpoint::

/api/v1/{db_connection_id}/instructions

** Parameters **

.. csv-table::
:header: "Name", "Type", "Description"
:widths: 20, 20, 60

"db_connection_id", "string", "Database connection we want to add instructions, ``Required``"
/api/v1/instructions

**Request body**

.. code-block:: rst
{
"instruction": "string"
"db_connection_id": "database_connection_id"
}
**Responses**
Expand All @@ -44,10 +37,11 @@ Only set a instruction for a database connection
.. code-block:: rst
curl -X 'POST' \
'<host>/api/v1/{db_connection_id}/instructions' \
'<host>/api/v1/instructions' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"instruction": "string",
"db_connection_id": "database_connection_id"
}'
7 changes: 3 additions & 4 deletions docs/api.delete_instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ If your business logic requires to delete a instruction, you can use this endpoi

Request this ``DELETE`` endpoint::

/api/v1/{db_connection_id}/instructions/{instruction_id}
/api/v1/instructions/{instruction_id}

** Parameters **

.. csv-table::
:header: "Name", "Type", "Description"
:widths: 20, 20, 60

"db_connection_id", "string", "Database connection we want to delete the instructions, ``Required``"
"instruction_id", "string", "Instruction id we want to delete, ``Required``"
"instruction_id", "string", "Instruction id we want to delete, ``Required``"

**Responses**

Expand All @@ -35,6 +34,6 @@ Only set a instruction for a database connection
.. code-block:: rst
curl -X 'DELETE' \
'http://localhost/api/v1/{db_connection_id}/instructions/{instruction_id}' \
'<host>/api/v1/instructions/{instruction_id}' \
-H 'accept: application/json'
6 changes: 3 additions & 3 deletions docs/api.list_instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ You can use this endpoint to retrieve a list of instructions for a database conn

Request this ``GET`` endpoint::

GET /api/v1/{db_connection_id}/instructions
GET /api/v1/instructions

** Parameters **

.. csv-table::
:header: "Name", "Type", "Description"
:widths: 20, 20, 60

"db_connection_id", "string", "Database connection we want to get instructions, ``Required``"
"db_connection_id", "string", "Database connection we want to get instructions, ``Optional``"
"page", "integer", "Page number, ``Optional``"
"limit", "integer", "Limit number of instructions, ``Optional``"

Expand All @@ -38,6 +38,6 @@ HTTP 201 code response
.. code-block:: rst
curl -X 'GET' \
'<host>/api/v1/{db_connection_id}/instructions?page=1&limit=10' \
'<host>/api/v1/instructions?page=1&limit=10' \
-H 'accept: application/json
11 changes: 5 additions & 6 deletions docs/api.update_instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ Update Instructions
=======================

In order to get the best performance from the engine you should try using different instructions for each database connection.
You can update the instructions for a database connection using the ``PATCH`` method.
You can update the instructions for a database connection using the ``PUT`` method.

Request this ``PATCH`` endpoint::
Request this ``PUT`` endpoint::

/api/v1/{db_connection_id}/instructions/{instruction_id}
/api/v1/instructions/{instruction_id}

** Parameters **

.. csv-table::
:header: "Name", "Type", "Description"
:widths: 20, 20, 60

"db_connection_id", "string", "Database connection we want to update the instructions, ``Required``"
"instruction_id", "string", "Instruction id we want to update, ``Required``"

**Request body**
Expand Down Expand Up @@ -45,8 +44,8 @@ Only set a instruction for a database connection

.. code-block:: rst
curl -X 'PATCH' \
'<host>/api/v1/{db_connection_id}/instructions/{instruction_id}' \
curl -X 'PUT' \
'<host>/api/v1/instructions/{instruction_id}' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
Expand Down

0 comments on commit c830f97

Please sign in to comment.