import json
import os
import urllib.error
import urllib.request

body = json.dumps({
	"serviceid": 2,
	"name": "Customer Name",
	"email": "customer@example.com"
}).encode("utf-8")
request = urllib.request.Request(
	"https://starbonding.com/api/v1/create.account.php",
	data=body,
	method="POST",
	headers={
		"X-API-Key": os.environ["STARBONDING_API_KEY"],
		"Idempotency-Key": "create-customer-0001",
		"Content-Type": "application/json"
	})
try:
	with urllib.request.urlopen(request, timeout=30) as response:
		data = json.loads(response.read().decode("utf-8"))
		print("HTTP", response.status)
		print(json.dumps(data))
		if data.get("success") is not True:
			raise SystemExit(1)
except urllib.error.HTTPError as error:
	print("HTTP", error.code)
	print(error.read().decode("utf-8"))
	raise SystemExit(1)
