Managing APIs (SDK) Using APIs
API Descriptionβ
1. Creating an API Endpoint
In the AskTable API, use the extapi
object to manage API Endpoints. To create a new API Endpoint, you can use the following code:
from asktable import AskTable
from atserver import config
token = config.at_auth_by_default_token
at = AskTable(token=token, api_url="https://your-api-url")
# Create a new API Endpoint
extapi = at.extapis.create(
name="Test API",
base_url="https://api.example.com/v1",
headers={"Authorization": "Bearer testtoken"}
)
print(extapi)
2. Updating an API Endpoint
You can update the name, base_url, and headers of an API Endpoint:
# Update the name of the API Endpoint
extapi = extapi.update(name="Updated Test API")
print(extapi)
# Update the headers of the API Endpoint
extapi = extapi.update(headers={"Authorization": "Bearer newtoken"})
print(extapi)
# Update the base_url of the API Endpoint
extapi = extapi.update(base_url="https://api.example.com/v2")
print(extapi)
3. Retrieving an API Endpoint
You can retrieve an API Endpoint by name or ID:
extapi = at.extapis.get(name="Updated Test API")
print(extapi)
extapi = at.extapis.get(id=extapi.id)
print(extapi)
4. Deleting an API Endpoint
Delete an API Endpoint:
extapi.delete()
5. Creating an API Route
Create a new Route under a specific API Endpoint:
route = extapi.routes.create(
name="Test Route",
path="/test",
method="GET",
route_params="{'id': 'Test ID'}",
query_params="{'filter': 'Filter condition'}",
body_params="{'data': 'Data payload'}"
)
print(route)
6. Updating an API Route
You can update the name, path, and query parameters of a Route:
# Update the name of the Route
route = route.update(name="Updated Test Route")
print(route)
# Update the path of the Route
route = route.update(path="/test_updated")
print(route)
# Update the query parameters of the Route
route = route.update(query_params="{'filter': 'Updated filter condition'}")
print(route)
7. Retrieving an API Route
Retrieve a Route by name or ID:
route = extapi.routes.get(id=route.id)
print(route)
route = extapi.routes.get(name="Updated Test Route")
print(route)
8. Deleting an API Route
Delete a Route:
route.delete()
Example Codeβ
Complete example code:
from asktable import AskTable
from atserver import config
token = config.at_auth_by_default_token
at = AskTable(token=token, api_url="https://your-api-url")
# Create a new API Endpoint
extapi = at.extapis.create(
name="Test API",
base_url="https://api.example.com/v1",
headers={"Authorization": "Bearer testtoken"}
)
# Update the name of the API Endpoint
extapi = extapi.update(name="Updated Test API")
# Create a new API Route
route = extapi.routes.create(
name="Test Route",
path="/test",
method="GET",
route_params="{'id': 'Test ID'}",
query_params="{'filter': 'Filter condition'}",
body_params="{'data': 'Data payload'}"
)
# Update the name of the Route
route = route.update(name="Updated Test Route")
# Retrieve and print the Route
route = extapi.routes.get(id=route.id)
print(route)
# Delete the Route and Endpoint
route.delete()
extapi.delete()