Python (or Go) for Automation
25 min
Day 28: Calling APIs and manipulating YAML/JSON
Talking to APIs and reshaping data
Most real automation is: call an API, get JSON back, transform it, do something with the result. Python's requests library and standard json/yaml modules make this a few lines of code.
Calling an API and parsing JSON
import requests
response = requests.get('https://api.github.com/repos/kubernetes/kubernetes')
response.raise_for_status() # throws if status >= 400
data = response.json()
print(f"Stars: {data['stargazers_count']}")Reading and editing YAML (e.g. a Kubernetes manifest)
import yaml
with open('deployment.yaml') as f:
manifest = yaml.safe_load(f)
manifest['spec']['replicas'] = 3
with open('deployment.yaml', 'w') as f:
yaml.safe_dump(manifest, f)Always check the status before trusting the body
raise_for_status() (or checking response.status_code) before parsing JSON prevents a whole class of bugs where an error page or empty body gets silently parsed as if it were valid data.
Key terms
- requests
- Python's most common HTTP client library.
- yaml.safe_load
- Parses YAML into Python data structures without executing arbitrary Python objects (unlike the unsafe yaml.load).
Why use yaml.safe_load instead of yaml.load on YAML from an untrusted source?