Python Getting Started
Dependencies
- Dependency:
requests
pip install requests
Examples live under sdk/python/, key files:
TokenProvider.py: fetch tokens using client-credentials flow with simple caching.DataServiceClient.py: wrapsqueryandmutationrequests.CRUDGenericRecordExampleV2.py: full CRUD example.
Run the example
Configure environment variables (adjust as needed):
export KC_TOKEN_URL=http://localhost:8180/realms/07c5b6ff/protocol/openid-connect/token
export KC_CLIENT_ID=myservice5
export KC_CLIENT_SECRET=2rSFDy7AKcN3st3rtbZUH3PQ01Y0sPqH
export TENANT_ID=07c5b6ff
export BASE_URL=http://localhost:8080
export ENTITY_NAME=customers
export PK_FIELD=id
python sdk/python/CRUDGenericRecordExampleV2.py
Code highlights (excerpt)
The snippet below from CRUDGenericRecordExampleV2.py shows list, conditional query, create, get-by-id, update, delete, conditional update/delete, and upsert:
tp = ClientCredentialsTokenProvider(token_url, client_id, client_secret)
ds = DataServiceClient(base_url + "/data", tenant_id, tp)
lst = list_records(ds, entity_name, limit=10, offset=0)
filtered = list_filtered(ds, entity_name, limit=10, offset=0)
created = create_record(ds, entity_name, {
"name": "RecordUser",
"email": "record.user@example.com",
"age": 25,
})
created_one = created[0] if created else None
if created_one and pk_field in created_one:
idv = created_one.get(pk_field)
by_id = get_by_id(ds, entity_name, pk_field, idv)
updated = update_by_id(ds, entity_name, pk_field, idv, {"email": "record.user+updated@example.com"})
deleted = delete_by_id(ds, entity_name, pk_field, idv)
updated_where = update_where(
ds,
entity_name,
{"op": "and", "conditions": [{"field": "email", "op": "like", "value": "%example.com"}]},
{"age": 27},
)
deleted_where = delete_where(
ds,
entity_name,
{"op": "and", "conditions": [{"field": "name", "op": "like", "value": "%TempUser"}]},
)
upserted = upsert_record(ds, entity_name, ["email"], {
"name": "RecordUpsert",
"email": "record.upsert@example.com",
"age": 31,
})
Exception handling and response compatibility details are in api_data and DataServiceClient._post_json in the example source.