11import logging
22from enum import Enum
3- from typing import List , Optional , Union
3+ from typing import Iterable , List
44
55from labelbox .orm import query
66from labelbox .orm .db_object import DbObject , Updateable
77from labelbox .orm .model import Entity , Field , Relationship
8- from labelbox import Client , Project
98
109logger = logging .getLogger (__name__ )
1110
@@ -25,24 +24,24 @@ class Webhook(DbObject, Updateable):
2524
2625 """
2726
28- class WebhookStatus (Enum ):
27+ class Status (Enum ):
2928 ACTIVE = "ACTIVE"
3029 INACTIVE = "INACTIVE"
3130 REVOKED = "REVOKED"
3231
33- class WebhookTopic (Enum ):
32+ class Topic (Enum ):
3433 LABEL_CREATED = "LABEL_CREATED"
3534 LABEL_UPDATED = "LABEL_UPDATED"
3635 LABEL_DELETED = "LABEL_DELETED"
3736 REVIEW_CREATED = "REVIEW_CREATED"
3837 REVIEW_UPDATED = "REVIEW_UPDATED"
3938 REVIEW_DELETED = "REVIEW_DELETED"
4039
41- #For backwards compatibility
42- for topic in WebhookStatus :
40+ # For backwards compatibility
41+ for topic in Status :
4342 vars ()[topic .name ] = topic .value
4443
45- for status in WebhookTopic :
44+ for status in Topic :
4645 vars ()[status .name ] = status .value
4746
4847 updated_at = Field .DateTime ("updated_at" )
@@ -56,15 +55,14 @@ class WebhookTopic(Enum):
5655 project = Relationship .ToOne ("Project" )
5756
5857 @staticmethod
59- def create (client : Client , topics : List [WebhookTopic ], url : str ,
60- secret : str , project : Project ):
58+ def create (client , topics , url , secret , project ):
6159 """ Creates a Webhook.
6260
6361 Args:
6462 client (Client): The Labelbox client used to connect
6563 to the server.
6664 topics (list of str): A list of topics this Webhook should
67- get notifications for. Must be one of Webhook.WebhookTopic
65+ get notifications for. Must be one of Webhook.Topic
6866 url (str): The URL to which notifications should be sent
6967 by the Labelbox server.
7068 secret (str): A secret key used for signing notifications.
@@ -75,7 +73,7 @@ def create(client: Client, topics: List[WebhookTopic], url: str,
7573 A newly created Webhook.
7674
7775 Raises:
78- ValueError: If the topic is not one of WebhookTopic or status is not one of WebhookStatus
76+ ValueError: If the topic is not one of Topic or status is not one of Status
7977
8078 Information on configuring your server can be found here (this is where the url points to and the secret is set).
8179 https://docs.labelbox.com/en/configure-editor/webhooks-setup#setup-steps
@@ -94,17 +92,17 @@ def create(client: Client, topics: List[WebhookTopic], url: str,
9492 return Webhook (client , client .execute (query_str )["createWebhook" ])
9593
9694 @staticmethod
97- def validate_topics (topics : List [ WebhookTopic ] ):
98- if not isinstance (topics , list ):
95+ def validate_topics (topics ):
96+ if isinstance ( topics , str ) or not isinstance (topics , Iterable ):
9997 raise TypeError (
100- f"Topics must be List[Webhook.WebhookTopic ]. Found `{ topics } `" )
98+ f"Topics must be List[Webhook.Topic ]. Found `{ topics } `" )
10199
102100 for topic in topics :
103- Webhook .validate_value (topic , Webhook .WebhookTopic )
101+ Webhook .validate_value (topic , Webhook .Topic )
104102
105103 @staticmethod
106- def validate_value (value : str , enum : Union [ WebhookStatus , WebhookTopic ] ):
107- supported_values = [ x .value for x in enum ]
104+ def validate_value (value , enum ):
105+ supported_values = { item .value for item in enum }
108106 if value not in supported_values :
109107 raise ValueError (
110108 f"Value `{ value } ` does not exist in supported values. Expected one of { supported_values } "
@@ -114,18 +112,15 @@ def delete(self):
114112 """
115113 Deletes the webhook
116114 """
117- self .update (status = self .WebhookStatus .INACTIVE )
115+ self .update (status = self .Status .INACTIVE )
118116
119- def update (self ,
120- topics : Optional [List [WebhookTopic ]] = None ,
121- url : Optional [str ] = None ,
122- status : Optional [WebhookStatus ] = None ):
117+ def update (self , topics = None , url = None , status = None ):
123118 """ Updates the Webhook.
124119
125120 Args:
126- topics (Optional[List[WebhookTopic ]]): The new topics.
121+ topics (Optional[List[Topic ]]): The new topics.
127122 url Optional[str): The new URL value.
128- status (Optional[WebhookStatus ]): The new status.
123+ status (Optional[Status ]): The new status.
129124
130125 If values are set to None then there are no updates made to that field.
131126
@@ -138,7 +133,7 @@ def update(self,
138133 self .validate_topics (topics )
139134
140135 if status is not None :
141- self .validate_value (status , self .WebhookStatus )
136+ self .validate_value (status , self .Status )
142137
143138 topics_str = "" if topics is None \
144139 else "topics: {set: [%s]}" % " " .join (topics )
0 commit comments