Files
Pivot/services/signpost_byod_service.py
T
2026-10-14 13:34:37 +02:00

140 lines
5.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
from config import Config
BASE_DATA_URL = "https://byod-api.signpost.be/v1"
def get_signpost_token():
"""Token met de Signpost/M4S-gegevens van de huidige school (None bij een fout)."""
return signpost_token_met_reden()[0]
def signpost_token_met_reden():
"""Geeft (token, None) of (None, leesbare foutmelding)."""
if not (Config.M4S_CLIENT_ID and Config.M4S_CLIENT_SECRET):
return None, ("Signpost is nog niet ingesteld voor deze school: vul de client-ID en het client-secret in "
"via 🔌 Koppelingen van deze school › Signpost / M4S.")
auth_payload = {
"grant_type": "client_credentials",
"client_id": Config.M4S_CLIENT_ID,
"client_secret": Config.M4S_CLIENT_SECRET,
"scope": "openid",
"resource": "management_portal:api"
}
try:
response = requests.post(Config.M4S_AUTH_URL, data=auth_payload, timeout=15)
except requests.exceptions.RequestException as e:
return None, f"Signpost ({Config.M4S_AUTH_URL}) is niet bereikbaar: {e}"
if response.status_code in (400, 401, 403):
detail = ''
try:
j = response.json()
detail = j.get('error_description') or j.get('error') or ''
except ValueError:
pass
return None, (f"Signpost weigert de aanmelding (HTTP {response.status_code}{': ' + detail if detail else ''}). "
"Controleer de client-ID en het client-secret bij 🔌 Koppelingen van deze school.")
if not response.ok:
return None, f"Signpost gaf foutcode {response.status_code} bij het aanmelden."
token = response.json().get("access_token")
return (token, None) if token else (None, "Signpost gaf geen token terug.")
#-------------------------------------------------------------------------------------------------
#herstelling signpost doorgeven
#-------------------------------------------------------------------------------------------------
def create_signpost_case(serial_number, description, damage_flags=None):
"""
Maakt een nieuw herstellingsdossier aan bij Signpost.
:param serial_number: Het serienummer van het toestel.
:param description: Omschrijving van het probleem.
:param damage_flags: Dict met booleans (bv. {'waterDamage': True})
"""
token = get_signpost_token()
if not token:
return False, "Authenticatie mislukt."
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"Accept": "application/json"
}
# Bouw het payload op basis van de Config klasse
payload = {
"languageCode": getattr(Config, "M4S_LANGUAGE_CODE", "nl-BE"),
"description": description,
"status": getattr(Config, "M4S_REPAIR_STATUS", "Nieuw"),
"repairLocationType": int(getattr(Config, "M4S_REPAIR_LOCATION_TYPE", 1)),
"serialNumber": serial_number,
"institutionGuid": Config.M4S_INSTITUTIONGUID,
"address": {
"street": Config.M4S_REPAIR_STREET,
"number": Config.M4S_REPAIR_STREETNUMBER,
"zipCode": Config.M4S_REPAIR_ZIPCODE,
"city": Config.M4S_REPAIIR_CITY,
"countryCode": Config.M4S_REPAIR_COUNTRYCODE,
"repairAtInstitution": True
},
"contacts": [{
"firstName": Config.M4S_CONTACT_FIRST_NAME,
"lastName": Config.M4S_CONTACT_LAST_NAME,
"email": Config.M4S_CONTACT_EMAIL,
"phone": Config.M4S_CONTACT_PHONE
}],
"billingContact": {
"firstName": Config.M4S_BILLING_FIRST_NAME,
"lastName": Config.M4S_BILLING_LAST_NAME,
"email": Config.M4S_BILLING_EMAIL,
"phone": Config.M4S_BILLING_PHONE,
"street": Config.M4S_REPAIR_STREET,
"number": Config.M4S_REPAIR_STREETNUMBER,
"zipCode": Config.M4S_REPAIR_ZIPCODE,
"city": Config.M4S_REPAIIR_CITY,
"countryCode": Config.M4S_REPAIR_COUNTRYCODE
}
}
# Voeg schade-indicatoren toe indien meegegeven
if damage_flags:
payload.update(damage_flags)
try:
res = requests.post(f"{BASE_DATA_URL}/field-service/cases", headers=headers, json=payload, timeout=15)
if res.status_code in [200, 201]:
return True, res.json().get("ourReference", "Succesvol aangemaakt")
else:
return False, f"API Fout {res.status_code}: {res.text}"
except Exception as e:
return False, f"Verbindingsfout: {str(e)}"
#-------------------------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------------------------
def get_cases_by_serial(serial_number):
token = get_signpost_token()
if not token:
return []
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/json"
}
params = {"$search": serial_number}
try:
res = requests.get(f"{BASE_DATA_URL}/field-service/cases", headers=headers, params=params, timeout=15)
if res.status_code == 200:
return res.json().get("items", [])
return []
except Exception as e:
print(f"Signpost API Fout: {e}")
return []