Use this section if sapi.py is not avialable in your distribution
Inside the folder create the __init__.py and the endpoint file
Tip |
---|
A descriptive name works the best |
On the endpoint file
Special attention to APIRouter
Code Block | ||||
---|---|---|---|---|
| ||||
from fastapi import APIRouter, Body from fastapi.requests import Request |
The prefix indicates the prefix of the endpoint
Info |
---|
All endpoint have the same base path, /es/api/v1, from here we attach the prefix, in this case being /Simple, so /es/api/v1/Simple |
Code Block | ||||
---|---|---|---|---|
| ||||
router = APIRouter(prefix='/Simple', tags=['Simple']) |
Tip |
---|
If you want more information on how to build routes, and what features are available please go to the FastAPI tutorial, whenever you see @app, just replace that with @router |
Code Block | ||||
---|---|---|---|---|
| ||||
@router.post('/post') async def post_request(req: Request, body: dict = Body()): return body @router.post('/put/{name}') async def post_request(req: Request, name: str): return {"name": name} @router.post('/get/{id}') async def post_request(req: Request, id: int, param_a: int = 0): return f'GET Request for id {id}, with number {param_a}' |
Info |
---|
In this scenario the path for each route is: /es/api/v1/Simple/post /es/api/v1/Simple/put/lincon /es/api/v1/Simple/get/42 |
on the __init__ file
Warning |
---|
If you don’t import the router in the __init__.py, Search API won’t load it |
Code Block | ||||
---|---|---|---|---|
| ||||
from .simple import router |
You can go to /es/docs or /es/redocand check the
API documentation
Swagger (/es/docs)
ReDoc (/es/redoc)
Using an endpoint to wrap a pipeline gives some advantages when working on a project, specially if a separate team is needs to consume said endpoint, specially when compared to use the provided Pipeline endpoint
General Pipeline Endpoint
Specific Pipeline Endpoint
We will use the Search Endpoint for this example
Create a file for the model in models/api
Info |
---|
The __init__.py is already included in this package |
on the model file
Since we are making a model from scratch we need to import BaseModel
Code Block | ||||
---|---|---|---|---|
| ||||
from typing import Optional from pydantic import BaseModel, Field, Extra from framework.aggregations.utils import SelectionAgg from models.engines import SortEntry from models.utils import BoolOperation |
All models must inherit from BaseModel or from another model
Declare parameters as we did for Stages
Code Block | ||||
---|---|---|---|---|
| ||||
class ApiSearchRequest(BaseModel): q: Optional[str] = Field(default='*', description='Query string') query: Optional[dict] = Field(default=None, description='Specific query object for search engine') knn: Optional[dict] = Field(default=None, description='Specific knn query object for search engine') size: Optional[int] = Field(default=25, description='Number of hits to return per request') sort: Optional[SortEntry] = Field(description='Sort field and order') start: Optional[int] = Field(alias='from', description='Start position for retrieving hits', ge=0) page: Optional[int] = Field(description='Start page for retrieving hits. Minimum page is 1. Not applicable when start, is being used', ge=1) fetch_fields: Optional[list[str]] = Field( description='List of fields to return in the response based on field values', title='Fetch Fields') scroll: Optional[str] = Field(default=None, description='Period to retain the search context for scrolling') default_operator: BoolOperation = Field(BoolOperation.OR, description='The default operator for query string query: AND or OR', title='Default Operator') exclude_fields: Optional[list[str]] = Field( description='List of fields to exclude in the response based on field values.', title='Exclude Fields') aggs: Optional[list[SelectionAgg]] = Field( description='List of selected aggregations. Usable only when a DynamicAggStage exist in the pipeline', title='Aggregations') |
Note | ||
---|---|---|
We will not import the model in the __init__, because we want the route to be import like this
|