Request examples¶
Because it might be difficult to figure out how to make a request from the framework you are using, we have collected a few examples here for various environments.
Below you can check some examples for creating a direct request to the default version of a deployment and to a pipeline.
API endpoints
See this page on API endpoints to find the right endpoint URLs to use with the examples below.
Direct requests to a default deployment version¶
Making a direct request to the default version of a deployment with the Python Client Library:
# Using the Python Client Library
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
api = ubiops.CoreApi(client)
data = {'input_field_1': 123}
# A direct request to the default version
request_result = api.deployment_requests_create(
project_name=PROJECT_NAME,
deployment_name=DEPLOYMENT_NAME,
data=data
)
print(request_result)
The same request, now using generic methods in different programming languages:
# Using a 'raw' HTTP request
import requests
# You can copy the endpoint URL from the WebApp or look up the format on Swagger
receive = requests.post("<deployment endpoint url>/requests",
json = {'input field 1': 123},
headers={'Authorization':'Token <YOUR_API_TOKEN>'})
print(receive.json()['result'])
# Using a 'raw' HTTP request
content_type <- httr::content_type_json()
body <- rjson::toJSON(list(input_field_1 = 123))
headers <- httr::add_headers(Authorization="Token <YOUR_API_TOKEN>")
r <- httr::POST("<deployment endpoint url>/requests", headers, content_type, body = body, encode = "json")
content <- httr::content(r, "text", encoding = "UTF-8")
print(content)
// Helper function for posting a request
async function postRequest(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: API_TOKEN,
},
body: JSON.stringify(data),
});
return response.json();
}
// Using the postRequest function
postRequest(
`<deployment endpoint url>/requests`,
{ input_field_1: 123 }
).then((response) => doSomethingWith(response))
Direct requests to a default pipeline version¶
Making a direct request to the default version of a pipeline with the Python Client Library:
# Using the Python Client Library
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
api = ubiops.CoreApi(client)
data = {'input_field_1': 123}
request_result = api.pipeline_requests_create(
project_name=PROJECT_NAME,
pipeline_name=PIPELINE_NAME,
data=data
)
print(request_result)
The same, now using generic methods:
# Using a 'raw' HTTP request
import requests
# You can copy the endpoint URL from the WebApp or look up the format on Swagger
receive = requests.post(
url='<pipeline endpoint url>/requests',
json={'input field 1': 123},
headers={'Authorization':'Token <YOUR_API_TOKEN>'}
)
print(receive.json()['result'])
# Using a 'raw' HTTP request
content_type <- httr::content_type_json()
body <- rjson::toJSON(list(input_field_1 = 123))
headers <- httr::add_headers(Authorization="Token <YOUR_API_TOKEN>")
r <- httr::POST("<pipeline endpoint url>/requests", headers, content_type, body = body, encode = "json")
content <- httr::content(r, "text", encoding = "UTF-8")
print(content)
// Helper function for posting a request
async function postRequest(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: API_TOKEN,
},
body: JSON.stringify(data),
});
return response.json();
}
// Using the postRequest function
postRequest(
`<pipeline endpoint url>/requests`,
{ input_field_1: 123 }
).then((response) => doSomethingWith(response))
Creating a streaming request to a default deployment version¶
If your deployment code supports streamed updates you can use the /stream
endpoint for creating a request where you can receive those streamed updates. Using the client library that would look like this:
import json
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
project_name = 'project-name-example'
deployment_name = 'deployment-name-example'
version = 'version-example'
data = 'data example'
timeout = 3600
full_response = False # Whether to return the full response in the end as well (optional)
# Create a streaming deployment request
for item in ubiops.utils.stream_deployment_request(
client=client,
project_name=project_name,
deployment_name=deployment_name,
version=version,
data=data,
timeout=timeout,
full_response=full_response,
):
print(item, end = '')
It works similarly for pipelines:
import json
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
project_name = 'project-name-example'
pipeline_name = 'pipeline-name-example'
version = 'version-example'
data = 'data example'
timeout = 7200
full_response = False # Whether to return the full response in the end as well (optional)
# Create a streaming pipeline request
for item in ubiops.utils.stream_pipeline_request(
client=client,
project_name=project_name,
pipeline_name=pipeline_name,
version=version,
data=data,
timeout=timeout,
full_response=full_response,
):
print(item, end = '')