Skip to content

Commit

Permalink
feat(python-sdk): add condition support (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 9, 2024
2 parents 4a8af1b + bb574bb commit de95f50
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:

- name: Run All Tests
run: |-
make test-client-python OPEN_API_REF=0f1d73e766d26ac5c004383d741ee0f815c9b1e6 # TODO: Remove OPEN_API_REF after support for conditions
make test-client-python
- name: Check for SDK changes
run: ./scripts/commit_push_changes.sh
Expand Down
9 changes: 9 additions & 0 deletions config/clients/go/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v0.3.4

### [0.3.4](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.3.3...v0.3.4) (2023-12-21)

- feat: support for conditions
- chore: use latest API interfaces for type info
- chore: add [example project](./example)
- chore: dependency updates

## v0.3.3

### [0.3.3](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.3.2...v0.3.3) (2023-12-21)
Expand Down
8 changes: 2 additions & 6 deletions config/clients/python/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"sdkId": "python",
"gitRepoId": "python-sdk",
"packageName": "openfga_sdk",
"packageVersion": "0.3.3",
"packageVersion": "0.3.4",
"packageDescription": "Python SDK for OpenFGA",
"packageDetailedDescription": "This is an autogenerated python SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).",
"fossaComplianceNoticeId": "2f8a8629-b46c-435e-b8cd-1174a674fb4b",
Expand Down Expand Up @@ -158,15 +158,11 @@
"destinationFilename": "example/README.md",
"templateType": "SupportingFiles"
},
"example/example1/auth-model.json": {
"destinationFilename": "example/example1/auth-model.json",
"templateType": "SupportingFiles"
},
"example/example1/example1.py": {
"destinationFilename": "example/example1/example1.py",
"templateType": "SupportingFiles"
},
"example/example1/requirements.txt": {
"example/example1/requirements.txt.mustache": {
"destinationFilename": "example/example1/requirements.txt",
"templateType": "SupportingFiles"
},
Expand Down
93 changes: 77 additions & 16 deletions config/clients/python/template/README_calling_api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ Create a new authorization model.

```python
body = WriteAuthorizationModelRequest(
schema_version = "1.1",
schema_version="1.1",
type_definitions=[
TypeDefinition(
type="user",
type="user"
),
TypeDefinition(
type="document",
Expand All @@ -107,9 +107,41 @@ body = WriteAuthorizationModelRequest(
],
),
),
),
metadata=Metadata(
relations=dict(
writer=RelationMetadata(
directly_related_user_types=[
RelationReference(type="user"),
RelationReference(type="user", condition="ViewCountLessThan200"),
]
),
viewer=RelationMetadata(
directly_related_user_types=[
RelationReference(type="user"),
]
)
)
)
),
)
],
conditions=dict(
ViewCountLessThan200=Condition(
name="ViewCountLessThan200",
expression="ViewCount < 200",
parameters=dict(
ViewCount=ConditionParamTypeRef(
type_name="TYPE_NAME_INT"
),
Type=ConditionParamTypeRef(
type_name="TYPE_NAME_STRING"
),
Name=ConditionParamTypeRef(
type_name="TYPE_NAME_STRING"
),
)
)
)
)

response = await fga_client.write_authorization_model(body)
Expand All @@ -129,7 +161,7 @@ options = {
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"
}

response = await fga_client.read_authorization_model(id)
response = await fga_client.read_authorization_model(options)
# response.authorization_model = AuthorizationModel(id='01GXSA8YR785C4FYS3C0RTG7B1', schema_version = '1.1', type_definitions=type_definitions[...])
```

Expand Down Expand Up @@ -159,7 +191,7 @@ options = {
"page_size": "25",
"continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="
}
body = ClientReadChangesRequest("document")
body = ClientReadChangesRequest(type="document")

response = await fga_client.read_changes(body, options)
# response.continuation_token = ...
Expand All @@ -174,7 +206,7 @@ Reads the relationship tuples stored in the database. It does not evaluate nor e

```python
# Find if a relationship tuple stating that a certain user is a viewer of certain document
body = TupleKey(
body = ReadRequestTupleKey(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:roadmap",
Expand All @@ -186,7 +218,7 @@ response = await fga_client.read(body)

```python
# Find all relationship tuples where a certain user has a relationship as any relation to a certain document
body = TupleKey(
body = ReadRequestTupleKey(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
object="document:roadmap",
)
Expand All @@ -198,7 +230,7 @@ response = await fga_client.read(body)

```python
# Find all relationship tuples where a certain user is a viewer of any document
body = TupleKey(
body = ReadRequestTupleKey(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:",
Expand All @@ -210,7 +242,7 @@ response = await fga_client.read(body)

```python
# Find all relationship tuples where any user has a relationship as any relation with a particular document
body = TupleKey(
body = ReadRequestTupleKey(
object="document:roadmap",
)

Expand All @@ -220,7 +252,7 @@ response = await fga_client.read(body)

```python
# Read all stored relationship tuples
body = TupleKey()
body = ReadRequestTupleKey()

response = await api_instance.read(body)
# response = ReadResponse({"tuples": [Tuple({"key": TupleKey({"user":"...","relation":"...","object":"..."}), "timestamp": datetime.fromisoformat("...") })]})
Expand All @@ -247,6 +279,13 @@ body = ClientWriteRequest(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:roadmap",
condition=RelationshipCondition(
name='ViewCountLessThan200',
context=dict(
Name='Roadmap',
Type='Document',
),
),
),
ClientTuple(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Expand Down Expand Up @@ -293,6 +332,13 @@ body = ClientWriteRequest(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="viewer",
object="document:budget",
condition=RelationshipCondition(
name='ViewCountLessThan200',
context=dict(
Name='Roadmap',
Type='Document',
),
),
),
],
deletes=[
Expand Down Expand Up @@ -324,6 +370,9 @@ body = ClientCheckRequest(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="writer",
object="document:roadmap",
context=dict(
ViewCount=100
),
)

response = await fga_client.check(body, options)
Expand Down Expand Up @@ -351,7 +400,10 @@ body = [ClientCheckRequest(
relation="editor",
object="document:roadmap",
),
]
],
context=dict(
ViewCount=100
)
), ClientCheckRequest(
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
relation="admin",
Expand Down Expand Up @@ -384,7 +436,10 @@ response = await fga_client.batch_check(body, options)
# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
# relation: "editor",
# object: "document:roadmap"
# }]
# }],
# context=dict(
# ViewCount=100
# )
# }
# }, {
# allowed: false,
Expand Down Expand Up @@ -458,7 +513,10 @@ body = ClientListObjectsRequest(
relation="writer",
object="document:budget",
),
]
],
context=dict(
ViewCount=100
)
)

response = await fga_client.list_objects(body)
Expand All @@ -484,7 +542,10 @@ body = ClientListRelationsRequest(
relation="writer",
object="document:budget",
),
]
],
context=dict(
ViewCount=100
)
)
var response = await fga_client.list_relations(body, options);

Expand All @@ -499,7 +560,7 @@ Read assertions for a particular authorization model.

[API Documentation]({{apiDocsUrl}}#/Assertions/Read%20Assertions)

```csharp
```python
options = {
# You can rely on the model id set in the configuration or override it for this specific request
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"
Expand All @@ -513,7 +574,7 @@ Update the assertions for a particular authorization model.

[API Documentation]({{apiDocsUrl}}#/Assertions/Write%20Assertions)

```csharp
```python
options = {
# You can rely on the model id set in the configuration or override it for this specific request
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"
Expand Down
Loading

0 comments on commit de95f50

Please sign in to comment.