Python is the industry standard for predictive model implementation and data science workflows.
Table of Contents
1. Authentication
Authenticate with the API to get a Bearer token.
python
import requests
def login(service_id, password):
url = "https://api.yookr.com/serviceLogin"
data = {
"service_id": service_id,
"password": password
}
response = requests.post(url, json=data)
response.raise_for_status()
return response.json()['token']2. Fetch Jobs
Get the list of active jobs assigned to your service version.
python
def fetch_jobs(token, service_id):
url = f"https://api.yookr.com/api/v1/jobs/service/{service_id}"
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()3. Process Each Job
Loop through the jobs. For each job, identify its input bindings (e.g., Location).
python
for job in jobs:
# Find the location binding to know WHERE to forecast for
location_binding = next((b for b in job['job_role_bindings'] if b['input_type'] == 'location'), None)
if not location_binding:
continue
# Access the location name and coordinates
location = location_binding['location']
print(f"Processing job {job['id']} for location: {location['name']}")
# Call your external data source (e.g., Weather API)
weather_data = my_weather_fetcher(location['latitude'], location['longitude'])
# ... Map weather_data to the job targets ...4. Map Data to Targets
The most important step is mapping your external data to the target_id requested by the job.
python
predictions_for_this_run = []
for target in job['job_targets']:
# If the job asks for a specific "temperature" role
if target['role'] == "temperature":
# Pull the corresponding value from your external data
temp_value = weather_data.get('current_temp')
# Add to the payload using the target's explicit target_id
predictions_for_this_run.append({
"target_id": target['target_id'], # CRITICAL: Use the ID from the job
"value_num": temp_value
})5. Submit Predictions
Send the constructed payload back to the API.
python
def submit_predictions(token, job_id, predictions):
url = "https://api.yookr.com/api/v1/predictions"
headers = {"Authorization": f"Bearer {token}"}
payload = {
"job_id": job_id,
"predictions": [
{
"datetime_measure": "2023-10-27T10:00:00Z", # Current timestamp
"targets": predictions
}
]
}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()Core Advantage
By using this Job-Driven pattern, your code becomes generic. If you need to forecast for a new location, you simply configure it in the Yookr Dashboard and your Python worker will pick it up automatically during its next run!