Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 7Naam in de cursus: Web requests and REST APIs

Talk to web APIs

Almost every tool has a REST API: GitHub, Azure, Kubernetes, monitoring, ticket systems. With requests (pip install requests) you check websites, read and change data, with tokens, timeouts and retries.

  • 5 Lessen
  • 0 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

7.1

Make GET requests

requests.get returns a response with status, headers and the body (often JSON).

get.py
import requests
 
r = requests.get("https://api.github.com/repos/s0sta/it-notes", timeout=10)
r.raise_for_status()
repo = r.json()
print(repo["full_name"], repo["stargazers_count"], repo["updated_at"])
 
r = requests.get("https://s0sta.com/", timeout=10)
print(r.status_code, r.elapsed.total_seconds(), r.headers.get("server"))

Onthoud

Always set a timeout. Without it, a script can hang forever on a dead server.

7.2

Send data and use tokens

Send JSON with json=; put the token in a header from an environment variable — never in the code.

post.py
import os
import requests
 
token = os.environ["GITHUB_TOKEN"]
headers = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"}
issue = {"title": "Disk on web1 above 90%", "labels": ["ops"]}
r = requests.post("https://api.github.com/repos/s0sta/infra-shop/issues", json=issue, headers=headers, timeout=10)
r.raise_for_status()
print("created", r.json()["html_url"])

7.3

Handle pages

APIs return big lists in pages. Follow the next link (or page number) until the end.

paging.py
import os
import requests
 
url = "https://api.github.com/orgs/github/repos?per_page=100"
headers = {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"}
names = []
while url:
    r = requests.get(url, headers=headers, timeout=10)
    r.raise_for_status()
    names += [repo["name"] for repo in r.json()]
    url = r.links.get("next", {}).get("url")
print(len(names))

7.4

Retry on errors

Networks fail sometimes. Retry with a growing wait for temporary errors (429, 5xx).

retry.py
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
 
session = requests.Session()
retry = Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504], allowed_methods=["GET", "POST"])
session.mount("https://", HTTPAdapter(max_retries=retry))
r = session.get("https://api.example.com/health", timeout=5)
print(r.status_code)

7.5

Check many websites

A small uptime checker: status, time and certificate per site.

uptime.py
import requests
 
sites = ["https://s0sta.com/", "https://web3.s0sta.com/", "https://babolatksa.com/"]
for url in sites:
    try:
        r = requests.get(url, timeout=10)
        state = "UP" if r.ok else f"HTTP {r.status_code}"
        print(f"{state:8} {r.elapsed.total_seconds():5.2f}s {url}")
    except requests.RequestException as e:
        print(f"DOWN     {url} ({e.__class__.__name__})")

Op het werk

Use the official SDKs where they exist (azure-identity + azure-mgmt-*, kubernetes, PyGithub) — they handle login, paging and retries for you.