Skip to content

Commit

Permalink
release(python): v0.3.4 with conditions support
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Jan 9, 2024
1 parent 14ef33d commit 1c53f6e
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 151 deletions.
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
4 changes: 2 additions & 2 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 @@ -166,7 +166,7 @@
"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
2 changes: 1 addition & 1 deletion config/clients/python/template/client/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ class OpenFgaClient():
options = set_heading_if_not_set(options, CLIENT_METHOD_HEADER, "ListRelations")
options = set_heading_if_not_set(options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4()))

request_body = [construct_check_request(user=body.user, relation=i, object=body.object, contextual_tuples=body.contextual_tuples) for i in body.relations]
request_body = [construct_check_request(user=body.user, relation=i, object=body.object, contextual_tuples=body.contextual_tuples, context=body.context) for i in body.relations]
result = {{#asyncio}}await {{/asyncio}}self.batch_check(request_body, options)
# need to filter with the allowed response
result_iterator = filter(_check_allowed, result)
Expand Down
2 changes: 1 addition & 1 deletion config/clients/python/template/client/client_sync.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class OpenFgaClient():
options = set_heading_if_not_set(options, CLIENT_METHOD_HEADER, "ListRelations")
options = set_heading_if_not_set(options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4()))

request_body = [construct_check_request(user=body.user, relation=i, object=body.object, contextual_tuples=body.contextual_tuples) for i in body.relations]
request_body = [construct_check_request(user=body.user, relation=i, object=body.object, contextual_tuples=body.contextual_tuples, context=body.context) for i in body.relations]
result = self.batch_check(request_body, options)
# need to filter with the allowed response
result_iterator = filter(_check_allowed, result)
Expand Down
34 changes: 17 additions & 17 deletions config/clients/python/template/client/models/check_request.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ from {{packageName}}.client.models.tuple import ClientTuple
from typing import List, Any


def construct_check_request(user: str, relation: str, object: str, context: Any = None, contextual_tuples: List[ClientTuple]=None):
def construct_check_request(user: str, relation: str, object: str, contextual_tuples: List[ClientTuple] = None, context: object = None):
"""
helper function to construct the check request body
"""
return ClientCheckRequest(user, relation, object, context, contextual_tuples)
return ClientCheckRequest(user, relation, object, contextual_tuples, context)


class ClientCheckRequest():
"""
ClientCheckRequest encapsulates the parameters for check request
"""

def __init__(self, user: str, relation: str, object: str, context: Any = None, contextual_tuples: List[ClientTuple]=None):
def __init__(self, user: str, relation: str, object: str, contextual_tuples: List[ClientTuple] = None, context: object = None):
self._user = user
self._relation = relation
self._object = object
self._context = context
self._contextual_tuples = None
if contextual_tuples:
self._contextual_tuples = contextual_tuples
self._context = context

@property
def user(self):
Expand All @@ -49,18 +49,18 @@ class ClientCheckRequest():
return self._object

@property
def context(self):
def contextual_tuples(self):
"""
Return context
Return contextual tuples
"""
return self._context
return self._contextual_tuples

@property
def contextual_tuples(self):
def context(self):
"""
Return contextual tuples
Return context
"""
return self._contextual_tuples
return self._context

@user.setter
def user(self, value):
Expand All @@ -83,16 +83,16 @@ class ClientCheckRequest():
"""
self._object = value

@context.setter
def context(self, value):
"""
Set context
"""
self._context = value

@contextual_tuples.setter
def contextual_tuples(self, value):
"""
Set contextual tuples
"""
self._contextual_tuples = value

@context.setter
def context(self, value):
"""
Set context
"""
self._context = value
Loading

0 comments on commit 1c53f6e

Please sign in to comment.