Skip to content

Commit

Permalink
Convert readonly properties to be
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Dec 7, 2023
1 parent cdc23a6 commit 576da6b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
14 changes: 13 additions & 1 deletion authress/models/access_record.py
Expand Up @@ -21,6 +21,8 @@
from datetime import datetime
from typing import Dict, List, Optional, Union
from pydantic import BaseModel, Field, StrictStr, confloat, conint, conlist, constr, validator
from authress.models.access_record_account import AccessRecordAccount
from authress.models.account_links import AccountLinks
from authress.models.linked_group import LinkedGroup
from authress.models.statement import Statement
from authress.models.user import User
Expand All @@ -35,12 +37,14 @@ class AccessRecord(BaseModel):
capacity: Optional[Union[confloat(le=1, ge=0, strict=True), conint(le=1, ge=0, strict=True)]] = Field(None, description="Percentage capacity of record that is filled.")
last_updated: Optional[datetime] = Field(None, alias="lastUpdated", description="The expected last time the record was updated")
status: Optional[StrictStr] = Field(None, description="Current status of the access record.")
account: Optional[AccessRecordAccount] = Field(...)
users: Optional[conlist(User)] = Field(None, description="The list of users this record applies to")
admins: Optional[conlist(User)] = Field(None, description="The list of admin that can edit this record even if they do not have global record edit permissions.")
groups: Optional[conlist(LinkedGroup)] = Field(None, description="The list of groups this record applies to. Users in these groups will be receive access to the resources listed.")
statements: conlist(Statement, min_items=1) = Field(..., description="A list of statements which match roles to resources.")
links: Optional[AccountLinks] = Field(...)
tags: Optional[Dict[str, constr(strict=True, max_length=128)]] = Field(None, description="The tags associated with this resource, this property is an map. { key1: value1, key2: value2 }")
__properties = ["recordId", "name", "description", "capacity", "lastUpdated", "status", "users", "admins", "groups", "statements", "tags"]
__properties = ["recordId", "name", "description", "capacity", "lastUpdated", "status", "account", "users", "admins", "groups", "statements", "links", "tags"]

@validator('record_id')
def record_id_validate_regular_expression(cls, value):
Expand Down Expand Up @@ -89,6 +93,9 @@ def to_dict(self):
"status",
},
exclude_none=True)
# override the default output from pydantic by calling `to_dict()` of account
if self.account:
_dict['account'] = self.account.to_dict()
# override the default output from pydantic by calling `to_dict()` of each item in users (list)
_items = []
if self.users:
Expand Down Expand Up @@ -117,6 +124,9 @@ def to_dict(self):
if _item:
_items.append(_item.to_dict())
_dict['statements'] = _items
# override the default output from pydantic by calling `to_dict()` of links
if self.links:
_dict['links'] = self.links.to_dict()
# set to None if description (nullable) is None
# and __fields_set__ contains the field
if self.description is None and "description" in self.__fields_set__:
Expand Down Expand Up @@ -160,10 +170,12 @@ def from_dict(cls, obj: dict) -> AccessRecord:
"capacity": obj.get("capacity"),
"last_updated": obj.get("lastUpdated"),
"status": obj.get("status"),
"account": AccessRecordAccount.from_dict(obj.get("account")) if obj.get("account") is not None else None,
"users": [User.from_dict(_item) for _item in obj.get("users")] if obj.get("users") is not None else None,
"admins": [User.from_dict(_item) for _item in obj.get("admins")] if obj.get("admins") is not None else None,
"groups": [LinkedGroup.from_dict(_item) for _item in obj.get("groups")] if obj.get("groups") is not None else None,
"statements": [Statement.from_dict(_item) for _item in obj.get("statements")] if obj.get("statements") is not None else None,
"links": AccountLinks.from_dict(obj.get("links")) if obj.get("links") is not None else None,
"tags": obj.get("tags")
})
return _obj
Expand Down
8 changes: 7 additions & 1 deletion authress/models/access_request.py
Expand Up @@ -22,6 +22,7 @@
from typing import Dict, Optional
from pydantic import BaseModel, Field, StrictStr, constr, validator
from authress.models.access_template import AccessTemplate
from authress.models.account_links import AccountLinks

