Skip to content

Commit eac1c8d

Browse files
author
Matt Sokoloff
committed
beta wip
1 parent 10cf8de commit eac1c8d

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

labelbox/client.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class Client:
3737

3838
def __init__(self,
3939
api_key=None,
40-
endpoint='https://api.labelbox.com/graphql'):
40+
endpoint='https://api.labelbox.com/graphql',
41+
enable_beta = False):
4142
""" Creates and initializes a Labelbox Client.
4243
4344
Logging is defaulted to level WARNING. To receive more verbose
@@ -50,6 +51,7 @@ def __init__(self,
5051
Args:
5152
api_key (str): API key. If None, the key is obtained from the "LABELBOX_API_KEY" environment variable.
5253
endpoint (str): URL of the Labelbox server to connect to.
54+
enable_beta (bool): Indicated whether or not to use beta features
5355
Raises:
5456
labelbox.exceptions.AuthenticationError: If no `api_key`
5557
is provided as an argument or via the environment
@@ -61,6 +63,10 @@ def __init__(self,
6163
"Labelbox API key not provided")
6264
api_key = os.environ[_LABELBOX_API_KEY]
6365
self.api_key = api_key
66+
self.enable_beta = enable_beta
67+
68+
if enable_beta:
69+
logger.info("Beta features have been enabled")
6470

6571
logger.info("Initializing Labelbox client at '%s'", endpoint)
6672

@@ -74,7 +80,7 @@ def __init__(self,
7480

7581
@retry.Retry(predicate=retry.if_exception_type(
7682
labelbox.exceptions.InternalServerError))
77-
def execute(self, query, params=None, timeout=30.0, experimental=False):
83+
def execute(self, query, params=None, timeout=30.0):
7884
""" Sends a request to the server for the execution of the
7985
given query.
8086
@@ -86,8 +92,6 @@ def execute(self, query, params=None, timeout=30.0, experimental=False):
8692
params (dict): Query parameters referenced within the query.
8793
timeout (float): Max allowed time for query execution,
8894
in seconds.
89-
experimental (bool): Enables users to query experimental features.
90-
Users should be aware that these features could change slightly over time.
9195
Returns:
9296
dict, parsed JSON response.
9397
Raises:
@@ -122,15 +126,7 @@ def convert_value(value):
122126
data = json.dumps({'query': query, 'variables': params}).encode('utf-8')
123127

124128
try:
125-
endpoint = self.endpoint
126-
127-
if experimental:
128-
endpoint = self.endpoint.replace('graphql', '_gql')
129-
logger.info(
130-
"Experimental queries/mutations aren't guarenteed to maintain their interface."
131-
)
132-
133-
response = requests.post(endpoint,
129+
response = requests.post(self.endpoint,
134130
data=data,
135131
headers=self.headers,
136132
timeout=timeout)

labelbox/orm/db_object.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,20 @@ def delete(self):
252252
a call to this you should not use this DB object anymore.
253253
"""
254254
type(self).bulk_delete([self])
255+
256+
257+
258+
def beta(fn):
259+
def wrapper(self, *args, **kwargs):
260+
if not isinstance(self, DbObject):
261+
raise TypeError("Cannot decorate functions that are not functions of `DbOjects` with `beta` decorator")
262+
if not self.client.enable_beta:
263+
logger.warning(
264+
f"This function {fn.__name__} relies on a beta feature in the api. This means that the interface could change."
265+
" Set `enable_beta=True` in the client to silence this warning.")
266+
self.client.endpoint = self.client.endpoint.replace("/graphql", "/_gql")
267+
result = fn(self, *args, **kwargs)
268+
self.client.endpoint = self.client.endpoint.replace("/_gql", "/graphql")
269+
return result
270+
return wrapper
271+

labelbox/pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self,
1818
dereferencing,
1919
obj_class,
2020
cursor_path=None,
21-
experimental=False):
21+
beta=False):
2222
""" Creates a PaginatedCollection.
2323
2424
Args:
@@ -40,7 +40,7 @@ def __init__(self,
4040
self.params = params
4141
self.dereferencing = dereferencing
4242
self.obj_class = obj_class
43-
self.experimental = experimental
43+
self.beta = beta
4444

4545
self._fetched_all = False
4646
self._data = []
@@ -66,7 +66,7 @@ def __next__(self):
6666
raise StopIteration()
6767

6868
results = self.paginator.fetch_results(self.query, self.params,
69-
self.experimental)
69+
self.beta)
7070
page_data = self.get_page_data(results)
7171
self._data.extend(page_data)
7272
n_items = len(page_data)

labelbox/schema/invite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional
33
from datetime import datetime
44

5-
from labelbox.orm.db_object import DbObject
5+
from labelbox.orm.db_object import DbObject, beta
66
from labelbox.orm.model import Field
77
from labelbox.schema.role import ProjectRole
88

@@ -50,6 +50,7 @@ def __init__(self, client, invite_response):
5050
for r in project_roles
5151
]
5252

