-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_client.py
More file actions
executable file
·57 lines (48 loc) · 1.78 KB
/
Copy path_client.py
File metadata and controls
executable file
·57 lines (48 loc) · 1.78 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
53
54
55
56
57
import os
import json
import urllib.request
import urllib.parse
import sys
# Build base URL from environment variables
def get_base_url():
auth_key = os.environ.get('EXO_AUTH_KEY')
auth_token = os.environ.get('EXO_AUTH_TOKEN')
domain = os.environ.get('EXO_API_DOMAIN')
account_sid = os.environ.get('EXO_ACCOUNT_SID')
if not all([auth_key, auth_token, domain, account_sid]):
print("Error: Missing required environment variables (EXO_AUTH_KEY, EXO_AUTH_TOKEN, EXO_API_DOMAIN, EXO_ACCOUNT_SID)")
sys.exit(1)
return f"https://{auth_key}:{auth_token}@{domain}/v2/accounts/{account_sid}"
BASE = get_base_url()
def _request(method, path, data=None, query=None):
url = BASE + path
if query:
q = {k: v for k, v in query.items() if v is not None and str(v) != ''}
if q:
url += '?' + urllib.parse.urlencode(q)
body = None
headers = {}
if data is not None:
body = json.dumps(data).encode('utf-8')
headers['Content-Type'] = 'application/json'
try:
req = urllib.request.Request(url, data=body, headers=headers, method=method)
with urllib.request.urlopen(req) as resp:
raw = resp.read().decode('utf-8')
print(raw)
return json.loads(raw)
except urllib.error.HTTPError as e:
print(f"HTTP Error {e.code}: {e.reason}")
print(e.read().decode('utf-8'))
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def post(path, payload):
return _request('POST', path, data=payload)
def get(path, query=None):
return _request('GET', path, query=query)
def put(path, payload):
return _request('PUT', path, data=payload)
def delete(path, query=None):
return _request('DELETE', path, query=query)