Zum Inhalt

Code-Beispiele

Praktische Beispiele für die Verwendung der calServer API v2.

Token erstellen

curl -X POST "https://ihre-instanz.example.com/api/v2/auth/token" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "IhrPasswort",
    "device_name": "api-client"
  }'
import requests

response = requests.post(
    "https://ihre-instanz.example.com/api/v2/auth/token",
    json={
        "email": "user@example.com",
        "password": "IhrPasswort",
        "device_name": "api-client"
    }
)
token = response.json()["token"]
const response = await fetch(
  "https://ihre-instanz.example.com/api/v2/auth/token",
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      email: "user@example.com",
      password: "IhrPasswort",
      device_name: "api-client",
    }),
  }
);
const { token } = await response.json();
$client = new \GuzzleHttp\Client();
$response = $client->post(
    'https://ihre-instanz.example.com/api/v2/auth/token',
    [
        'json' => [
            'email' => 'user@example.com',
            'password' => 'IhrPasswort',
            'device_name' => 'api-client',
        ],
    ]
);
$token = json_decode($response->getBody(), true)['token'];

Inventarliste abrufen

curl -X GET "https://ihre-instanz.example.com/api/v2/inventories?per_page=25&sort=-created_at" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: application/json"
headers = {
    "Authorization": f"Bearer {token}",
    "Accept": "application/json"
}

response = requests.get(
    "https://ihre-instanz.example.com/api/v2/inventories",
    headers=headers,
    params={"per_page": 25, "sort": "-created_at"}
)
data = response.json()

for item in data["data"]:
    print(f"{item['attributes']['inventory_number']}: {item['attributes']['manufacturer']}")

Inventar anlegen

curl -X POST "https://ihre-instanz.example.com/api/v2/inventories" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "inventory_number": "INV-2026-001",
    "manufacturer": "Fluke",
    "model": "8845A",
    "serial_number": "SN-12345",
    "status": "AKTIV"
  }'
response = requests.post(
    "https://ihre-instanz.example.com/api/v2/inventories",
    headers=headers,
    json={
        "inventory_number": "INV-2026-001",
        "manufacturer": "Fluke",
        "model": "8845A",
        "serial_number": "SN-12345",
        "status": "AKTIV"
    }
)
new_id = response.json()["data"]["id"]

Filtern und Suchen

# Inventar nach Hersteller filtern
curl -X GET "https://ihre-instanz.example.com/api/v2/inventories?filter[manufacturer]=Fluke" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: application/json"

# Kalibrierungen nach Datum sortieren
curl -X GET "https://ihre-instanz.example.com/api/v2/calibrations?sort=-calibration_date&per_page=10" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: application/json"

# Kunden nach Name suchen
curl -X GET "https://ihre-instanz.example.com/api/v2/customers?filter[name]=Labor" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: application/json"

Paginierung durchlaufen

def get_all_items(base_url, headers):
    """Alle Seiten einer paginierten API-Antwort abrufen."""
    items = []
    page = 1

    while True:
        response = requests.get(
            f"{base_url}?page={page}&per_page=100",
            headers=headers
        )
        data = response.json()
        items.extend(data["data"])

        meta = data.get("meta", {})
        if page >= meta.get("last_page", 1):
            break
        page += 1

    return items

all_inventories = get_all_items(
    "https://ihre-instanz.example.com/api/v2/inventories",
    headers
)
print(f"Gesamt: {len(all_inventories)} Inventare")

Fehlerbehandlung

response = requests.get(
    "https://ihre-instanz.example.com/api/v2/inventories/nicht-vorhanden",
    headers=headers
)

if response.status_code == 401:
    print("Token ungültig oder abgelaufen — neuen Token erstellen")
elif response.status_code == 403:
    print("Keine Berechtigung für diese Aktion")
elif response.status_code == 404:
    print("Datensatz nicht gefunden")
elif response.status_code == 422:
    errors = response.json().get("errors", {})
    for field, messages in errors.items():
        print(f"Feld '{field}': {', '.join(messages)}")
elif response.status_code >= 500:
    print("Serverfehler — Anwendungslogs prüfen")
else:
    data = response.json()
    print(f"Erfolg: {data}")

Kalibrierung mit Standard anlegen

# 1. Kalibrierung anlegen
curl -X POST "https://ihre-instanz.example.com/api/v2/calibrations" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "mtag": "inventory-uuid-here",
    "calibration_date": "2026-03-08",
    "next_calibration": "2027-03-08",
    "result": "bestanden",
    "technician": "Max Mustermann"
  }'

Antwortformat

Alle erfolgreichen API-Antworten folgen diesem Format:

{
  "data": {
    "id": "uuid",
    "type": "inventory",
    "attributes": {
      "inventory_number": "INV-2026-001",
      "manufacturer": "Fluke",
      "model": "8845A"
    },
    "relationships": {
      "customer": { "data": { "id": "uuid", "type": "customer" } }
    }
  },
  "meta": {
    "api_version": "2.0",
    "total": 150,
    "page": 1,
    "per_page": 25,
    "last_page": 6
  },
  "links": {
    "self": "https://ihre-instanz.example.com/api/v2/inventories?page=1",
    "next": "https://ihre-instanz.example.com/api/v2/inventories?page=2"
  }
}