atoti.Session.endpoint()#

Session.endpoint(route, *, method='GET')#

Create a custom endpoint at f"/proxy/{route}".

Note

Calling this method overrides atoti.Session.proxy’s url.

The endpoint logic is written in Python but the endpoint is exposed by Atoti Server. This allows to deploy the project in a container or a VM with a single opened port (the one of Atoti Server) instead of two.

The decorated function must:

Parameters:
  • route (str) – The template of the path after "/proxy". For instance, if "foo/bar" is passed, a request to "/proxy/foo/bar?query=string will match. Path parameters can be configured by wrapping their name in curly braces in the template.

  • method (Literal['DELETE', 'GET', 'PATCH', 'POST', 'PUT']) – The HTTP method the request must be using to trigger this endpoint. DELETE, PATCH, POST, and PUT requests can have a body but it must be JSON.

Return type:

Callable[[Callable[[Request, User, Session], JsonValue]], Callable[[Request, User, Session], JsonValue]]

Example

>>> import httpx
>>> df = pd.DataFrame(
...     columns=["Year", "Month", "Day", "Quantity"],
...     data=[
...         (2019, 7, 1, 15),
...         (2019, 7, 2, 20),
...     ],
... )
>>> table = session.read_pandas(
...     df, keys={"Year", "Month", "Day"}, table_name="Quantity"
... )
>>> @session.endpoint("tables/{table_name}/count", method="GET")
... def get_table_row_count(request, user, session, /):
...     table_name = request.path_parameters["table_name"]
...     return session.tables[table_name].row_count
>>> response = httpx.get(f"{session.url}/proxy/tables/{table.name}/count")
>>> response.raise_for_status().json()
2
>>> @session.endpoint("tables/{table_name}/rows", method="POST")
... def append_rows_to_table(request, user, session, /):
...     rows = request.body
...     table_name = request.path_parameters["table_name"]
...     table = session.tables[table_name]
...     dataframe = pd.DataFrame(rows, columns=list(table))
...     table.load(dataframe)
>>> response = httpx.post(
...     f"{session.url}/proxy/tables/{table.name}/rows",
...     json=[
...         (2021, 5, 19, 50),
...         (2021, 5, 20, 6),
...     ],
... )
>>> response.status_code
200
>>> response = httpx.get(f"{session.url}/proxy/tables/{table.name}/count")
>>> response.raise_for_status().json()
4
>>> table.head()
                Quantity
Year Month Day
2019 7     1          15
           2          20
2021 5     19         50
           20          6