SPB Git

Repository Public License

asc.py 3.3 KB 57 lines python

1#!/usr/bin/env python32# ORIX Secure3# File: Scripts/asc.py4# Description: Read-mostly App Store Connect API client. Credentials come from ~/.config/orix-secure/asc.env; nothing secret is stored in the repository.5#6# Author: Simon-Pierre Boucher7# Contact: contact@spboucher.ai8#9# usage: Scripts/asc.py GET '/v1/apps?filter[bundleId]=ai.spboucher.orix.secure'10#        Scripts/asc.py status          summary of bundle IDs, capabilities, app record and builds for ORIX Secure11import json, os, pathlib, sys, time, urllib.error, urllib.parse, urllib.request12import jwt1314BUNDLE = "ai.spboucher.orix.secure"15env = {}16for line in (pathlib.Path.home() / ".config/orix-secure/asc.env").read_text().splitlines():17    if "=" in line and not line.strip().startswith("#"):18        k, v = line.split("=", 1); env[k.strip()] = os.path.expandvars(os.path.expanduser(v.strip().strip('"').strip("'")))1920def token():21    now = int(time.time())22    return jwt.encode({"iss": env["ASC_ISSUER_ID"], "iat": now, "exp": now + 900, "aud": "appstoreconnect-v1"},23                      pathlib.Path(env.get("ASC_KEY_PATH") or pathlib.Path.home() / ".appstoreconnect/private_keys" / f"AuthKey_{env['ASC_KEY_ID']}.p8").read_text(), algorithm="ES256", headers={"kid": env["ASC_KEY_ID"]})2425def call(method, path, payload=None):26    base, _, query = path.partition("?")27    url = "https://api.appstoreconnect.apple.com" + base + ("?" + urllib.parse.quote(query, safe="=&,") if query else "")28    req = urllib.request.Request(url, method=method, data=json.dumps(payload).encode() if payload is not None else None,29                                 headers={"Authorization": "Bearer " + token(), "Content-Type": "application/json"})30    try:31        with urllib.request.urlopen(req, timeout=60) as r: return r.status, json.loads(r.read() or b"{}")32    except urllib.error.HTTPError as e:33        return e.code, json.loads(e.read() or b"{}")3435def status():36    s, d = call("GET", f"/v1/bundleIds?filter[identifier]={BUNDLE}&include=bundleIdCapabilities&limit=20")37    caps = {i["id"]: i["attributes"].get("capabilityType") for i in d.get("included", [])}38    for b in d.get("data", []):39        mine = [caps.get(c["id"]) for c in b["relationships"]["bundleIdCapabilities"]["data"]]40        print(f"bundle id  {b['attributes']['identifier']:<40} platform {b['attributes']['platform']}  capabilities {mine or '—'}")41    if not d.get("data"): print("bundle id  none registered")42    s, d = call("GET", f"/v1/apps?filter[bundleId]={BUNDLE}")43    apps = d.get("data", [])44    if not apps: print("app record none (the App Store Connect record has not been created)"); return45    for a in apps:46        at = a["attributes"]47        print(f"app record {a['id']}  name “{at['name']}”  sku {at['sku']}  primary locale {at['primaryLocale']}")48        s, b = call("GET", f"/v1/builds?filter[app]={a['id']}&limit=10&sort=-uploadedDate")49        for x in b.get("data", []):50            xa = x["attributes"]; print(f"  build {xa['version']}  {xa['processingState']}  uploaded {xa['uploadedDate']}  expired {xa['expired']}")51        if not b.get("data"): print("  builds none uploaded")5253if __name__ == "__main__":54    if sys.argv[1:] == ["status"]: status(); sys.exit(0)55    code, data = call(sys.argv[1], sys.argv[2], json.loads(sys.argv[3]) if len(sys.argv) > 3 else None)56    print(code); print(json.dumps(data, indent=1, ensure_ascii=False)[:6000])57
Files main
Menu

Theme