Skip to content

Commit

Permalink
List Users
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 committed Aug 17, 2023
1 parent eebff30 commit 2c05622
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
78 changes: 78 additions & 0 deletions workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from workos.resources.users import (
WorkOSUser,
)
from workos.utils.pagination_order import Order
from workos.utils.request import (
RequestHelper,
REQUEST_METHOD_POST,
REQUEST_METHOD_GET,
)
from workos.utils.user_types import UserType
from workos.utils.validation import validate_settings, USERS_MODULE

USER_PATH = "users"
Expand Down Expand Up @@ -72,3 +74,79 @@ def get_user(self, user):
)

return WorkOSUser.construct_from_response(response).to_dict()

def list_users(self,
type=None,
email=None,
organization=None,
limit=None,
before=None,
after=None,
order=None,
):
"""Get a list of all of your existing users matching the criteria specified.
Kwargs:
type (str): Filter Users by their type: "unmanged" or "managed" (Optional)
email (str): Filter Users by their email. (Optional)
organization (list): Filter Users by the organization they are members of. (Optional)
limit (int): Maximum number of records to return. (Optional)
before (str): Pagination cursor to receive records before a provided User ID. (Optional)
after (str): Pagination cursor to receive records after a provided User ID. (Optional)
order (Order): Sort records in either ascending or descending order by created_at timestamp: "asc" or "desc" (Optional)
Returns:
dict: Users response from WorkOS.
"""

if limit is None:
limit = RESPONSE_LIMIT
default_limit = True

params = {
"email": email,
"organization": organization,
"limit": limit,
"before": before,
"after": after,
}

if type is not None:
if isinstance(type, UserType):
params["type"] = str(type.value)
elif type == "unmanaged" or type == "managed":
params["type"] = type
else:
raise ValueError("Parameter type must be either 'unmanaged' or 'managed'")

if order is not None:
if isinstance(order, Order):
params["order"] = str(order.value)
elif order == "asc" or order == "desc":
params["order"] = order
else:
raise ValueError("Parameter order must be of enum type Order")

response = self.request_helper.request(
USER_PATH,
method=REQUEST_METHOD_GET,
params=params,
token=workos.api_key,
)

dict_response = response.to_dict()

dict_response["metadata"] = {
"params": params,
"method": Organizations.list_organizations_v2,
}

if "default_limit" in locals():
if "metadata" in dict_response and "params" in dict_response["metadata"]:
dict_response["metadata"]["params"]["default_limit"] = default_limit
else:
dict_response["metadata"] = {"params": {"default_limit": default_limit}}

return self.construct_from_response(dict_response)
6 changes: 6 additions & 0 deletions workos/utils/user_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class UserType(Enum):
Unmanaged = "unmanaged"
Managed = "managed"

0 comments on commit 2c05622

Please sign in to comment.