Homeโ€บ๐Ÿ Python Extensionsโ€บModule 101 min read ยท 11/16

API Integration

Hands-on2 exercises

Making API Calls

Use the built-in requests library (bundled with the SDK):

def query(self):
    host = self.activation_config.get("host")
    token = self.activation_config.get("api_token")

    response = requests.get(
        f"https://{host}/api/v1/health",
        headers={"Authorization": f"Bearer {token}"},
        verify=True, timeout=30
    )
    data = response.json()

โš ๏ธ Always set timeout=30. Without it, a hung API blocks the entire EEC โ€” all extensions stop polling.

Parsing JSON to Metrics

for node in data.get("nodes", []):
    dims = {
        "device.address": host,
        "node.name": node["name"]
    }
    self.report_metric("my_ext.cpu", node["cpu_pct"], dims)
    self.report_metric("my_ext.memory", node["mem_used"], dims)

๐Ÿ’ก Dimensions must match what's in your extension.yaml topology rules โ€” same keys, same values.

๐Ÿ›  Try it

This load balancer extension has cumulative byte counters but the keys are missing the .count suffix. Fix bytes.in and bytes.out.

extension.yamlYAML
Loading...

Error Handling Pattern

try:
    response = requests.get(url, timeout=30)
    response.raise_for_status()
    data = response.json()
    self.report_status(Status(StatusValue.OK))
except requests.RequestException as e:
    self.report_dt_event(
        title="API Connection Failed",
        description=str(e),
        event_type="ERROR_EVENT"
    )
    self.report_status(Status(StatusValue.GENERIC_ERROR, str(e)))
    return  # Skip metric reporting this cycle

HTTP Status Codes to Handle

CodeMeaningAction
200SuccessParse response, report metrics
401UnauthorizedReport error event, check token
403ForbiddenToken lacks required permissions
404Not foundWrong endpoint URL
429Rate limitedIncrease polling interval
500+Server errorRetry next cycle, report event
๐Ÿ›  Try itExercise 2

Add a pool.health gauge metric with unit Percent to this extension.

extension.yamlYAML
Loading...

What's Next

Module 11 โ€” Add topology and screens to your Python extension.