Repository navigation
Expand file tree
/
Copy pathcreate_site.py
More file actions
52 lines (42 loc) · 1.72 KB
/
Copy pathcreate_site.py
File metadata and controls
52 lines (42 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Create a WordPress site and wait until it's ready. Python 3, standard library only.
Usage: python3 create_site.py [label] (label: 5-15 characters)
"""
import json
import os
import sys
import time
import urllib.error
import urllib.request
import uuid
API = "https://api.magicwp.io/v1"
token = os.environ.get("MAGICWP_TOKEN") or exit("Set MAGICWP_TOKEN to an API token (Settings → API tokens)")
def call(path, method="GET", body=None, headers=None):
req = urllib.request.Request(
API + path,
method=method,
data=json.dumps(body).encode() if body is not None else None,
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json", **(headers or {})},
)
try:
with urllib.request.urlopen(req) as res:
return json.load(res)["data"]
except urllib.error.HTTPError as e:
exit(f"{e.code}: {json.load(e).get('message')}")
def wait(task_id, every=5):
"""Long actions return a task; poll it until it finishes."""
while True:
t = call(f"/tasks/{task_id}")
print(f" {t['status']}")
if t["status"] == "completed":
return
if t["status"] == "failed":
exit(t["error_message"] or "Task failed")
time.sleep(every)
label = sys.argv[1] if len(sys.argv) > 1 else "My new site"
# The Idempotency-Key makes a retry of this request safe: it won't create a second site.
created = call("/sites", "POST", {"site_label": label, "site_type": "empty_site"}, {"Idempotency-Key": str(uuid.uuid4())})
site, task = created["site"], created["task"]
print(f"Creating site {site['id']}, task {task['id']}")
wait(task["id"], every=10)
ready = call(f"/sites/{site['id']}")
print(f"Ready: https://{ready['domain_name']}")