class AccessRequest(BaseModel):
"""
Expand All @@ -31,8 +32,9 @@ class AccessRequest(BaseModel):
last_updated: Optional[datetime] = Field(None, alias="lastUpdated", description="The expected last time the request was updated")
status: Optional[StrictStr] = Field(None, description="Current status of the access request.")
access: AccessTemplate = Field(...)
links: Optional[AccountLinks] = Field(...)
tags: Optional[Dict[str, constr(strict=True, max_length=128)]] = Field(None, description="The tags associated with this resource, this property is an map. { key1: value1, key2: value2 }")
__properties = ["requestId", "lastUpdated", "status", "access", "tags"]
__properties = ["requestId", "lastUpdated", "status", "access", "links", "tags"]

@validator('request_id')
def request_id_validate_regular_expression(cls, value):
Expand Down Expand Up @@ -81,6 +83,9 @@ def to_dict(self):
# override the default output from pydantic by calling `to_dict()` of access
if self.access:
_dict['access'] = self.access.to_dict()
# override the default output from pydantic by calling `to_dict()` of links
if self.links:
_dict['links'] = self.links.to_dict()
# set to None if tags (nullable) is None
# and __fields_set__ contains the field
if self.tags is None and "tags" in self.__fields_set__:
Expand All @@ -102,6 +107,7 @@ def from_dict(cls, obj: dict) -> AccessRequest:
"last_updated": obj.get("lastUpdated"),
"status": obj.get("status"),
"access": AccessTemplate.from_dict(obj.get("access")) if obj.get("access") is not None else None,
"links": AccountLinks.from_dict(obj.get("links")) if obj.get("links") is not None else None,
"tags": obj.get("tags")
})
return _obj
Expand Down
11 changes: 8 additions & 3 deletions authress/models/account.py
Expand Up @@ -21,6 +21,7 @@
from datetime import datetime
from typing import Any, Dict, Optional
from pydantic import BaseModel, Field, constr
from authress.models.account_links import AccountLinks

class Account(BaseModel):
"""
Expand All @@ -30,7 +31,8 @@ class Account(BaseModel):
created_time: datetime = Field(..., alias="createdTime")
name: Optional[constr(strict=True, max_length=32)] = None
company: Dict[str, Any] = Field(...)
__properties = ["accountId", "createdTime", "name", "company"]
links: Optional[AccountLinks] = Field(...)
__properties = ["accountId", "createdTime", "name", "company", "links"]

class Config:
"""Pydantic configuration"""
Expand All @@ -57,7 +59,9 @@ def to_dict(self):
"created_time",
},
exclude_none=True)

# override the default output from pydantic by calling `to_dict()` of links
if self.links:
_dict['links'] = self.links.to_dict()
# set to None if name (nullable) is None
# and __fields_set__ contains the field
if self.name is None and "name" in self.__fields_set__:
Expand All @@ -78,7 +82,8 @@ def from_dict(cls, obj: dict) -> Account:
"account_id": obj.get("accountId"),
"created_time": obj.get("createdTime"),
"name": obj.get("name"),
"company": obj.get("company")
"company": obj.get("company"),
"links": AccountLinks.from_dict(obj.get("links")) if obj.get("links") is not None else None
})
return _obj