53+
@beta
5354
def revoke(self):
5455
""" Makes the invitation invalid.
5556
"""

labelbox/schema/organization.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from labelbox.exceptions import LabelboxError
44
from labelbox import utils
55
from labelbox.pagination import PaginatedCollection
6-
from labelbox.orm.db_object import DbObject
6+
from labelbox.orm.db_object import DbObject, beta
77
from labelbox.orm.model import Field, Relationship
88
from labelbox.schema.invite import Invite, InviteLimit, UserLimit, ProjectRole
99
from labelbox.schema.user import User
1010
from labelbox.schema.role import Role
11+
import logging
1112

13+
logger = logging.getLogger(__name__)
1214

1315
class Organization(DbObject):
1416
""" An Organization is a group of Users.
@@ -43,6 +45,7 @@ def __init__(self, *args, **kwargs):
4345
projects = Relationship.ToMany("Project", True)
4446
webhooks = Relationship.ToMany("Webhook", False)
4547

48+
@beta
4649
def invites(self) -> PaginatedCollection:
4750
""" List all current invitees
4851
@@ -97,6 +100,7 @@ def _assign_user_role(self, email: str, role: Role,
97100
invite_info = res['createInvites'][0]['invite']
98101
return invite_info
99102

103+
@beta
100104
def invite_user(
101105
self,
102106
email: str,
@@ -144,6 +148,7 @@ def invite_user(
144148
invite_response = self._assign_user_role(email, role, _project_roles)
145149
return Invite(self.client, invite_response)
146150

151+
@beta
147152
def user_limit(self) -> UserLimit:
148153
""" Retrieve user limits for the org
149154
@@ -161,6 +166,7 @@ def user_limit(self) -> UserLimit:
161166
['account']['usersLimit'].items()
162167
})
163168

169+
@beta
164170
def invite_limit(self) -> InviteLimit:
165171
""" Retrieve invite limits for the org
166172
This already accounts for users currently in the org
@@ -178,6 +184,7 @@ def invite_limit(self) -> InviteLimit:
178184
return InviteLimit(
179185
**{utils.snake_case(k): v for k, v in res['invitesLimit'].items()})
180186

187+
181188
def remove_user(self, user: User):
182189
"""
183190
Deletes a user from the organization. This cannot be undone without sending another invite.

labelbox/schema/project.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from labelbox.orm import query
1414
from labelbox.schema.bulk_import_request import BulkImportRequest
1515
from labelbox.exceptions import InvalidQueryError
16-
from labelbox.orm.db_object import DbObject, Updateable, Deletable
16+
from labelbox.orm.db_object import DbObject, Updateable, Deletable, beta
1717
from labelbox.orm.model import Entity, Field, Relationship
1818
from labelbox.pagination import PaginatedCollection
1919

@@ -79,6 +79,7 @@ class Project(DbObject, Updateable, Deletable):
7979
predictions = Relationship.ToMany("Prediction", False)
8080
ontology = Relationship.ToOne("Ontology", True)
8181

82+
@beta
8283
def invites(self):
8384
""" Fetch all current invites for this project
8485
@@ -97,8 +98,7 @@ def invites(self):
9798
self.client,
9899
query_str, {id_param: self.uid}, ['project', 'invites', 'nodes'],
99100
Invite,
100-
cursor_path=['project', 'invites', 'nextCursor'],
101-
experimental=True)
101+
cursor_path=['project', 'invites', 'nextCursor'])
102102

103103
def members(self):
104104
""" Fetch all current members for this project

labelbox/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#from labelbox.orm.db_object import DbObject
12
import re
23

34

@@ -23,3 +24,7 @@ def title_case(s):
2324
def snake_case(s):
2425
""" Converts a string in [snake|camel|title]case to snake_case. """
2526
return _convert(s, "_", lambda i: False)
27+
28+
29+
30+

0 commit comments

Comments
 (0)