60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Simple Python GraphQL client example for Ordrestyring API.
|
|
|
|
Requires: requests
|
|
|
|
Usage: python3 python_client.py
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
import requests
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(__file__))
|
|
KEY_FILE = os.path.join(ROOT, 'apikey')
|
|
API_URL = 'https://graphql.ordrestyring.dk/graphql'
|
|
|
|
def read_key():
|
|
with open(KEY_FILE, 'r') as f:
|
|
return f.read().strip()
|
|
|
|
def unix_to_iso(ts):
|
|
try:
|
|
return datetime.utcfromtimestamp(int(ts)).isoformat() + 'Z'
|
|
except Exception:
|
|
return ts
|
|
|
|
def main():
|
|
token = read_key()
|
|
headers = {
|
|
'Authorization': f'Bearer {token}',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
query = '''query { cases(pagination: {cursor: null, limit: 5}, orderBy: {field: "updatedAt", direction: DESC}) { items { id caseNumber projectName description offerTotal status { id text } customer { id name } createdAt updatedAt } nextCursor previousCursor } }'''
|
|
payload = { 'query': query }
|
|
r = requests.post(API_URL, headers=headers, json=payload)
|
|
r.raise_for_status()
|
|
data = r.json()
|
|
if 'errors' in data:
|
|
print('Errors:', json.dumps(data['errors'], indent=2), file=sys.stderr)
|
|
return
|
|
items = data.get('data', {}).get('cases', {}).get('items', [])
|
|
for it in items:
|
|
print('---')
|
|
print('id:', it.get('id'))
|
|
print('caseNumber:', it.get('caseNumber'))
|
|
print('projectName:', it.get('projectName'))
|
|
print('description:', it.get('description'))
|
|
print('offerTotal:', it.get('offerTotal'))
|
|
status = it.get('status') or {}
|
|
print('status:', status.get('id'), status.get('text'))
|
|
customer = it.get('customer') or {}
|
|
print('customer:', customer.get('id'), customer.get('name'))
|
|
print('createdAt:', unix_to_iso(it.get('createdAt')))
|
|
print('updatedAt:', unix_to_iso(it.get('updatedAt')))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|