Expand Down
4 changes: 2 additions & 2 deletions authress/models/extension.py
Expand Up @@ -30,9 +30,9 @@ class Extension(BaseModel):
"""
extension_id: constr(strict=True, max_length=64, min_length=1) = Field(..., alias="extensionId")
name: Optional[constr(strict=True, max_length=128, min_length=1)] = Field(None, description="The name of the extension. This name is visible in the Authress management portal")
created_time: Optional[datetime] = Field(..., alias="createdTime")
created_time: datetime = Field(..., alias="createdTime")
application: Optional[ExtensionApplication] = None
client: Optional[ExtensionClient] = Field(...)
client: ExtensionClient = Field(...)
tags: Optional[Dict[str, constr(strict=True, max_length=128)]] = Field(None, description="The tags associated with this resource, this property is an map. { key1: value1, key2: value2 }")
__properties = ["extensionId", "name", "createdTime", "application", "client", "tags"]

Expand Down
8 changes: 7 additions & 1 deletion authress/models/group.py
Expand Up @@ -21,6 +21,7 @@
from datetime import datetime
from typing import Dict, List, Optional
from pydantic import BaseModel, Field, conlist, constr, validator
from authress.models.account_links import AccountLinks
from authress.models.user import User

class Group(BaseModel):
Expand All @@ -32,8 +33,9 @@ class Group(BaseModel):
last_updated: Optional[datetime] = Field(None, alias="lastUpdated", description="The expected last time the group was updated")
users: conlist(User) = Field(..., description="The list of users in this group")
admins: conlist(User) = Field(..., description="The list of admins that can edit this record even if they do not have global record edit permissions.")
links: Optional[AccountLinks] = Field(...)
tags: Optional[Dict[str, constr(strict=True, max_length=128)]] = Field(None, description="The tags associated with this resource, this property is an map. { key1: value1, key2: value2 }")
__properties = ["groupId", "name", "lastUpdated", "users", "admins", "tags"]
__properties = ["groupId", "name", "lastUpdated", "users", "admins", "links", "tags"]

@validator('group_id')
def group_id_validate_regular_expression(cls, value):
Expand Down Expand Up @@ -84,6 +86,9 @@ def to_dict(self):
if _item:
_items.append(_item.to_dict())
_dict['admins'] = _items
# override the default output from pydantic by calling `to_dict()` of links
if self.links:
_dict['links'] = self.links.to_dict()
# set to None if tags (nullable) is None
# and __fields_set__ contains the field
if self.tags is None and "tags" in self.__fields_set__:
Expand All @@ -106,6 +111,7 @@ def from_dict(cls, obj: dict) -> Group:
"last_updated": obj.get("lastUpdated"),
"users": [User.from_dict(_item) for _item in obj.get("users")] if obj.get("users") is not None else None,
"admins": [User.from_dict(_item) for _item in obj.get("admins")] if obj.get("admins") is not None else None,
"links": AccountLinks.from_dict(obj.get("links")) if obj.get("links") is not None else None,
"tags": obj.get("tags")
})
return _obj
Expand Down
8 changes: 7 additions & 1 deletion authress/models/invite.py
Expand Up @@ -21,6 +21,7 @@

from typing import List, Optional
from pydantic import BaseModel, Field, StrictStr, conlist, constr
from authress.models.account_links import AccountLinks
from authress.models.statement import Statement

class Invite(BaseModel):
Expand All @@ -30,7 +31,8 @@ class Invite(BaseModel):
invite_id: StrictStr = Field(..., alias="inviteId", description="The unique identifier for the invite. Use this ID to accept the invite. This parameter is ignored during invite creation.")
tenant_id: Optional[constr(strict=True, max_length=64, min_length=0)] = Field(None, alias="tenantId")
statements: conlist(Statement) = Field(..., description="A list of statements which match roles to resources. The invited user will all statements apply to them when the invite is accepted.")
__properties = ["inviteId", "tenantId", "statements"]
links: Optional[AccountLinks] = Field(...)
__properties = ["inviteId", "tenantId", "statements", "links"]

class Config:
"""Pydantic configuration"""
Expand Down Expand Up @@ -67,6 +69,9 @@ def to_dict(self):
if _item:
_items.append(_item.to_dict())
_dict['statements'] = _items
# override the default output from pydantic by calling `to_dict()` of links
if self.links:
_dict['links'] = self.links.to_dict()
return _dict

@classmethod
Expand All @@ -82,6 +87,7 @@ def from_dict(cls, obj: dict) -> Invite:
"invite_id": obj.get("inviteId"),
"tenant_id": obj.get("tenantId"),
"statements": [Statement.from_dict(_item) for _item in obj.get("statements")] if obj.get("statements") is not None else None,
"links": AccountLinks.from_dict(obj.get("links")) if obj.get("links") is not None else None
})
return _obj

Expand Down
5 changes: 1 addition & 4 deletions docs/EXAMPLES.md
Expand Up @@ -22,12 +22,9 @@ def get_resource(resourceId):
authorization_token = request.headers.get('authorization')
authress_client.set_token(authorization_token)

# Get the user
user_id = get_user_id(request)

# Check Authress to authorize the user
try
authress_client.user_permissions.authorize_user(user_id, f'resources/{resourceId}', 'READ')
authress_client.user_permissions.authorize_user(None, f'resources/{resourceId}', 'READ')
except ApiException as api_exception:
# Will throw except if the user is not authorized to read the resource
if api_exception.status is 403:
Expand Down

0 comments on commit 576da6b

Please sign in to comment.