"""Server-side GET example. Credentials come from your secret manager."""
import hashlib
import hmac
import os
import secrets
import time
import urllib.request

base = os.environ.get('XEOTEC_BASE_URL', 'https://sandbox-api.xeotec.in')
key = os.environ['XEOTEC_API_KEY']
secret = os.environ['XEOTEC_API_SECRET']
path = '/api/v1/auth/ping'
timestamp = str(int(time.time()))
nonce = secrets.token_hex(24)
body_hash = hashlib.sha256(b'').hexdigest()
canonical = '\n'.join(['GET', path, timestamp, nonce, body_hash])
signature = hmac.new(secret.encode(), canonical.encode(), hashlib.sha256).hexdigest()
request = urllib.request.Request(base + path, headers={
    'x-api-key': key, 'x-timestamp': timestamp, 'x-nonce': nonce, 'x-signature': signature
})
with urllib.request.urlopen(request, timeout=30) as response:
    print('HTTP', response.status)
