forked from openfga/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_api_request_example.py
More file actions
271 lines (244 loc) · 10.2 KB
/
execute_api_request_example.py
File metadata and controls
271 lines (244 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# ruff: noqa: E402
"""
execute_api_request example — calls real OpenFGA endpoints and compares
the results with the regular SDK methods to verify correctness.
Requires a running OpenFGA server (default: http://localhost:8080).
export FGA_API_URL=http://localhost:8080 # optional, this is the default
python3 execute_api_request_example.py
"""
import asyncio
import os
import sys
sdk_path = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
sys.path.insert(0, sdk_path)
from openfga_sdk import (
ClientConfiguration,
CreateStoreRequest,
Metadata,
ObjectRelation,
OpenFgaClient,
RelationMetadata,
RelationReference,
TypeDefinition,
Userset,
Usersets,
WriteAuthorizationModelRequest,
)
from openfga_sdk.client.models import (
ClientCheckRequest,
ClientTuple,
ClientWriteRequest,
)
from openfga_sdk.credentials import Credentials
async def main():
api_url = os.getenv("FGA_API_URL", "http://localhost:8080")
configuration = ClientConfiguration(
api_url=api_url,
credentials=Credentials(),
)
async with OpenFgaClient(configuration) as fga_client:
# ─── Setup: create a store, model, and tuple ─────────────
print("=== Setup ===")
# Create a test store via the SDK
store = await fga_client.create_store(
CreateStoreRequest(name="execute_api_request_test")
)
fga_client.set_store_id(store.id)
print(f"Created store: {store.id}")
# Write an authorization model
model_resp = await fga_client.write_authorization_model(
WriteAuthorizationModelRequest(
schema_version="1.1",
type_definitions=[
TypeDefinition(type="user"),
TypeDefinition(
type="document",
relations=dict(
writer=Userset(this=dict()),
viewer=Userset(
union=Usersets(
child=[
Userset(this=dict()),
Userset(
computed_userset=ObjectRelation(
object="", relation="writer"
)
),
]
)
),
),
metadata=Metadata(
relations=dict(
writer=RelationMetadata(
directly_related_user_types=[
RelationReference(type="user"),
]
),
viewer=RelationMetadata(
directly_related_user_types=[
RelationReference(type="user"),
]
),
)
),
),
],
)
)
auth_model_id = model_resp.authorization_model_id
fga_client.set_authorization_model_id(auth_model_id)
print(f"Created model: {auth_model_id}")
# Write a tuple
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="writer",
object="document:roadmap",
),
]
)
)
print("Wrote tuple: user:anne → writer → document:roadmap")
# ─── Tests ────────────────────────────────────────────────
print("\n=== execute_api_request tests ===\n")
# ── 1. GET /stores ────────────────────────────────────────
print("1. ListStores (GET /stores)")
raw = await fga_client.execute_api_request(
operation_name="ListStores",
method="GET",
path="/stores",
query_params={"page_size": 100},
)
sdk = await fga_client.list_stores()
body = raw.json()
assert raw.status == 200, f"Expected 200, got {raw.status}"
assert "stores" in body
assert len(body["stores"]) == len(sdk.stores), (
f"Count mismatch: {len(body['stores'])} vs {len(sdk.stores)}"
)
print(f" ✅ {len(body['stores'])} stores (status {raw.status})")
# ── 2. GET /stores/{store_id} (auto-substitution) ────────
print("2. GetStore (GET /stores/{store_id})")
raw = await fga_client.execute_api_request(
operation_name="GetStore",
method="GET",
path="/stores/{store_id}",
)
sdk = await fga_client.get_store()
body = raw.json()
assert raw.status == 200
assert body["id"] == sdk.id
assert body["name"] == sdk.name
print(f" ✅ id={body['id']}, name={body['name']}")
# ── 3. GET /stores/{store_id}/authorization-models ────────
print(
"3. ReadAuthorizationModels (GET /stores/{store_id}/authorization-models)"
)
raw = await fga_client.execute_api_request(
operation_name="ReadAuthorizationModels",
method="GET",
path="/stores/{store_id}/authorization-models",
)
sdk = await fga_client.read_authorization_models()
body = raw.json()
assert raw.status == 200
assert len(body["authorization_models"]) == len(sdk.authorization_models)
print(f" ✅ {len(body['authorization_models'])} models")
# ── 4. POST /stores/{store_id}/check ──────────────────────
print("4. Check (POST /stores/{store_id}/check)")
raw = await fga_client.execute_api_request(
operation_name="Check",
method="POST",
path="/stores/{store_id}/check",
body={
"tuple_key": {
"user": "user:anne",
"relation": "viewer",
"object": "document:roadmap",
},
"authorization_model_id": auth_model_id,
},
)
sdk = await fga_client.check(
ClientCheckRequest(
user="user:anne",
relation="viewer",
object="document:roadmap",
)
)
body = raw.json()
assert raw.status == 200
assert body["allowed"] == sdk.allowed
print(f" ✅ allowed={body['allowed']}")
# ── 5. POST /stores/{store_id}/read ───────────────────────
print("5. Read (POST /stores/{store_id}/read)")
raw = await fga_client.execute_api_request(
operation_name="Read",
method="POST",
path="/stores/{store_id}/read",
body={
"tuple_key": {
"user": "user:anne",
"object": "document:",
},
},
)
body = raw.json()
assert raw.status == 200
assert "tuples" in body
assert len(body["tuples"]) >= 1
print(f" ✅ {len(body['tuples'])} tuples returned")
# ── 6. POST /stores — create store via raw request ────────
print("6. CreateStore (POST /stores)")
raw = await fga_client.execute_api_request(
operation_name="CreateStore",
method="POST",
path="/stores",
body={"name": "raw_request_test_store"},
)
body = raw.json()
assert raw.status == 201, f"Expected 201, got {raw.status}"
assert "id" in body
new_store_id = body["id"]
print(f" ✅ created store: {new_store_id}")
# ── 7. DELETE /stores/{store_id} — clean up ───────────────
print("7. DeleteStore (DELETE /stores/{store_id})")
raw = await fga_client.execute_api_request(
operation_name="DeleteStore",
method="DELETE",
path="/stores/{store_id}",
path_params={"store_id": new_store_id},
)
assert raw.status == 204, f"Expected 204, got {raw.status}"
print(f" ✅ deleted store: {new_store_id} (status 204 No Content)")
# ── 8. Custom headers ─────────────────────────────────────
print("8. Custom headers (GET /stores/{store_id})")
raw = await fga_client.execute_api_request(
operation_name="GetStoreWithHeaders",
method="GET",
path="/stores/{store_id}",
headers={"X-Custom-Header": "test-value"},
)
assert raw.status == 200
print(f" ✅ custom headers accepted (status {raw.status})")
# ── 9. Explicit path_params override for store_id ─────────
print("9. Explicit store_id in path_params")
raw = await fga_client.execute_api_request(
operation_name="GetStore",
method="GET",
path="/stores/{store_id}",
path_params={"store_id": store.id},
)
body = raw.json()
assert raw.status == 200
assert body["id"] == store.id
print(f" ✅ explicit store_id matched: {body['id']}")
# ─── Cleanup ─────────────────────────────────────────────
print("\n=== Cleanup ===")
await fga_client.delete_store()
print(f"Deleted test store: {store.id}")
print("\n All execute_api_request integration tests passed!\n")
asyncio.run(main())