Skip to main content

Python - Code Example

Below is a code example using Python to call the Edgify Agent's API endpoints

import json

import requests
from requests.auth import HTTPDigestAuth

agent_url = "http://localhost:8090/api/v2/"
api_key = '[API Key goes here]'
auth = HTTPDigestAuth('edgify-client', api_key)
headers = {
'Content-Type': 'application/json'
}

# inform edgify on transaction start
requests.request("POST", agent_url + 'events', headers=headers, auth=auth, data=json.dumps({'name': 'StartCustomerSession'}))

# have the Edgify agent capture an image and return a prediction
prediction_params = json.dumps({
'family': 'Vegetables',
'uxSource': 'AutoScale'
})
response = requests.request("POST", agent_url + 'predictions/capture-and-predict', headers=headers, auth=auth,
data=prediction_params)

if response.status_code != 200:
print(f'Error when trying to capture and predict: status {response.status_code}, message: {response.text}')
exit(0)

prediction = response.json()
uuid = prediction["uuid"]

print("Successfully got a prediction from the Edgify Agent")
print(f' Prediction UUID: {uuid}')
print(f' Prediction options: {prediction["options"]}')

# Autobuy flag
if prediction['certain']:
print('Using autobuy')

# after the user created the ground truth, pass to the agent the ground truth together with the prediction object
ground_truth = json.dumps({
'label': 'CucumberSKU',
'family': 'Vegetables',
'uxSource': 'RegularMenuSelection',
'prediction': prediction
})

response = requests.request("POST", agent_url + 'ground-truths', headers=headers, auth=auth, data=ground_truth)
if response.status_code != 201:
print(f'Error when trying to store ground truth: status {response.status_code}, message: {response.text}')
else:
print("Successfully stored a ground-truth sample")

# if you need to delete a sample
response = requests.request("DELETE", agent_url + 'ground-truths/' + uuid, auth=auth)
if response.status_code != 200:
print(f'Error when trying to delete ground truth sample: status {response.status_code}, message: {response.text}')
else:
print("Successfully deleted the ground truth sample from the Edgify Agent")

# inform edgify on transaction end
requests.request("POST", agent_url + 'events', auth=auth, data=json.dumps({'name': 'StopCustomerSession'}))