nbox.subway
These function are meant to make jobs easier and contain the code for what I am calling Subway
, which converts any OpenAPI spec into an RPC-like interface. At the end of the day, each API call is really nothing but a functional call with added steps like serialisation and networking combined. This kind of added complexity can:
- Make code look really ugly, think try-catches and
r.raise_for_request()
- Easy to develop because information has to be packaged as different parts for each type of function call (GET, POST, ...). The true intent was always to pass it the relevant information and forget about underlying details.
- API endpoints are just strings and it's easy to have a typo in the URL. When the
Subway
is loaded with the OpenAPI spec, it will disallow incorrect URLs. while managing the parameters correctly. - A more pythonic way of programming where you can use the dot notation.
Based os these ideas there are three types of subways:
Subway
: Does not load OpenAPI spec and will blindly call the API, avoid thisSub30
: Built for OpenAPI v3.0.0, this becomesnbox_ws_v1
SpecSubway
: This is used with the FastAPI's OpenAPI spec, this is used in systems that use FastAPI (eg. Compute Server)
Functions
Classes
- _url -
the base url of the API.
- _api -
OpenAPI json dict.
- _session -
Session object to use for requests.
- prefix -
This is internal, do not use it explicitly.
- last_call_at -
This is internal, do not use it explicitly.
Like Subway but built for Nimblebox Webserver APIs.
Example:
# Create a stub
ws = Sub30(
_url = "https://my-web.site/",
_session = nbox_session,
_api = loads(fetch("https://my-web.site/openapi.json", True).decode()),
)
# Call things as if they are functions
ws.users()
ws.users("post", name = "Fiona Apple")
Creates a keepalive thread. Need to define this as a function because sleeping can cause it to behave unlike a normal blocking program, so use this carefully.
- _method -
if only one method is present this will be ignored else "get" will be used. Defaults to None.
Call the API.
Subway but for fastAPI OpenAPI spec.
Create an abstract for the Sublime RPC service which implements VPC secure calls for NimbleBox Monitoring and Relics
function to convert the incoming message to sublime format