diff --git a/README.md b/README.md index 02d7d231..3b44fca3 100644 --- a/README.md +++ b/README.md @@ -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' \ - '/api/v1/{db_connection_id}/instructions' \ + '/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' \ - '/api/v1/{db_connection_id}/instructions?page=1&limit=10' \ + '/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' \ - '/api/v1/{db_connection_id}/instructions/{instruction_id}' \ + '/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' \ - '/api/v1/{db_connection_id}/instructions/{instruction_id}' \ +curl -X 'PUT' \ + '/api/v1/instructions/{instruction_id}' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ diff --git a/dataherald/api/__init__.py b/dataherald/api/__init__.py index 48743585..b9da5068 100644 --- a/dataherald/api/__init__.py +++ b/dataherald/api/__init__.py @@ -18,6 +18,7 @@ QuestionRequest, ScannerRequest, TableDescriptionRequest, + UpdateInstruction, UpdateQueryRequest, ) @@ -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 diff --git a/dataherald/api/fastapi.py b/dataherald/api/fastapi.py index aca19c31..3838e00f 100644 --- a/dataherald/api/fastapi.py +++ b/dataherald/api/fastapi.py @@ -41,6 +41,7 @@ QuestionRequest, ScannerRequest, TableDescriptionRequest, + UpdateInstruction, UpdateQueryRequest, ) @@ -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)) diff --git a/dataherald/server/fastapi/__init__.py b/dataherald/server/fastapi/__init__.py index 3298bfb2..55dffd12 100644 --- a/dataherald/server/fastapi/__init__.py +++ b/dataherald/server/fastapi/__init__.py @@ -22,6 +22,7 @@ QuestionRequest, ScannerRequest, TableDescriptionRequest, + UpdateInstruction, UpdateQueryRequest, ) @@ -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"], ) @@ -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. @@ -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 ) diff --git a/dataherald/types.py b/dataherald/types.py index ef8a6f03..d8bebb52 100644 --- a/dataherald/types.py +++ b/dataherald/types.py @@ -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 diff --git a/docs/api.add_instructions.rst b/docs/api.add_instructions.rst index 0330308c..93fece81 100644 --- a/docs/api.add_instructions.rst +++ b/docs/api.add_instructions.rst @@ -7,15 +7,7 @@ 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** @@ -23,6 +15,7 @@ Request this ``POST`` endpoint:: { "instruction": "string" + "db_connection_id": "database_connection_id" } **Responses** @@ -44,10 +37,11 @@ Only set a instruction for a database connection .. code-block:: rst curl -X 'POST' \ - '/api/v1/{db_connection_id}/instructions' \ + '/api/v1/instructions' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "instruction": "string", + "db_connection_id": "database_connection_id" }' diff --git a/docs/api.delete_instructions.rst b/docs/api.delete_instructions.rst index f868b522..36f01265 100644 --- a/docs/api.delete_instructions.rst +++ b/docs/api.delete_instructions.rst @@ -7,7 +7,7 @@ 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 ** @@ -15,8 +15,7 @@ Request this ``DELETE`` endpoint:: :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** @@ -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}' \ + '/api/v1/instructions/{instruction_id}' \ -H 'accept: application/json' diff --git a/docs/api.list_instructions.rst b/docs/api.list_instructions.rst index 23a67c5b..db2c6d9a 100644 --- a/docs/api.list_instructions.rst +++ b/docs/api.list_instructions.rst @@ -7,7 +7,7 @@ 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 ** @@ -15,7 +15,7 @@ Request this ``GET`` endpoint:: :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``" @@ -38,6 +38,6 @@ HTTP 201 code response .. code-block:: rst curl -X 'GET' \ - '/api/v1/{db_connection_id}/instructions?page=1&limit=10' \ + '/api/v1/instructions?page=1&limit=10' \ -H 'accept: application/json diff --git a/docs/api.update_instructions.rst b/docs/api.update_instructions.rst index b0351902..2340adca 100644 --- a/docs/api.update_instructions.rst +++ b/docs/api.update_instructions.rst @@ -4,11 +4,11 @@ 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 ** @@ -16,7 +16,6 @@ Request this ``PATCH`` endpoint:: :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** @@ -45,8 +44,8 @@ Only set a instruction for a database connection .. code-block:: rst - curl -X 'PATCH' \ - '/api/v1/{db_connection_id}/instructions/{instruction_id}' \ + curl -X 'PUT' \ + '/api/v1/instructions/{instruction_id}' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{