Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
name: Check examples

# Runs only when code or chapters change: README-only PRs skip it.
on:
pull_request:
paths:
- "chapters/**"
- "examples/**"
- "scripts/**"
- "*.py"
- ".github/workflows/check.yml"
push:
branches: [main]
paths:
- "chapters/**"
- "examples/**"
- "scripts/**"
- "*.py"
- ".github/workflows/check.yml"

jobs:
test:
Expand All @@ -21,5 +34,7 @@ jobs:
run: python3 -c "import json; m = json.load(open('chapters/chapters.json')); import os; [open(os.path.join('chapters', c['file'])) for s in m['sections'] for c in s['chapters']]"
- name: Run every example script
run: scripts/test_examples.sh
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Run every inline snippet in the chapters
run: python3 scripts/check_snippets.py
10 changes: 9 additions & 1 deletion chapters/11-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,23 @@ for line in result.stdout.splitlines():
#!/usr/bin/env python3
# github_stars.py
import json
import os
import urllib.request

url = "https://api.github.com/repos/tuladhar/Python-for-SysAdmin"
with urllib.request.urlopen(url, timeout=10) as response:
request = urllib.request.Request(url)
token = os.environ.get("GITHUB_TOKEN")
if token: # optional: raises the rate limit from 60 to 5,000 requests per hour
request.add_header("Authorization", f"Bearer {token}")

with urllib.request.urlopen(request, timeout=10) as response:
repo = json.load(response)

print(f"{repo['full_name']} has {repo['stargazers_count']} stars")
```

> Without a token, the GitHub API allows only 60 requests per hour per IP address. Shared networks (offices, CI runners) hit that limit quickly and get `HTTP Error 403: rate limit exceeded`. Reading secrets from environment variables, never from the script itself, keeps them out of Git.

## Exercises

- Run `kubectl get pods -A -o json` (or use a saved sample file) and print each pod's namespace, name and phase.
Expand Down
8 changes: 7 additions & 1 deletion examples/github_stars.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/env python3
# github_stars.py
import json
import os
import urllib.request

url = "https://api.github.com/repos/tuladhar/Python-for-SysAdmin"
with urllib.request.urlopen(url, timeout=10) as response:
request = urllib.request.Request(url)
token = os.environ.get("GITHUB_TOKEN")
if token: # optional: raises the rate limit from 60 to 5,000 requests per hour
request.add_header("Authorization", f"Bearer {token}")

with urllib.request.urlopen(request, timeout=10) as response:
repo = json.load(response)

print(f"{repo['full_name']} has {repo['stargazers_count']} stars")
Loading