CoolFace
Apppublic

LZ-SG/embedded-dev-tutorials

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
test_write.py43 linesDownload Raw Back to root
1#!/usr/bin/env python32"""Test if API write works - create a test file"""3import urllib.request, json, base644TOKEN = "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"5REPO = "sg06231219-boop/embedded-dev-tutorials"6HEADERS = {"Authorization": f"token {TOKEN}", "Accept": "application/vnd.github.v3+json", "User-Agent": "test"}7 8# Test: create new file9content = base64.b64encode(b"test").decode()10body = {"message": "test write", "content": content, "branch": "main"}11url = f"https://api.github.com/repos/{REPO}/contents/test_api_write.txt"12r = urllib.request.Request(url, data=json.dumps(body).encode(), 13    headers={**HEADERS, "Content-Type": "application/json"})14try:15    with urllib.request.urlopen(r, timeout=30) as resp:16        result = json.loads(resp.read())17        print(f"CREATE OK: {result.get('content',{}).get('name')}")18except urllib.error.HTTPError as e:19    body = e.read().decode()20    print(f"CREATE FAIL HTTP {e.code}: {body[:500]}")21 22# Test: update existing file23sha_url = f"https://api.github.com/repos/{REPO}/contents/app.py?ref=main"24r2 = urllib.request.Request(sha_url, headers=HEADERS)25try:26    with urllib.request.urlopen(r2, timeout=15) as resp:27        d = json.loads(resp.read())28        sha = d["sha"]29        content2 = base64.b64encode(d["content"].encode()).decode()30    31    body2 = {"message": "test update", "content": content2, "branch": "main", "sha": sha}32    url2 = f"https://api.github.com/repos/{REPO}/contents/app.py"33    r3 = urllib.request.Request(url2, data=json.dumps(body2).encode(),34        headers={**HEADERS, "Content-Type": "application/json"})35    with urllib.request.urlopen(r3, timeout=30) as resp:36        result = json.loads(resp.read())37        print(f"UPDATE OK: {result.get('commit',{}).get('sha','?')[:7]}")38except urllib.error.HTTPError as e:39    body = e.read().decode()40    print(f"UPDATE FAIL HTTP {e.code}: {body[:500]}")41except Exception as e:42    print(f"UPDATE ERROR: {e}")43