Skip to content

Commit 1cdff4a

Browse files
Erin LiuErin Liu
authored andcommitted
remove review_deleted
1 parent 62aff30 commit 1cdff4a

File tree

1 file changed

+153
-19
lines changed

1 file changed

+153
-19
lines changed

examples/project_configuration/webhooks.ipynb

Lines changed: 153 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
" 2. label_updated\n",
1919
" 3. label_deleted\n",
2020
" 4. review_created\n",
21-
" 5. review_updated\n",
22-
" 6. review_deleted"
21+
" 5. review_updated"
2322
]
2423
},
2524
{
@@ -28,47 +27,93 @@
2827
"id": "authentic-necessity",
2928
"metadata": {},
3029
"outputs": [],
31-
"source": "!pip install labelbox\n!pip install requests\n!pip install hmac\n!pip install hashlib\n!pip install flask\n!pip install Werkzeug"
30+
"source": [
31+
"!pip install labelbox\n",
32+
"!pip install requests\n",
33+
"!pip install hmac\n",
34+
"!pip install hashlib\n",
35+
"!pip install flask\n",
36+
"!pip install Werkzeug"
37+
]
3238
},
3339
{
3440
"cell_type": "code",
3541
"execution_count": 2,
3642
"id": "responsible-clinton",
3743
"metadata": {},
3844
"outputs": [],
39-
"source": "from labelbox import Client, Webhook\nfrom flask import Flask, request\nimport hmac\nimport hashlib\nimport threading\nfrom werkzeug.serving import run_simple\nimport json\nimport requests\nimport os\nfrom getpass import getpass\nimport socket"
45+
"source": [
46+
"from labelbox import Client, Webhook\n",
47+
"from flask import Flask, request\n",
48+
"import hmac\n",
49+
"import hashlib\n",
50+
"import threading\n",
51+
"from werkzeug.serving import run_simple\n",
52+
"import json\n",
53+
"import requests\n",
54+
"import os\n",
55+
"from getpass import getpass\n",
56+
"import socket"
57+
]
4058
},
4159
{
4260
"cell_type": "code",
4361
"execution_count": 3,
4462
"id": "foreign-theorem",
4563
"metadata": {},
4664
"outputs": [],
47-
"source": "# If you don't want to give google access to drive you can skip this cell\n# and manually set `API_KEY` below.\n\nCOLAB = \"google.colab\" in str(get_ipython())\nif COLAB:\n !pip install colab-env -qU\n from colab_env import envvar_handler\n\n envvar_handler.envload()\n\nAPI_KEY = os.environ.get(\"LABELBOX_API_KEY\")\nif not os.environ.get(\"LABELBOX_API_KEY\"):\n API_KEY = getpass(\"Please enter your labelbox api key\")\n if COLAB:\n envvar_handler.add_env(\"LABELBOX_API_KEY\", API_KEY)"
65+
"source": [
66+
"# If you don't want to give google access to drive you can skip this cell\n",
67+
"# and manually set `API_KEY` below.\n",
68+
"\n",
69+
"COLAB = \"google.colab\" in str(get_ipython())\n",
70+
"if COLAB:\n",
71+
" !pip install colab-env -qU\n",
72+
" from colab_env import envvar_handler\n",
73+
"\n",
74+
" envvar_handler.envload()\n",
75+
"\n",
76+
"API_KEY = os.environ.get(\"LABELBOX_API_KEY\")\n",
77+
"if not os.environ.get(\"LABELBOX_API_KEY\"):\n",
78+
" API_KEY = getpass(\"Please enter your labelbox api key\")\n",
79+
" if COLAB:\n",
80+
" envvar_handler.add_env(\"LABELBOX_API_KEY\", API_KEY)"
81+
]
4882
},
4983
{
5084
"cell_type": "code",
5185
"execution_count": 4,
5286
"id": "satellite-impossible",
5387
"metadata": {},
5488
"outputs": [],
55-
"source": "# Set this to a project that you want to use for the webhook\nPROJECT_ID = \"ckm4xyfncfgja0760vpfdxoro\"\n# Only update this if you have an on-prem deployment\nENDPOINT = \"https://api.labelbox.com/graphql\""
89+
"source": [
90+
"# Set this to a project that you want to use for the webhook\n",
91+
"PROJECT_ID = \"ckm4xyfncfgja0760vpfdxoro\"\n",
92+
"# Only update this if you have an on-prem deployment\n",
93+
"ENDPOINT = \"https://api.labelbox.com/graphql\""
94+
]
5695
},
5796
{
5897
"cell_type": "code",
5998
"execution_count": 5,
6099
"id": "clean-ireland",
61100
"metadata": {},
62101
"outputs": [],
63-
"source": "client = Client(api_key=API_KEY, endpoint=ENDPOINT)"
102+
"source": [
103+
"client = Client(api_key=API_KEY, endpoint=ENDPOINT)"
104+
]
64105
},
65106
{
66107
"cell_type": "code",
67108
"execution_count": 6,
68109
"id": "bacterial-cheat",
69110
"metadata": {},
70111
"outputs": [],
71-
"source": "# We are using port 3001 for this example.\n# Feel free to set to whatever port you want\nWH_PORT = 3001"
112+
"source": [
113+
"# We are using port 3001 for this example.\n",
114+
"# Feel free to set to whatever port you want\n",
115+
"WH_PORT = 3001"
116+
]
72117
},
73118
{
74119
"cell_type": "markdown",
@@ -102,7 +147,10 @@
102147
"output_type": "execute_result"
103148
}
104149
],
105-
"source": "if not COLAB:\n os.system(f\"ngrok http {WH_PORT} &\")"
150+
"source": [
151+
"if not COLAB:\n",
152+
" os.system(f\"ngrok http {WH_PORT} &\")"
153+
]
106154
},
107155
{
108156
"cell_type": "markdown",
@@ -118,15 +166,48 @@
118166
"id": "stable-group",
119167
"metadata": {},
120168
"outputs": [],
121-
"source": "# This can be any secret that matches your webhook config (we will set later)\nsecret = b\"example_secret\""
169+
"source": [
170+
"# This can be any secret that matches your webhook config (we will set later)\n",
171+
"secret = b\"example_secret\""
172+
]
122173
},
123174
{
124175
"cell_type": "code",
125176
"execution_count": 9,
126177
"id": "limiting-variety",
127178
"metadata": {},
128179
"outputs": [],
129-
"source": "app = Flask(__name__)\n\n\n@app.route(\"/\")\ndef hello_world():\n return \"Hello, World!\"\n\n\n@app.route(\"/webhook-endpoint\", methods=[\"POST\"])\ndef print_webhook_info():\n payload = request.data\n computed_signature = hmac.new(secret, msg=payload,\n digestmod=hashlib.sha1).hexdigest()\n if request.headers[\"X-Hub-Signature\"] != \"sha1=\" + computed_signature:\n print(\n \"Error: computed_signature does not match signature provided in the headers\"\n )\n return \"Error\", 500, 200\n\n print(\"=========== New Webhook Delivery ============\")\n print(\"Delivery ID: %s\" % request.headers[\"X-Labelbox-Id\"])\n print(\"Event: %s\" % request.headers[\"X-Labelbox-Event\"])\n print(\"Payload: %s\" %\n json.dumps(json.loads(payload.decode(\"utf8\")), indent=4))\n return \"Success\"\n\n\nthread = threading.Thread(target=lambda: run_simple(\"0.0.0.0\", WH_PORT, app))\nthread.start()"
180+
"source": [
181+
"app = Flask(__name__)\n",
182+
"\n",
183+
"\n",
184+
"@app.route(\"/\")\n",
185+
"def hello_world():\n",
186+
" return \"Hello, World!\"\n",
187+
"\n",
188+
"\n",
189+
"@app.route(\"/webhook-endpoint\", methods=[\"POST\"])\n",
190+
"def print_webhook_info():\n",
191+
" payload = request.data\n",
192+
" computed_signature = hmac.new(secret, msg=payload,\n",
193+
" digestmod=hashlib.sha1).hexdigest()\n",
194+
" if request.headers[\"X-Hub-Signature\"] != \"sha1=\" + computed_signature:\n",
195+
" print(\n",
196+
" \"Error: computed_signature does not match signature provided in the headers\"\n",
197+
" )\n",
198+
" return \"Error\", 500, 200\n",
199+
"\n",
200+
" print(\"=========== New Webhook Delivery ============\")\n",
201+
" print(\"Delivery ID: %s\" % request.headers[\"X-Labelbox-Id\"])\n",
202+
" print(\"Event: %s\" % request.headers[\"X-Labelbox-Event\"])\n",
203+
" print(\"Payload: %s\" %\n",
204+
" json.dumps(json.loads(payload.decode(\"utf8\")), indent=4))\n",
205+
" return \"Success\"\n",
206+
"\n",
207+
"\n",
208+
"thread = threading.Thread(target=lambda: run_simple(\"0.0.0.0\", WH_PORT, app))\n",
209+
"thread.start()"
210+
]
130211
},
131212
{
132213
"cell_type": "markdown",
@@ -158,7 +239,9 @@
158239
]
159240
}
160241
],
161-
"source": "print(requests.get(\"http://localhost:3001\").text)"
242+
"source": [
243+
"print(requests.get(\"http://localhost:3001\").text)"
244+
]
162245
},
163246
{
164247
"cell_type": "markdown",
@@ -183,15 +266,38 @@
183266
"id": "biblical-scottish",
184267
"metadata": {},
185268
"outputs": [],
186-
"source": "if not COLAB:\n res = requests.get(\"http://localhost:4040/api/tunnels\")\n assert (res.status_code == 200\n ), f\"ngrok probably isn't running. {res.status_code}, {res.text}\"\n tunnels = res.json()[\"tunnels\"]\n tunnel = [\n t for t in tunnels if t[\"config\"][\"addr\"].split(\":\")[-1] == str(WH_PORT)\n ]\n tunnel = tunnel[0] # Should only be one..\n public_url = tunnel[\"public_url\"]\nelse:\n public_url = f\"http://{socket.gethostbyname(socket.getfqdn(socket.gethostname()))}\"\nprint(public_url)"
269+
"source": [
270+
"if not COLAB:\n",
271+
" res = requests.get(\"http://localhost:4040/api/tunnels\")\n",
272+
" assert (res.status_code == 200\n",
273+
" ), f\"ngrok probably isn't running. {res.status_code}, {res.text}\"\n",
274+
" tunnels = res.json()[\"tunnels\"]\n",
275+
" tunnel = [\n",
276+
" t for t in tunnels if t[\"config\"][\"addr\"].split(\":\")[-1] == str(WH_PORT)\n",
277+
" ]\n",
278+
" tunnel = tunnel[0] # Should only be one..\n",
279+
" public_url = tunnel[\"public_url\"]\n",
280+
"else:\n",
281+
" public_url = f\"http://{socket.gethostbyname(socket.getfqdn(socket.gethostname()))}\"\n",
282+
"print(public_url)"
283+
]
187284
},
188285
{
189286
"cell_type": "code",
190287
"execution_count": 12,
191288
"id": "raising-preservation",
192289
"metadata": {},
193290
"outputs": [],
194-
"source": "# Set project to limit the scope to a single project\nproject = client.get_project(PROJECT_ID)\ntopics = {topic.value for topic in Webhook.Topic}\nwebhook = Webhook.create(client,\n topics=topics,\n url=public_url,\n secret=secret.decode(),\n project=project)"
291+
"source": [
292+
"# Set project to limit the scope to a single project\n",
293+
"project = client.get_project(PROJECT_ID)\n",
294+
"topics = {topic.value for topic in Webhook.Topic}\n",
295+
"webhook = Webhook.create(client,\n",
296+
" topics=topics,\n",
297+
" url=public_url,\n",
298+
" secret=secret.decode(),\n",
299+
" project=project)"
300+
]
195301
},
196302
{
197303
"cell_type": "code",
@@ -207,15 +313,21 @@
207313
]
208314
}
209315
],
210-
"source": "# Ok so we should be configured assuming everything is setup correctly.\n# Go to the following url and make a new label to see if it works\nprint(f\"https://app.labelbox.com/projects/{PROJECT_ID}\")"
316+
"source": [
317+
"# Ok so we should be configured assuming everything is setup correctly.\n",
318+
"# Go to the following url and make a new label to see if it works\n",
319+
"print(f\"https://app.labelbox.com/projects/{PROJECT_ID}\")"
320+
]
211321
},
212322
{
213323
"cell_type": "code",
214324
"execution_count": 14,
215325
"id": "worst-material",
216326
"metadata": {},
217327
"outputs": [],
218-
"source": "## Oops we made a mistake. The url was incorrect. We pointed the url to the hello world endpoint."
328+
"source": [
329+
"## Oops we made a mistake. The url was incorrect. We pointed the url to the hello world endpoint."
330+
]
219331
},
220332
{
221333
"cell_type": "markdown",
@@ -293,7 +405,15 @@
293405
]
294406
}
295407
],
296-
"source": "# url, topics, and status can all be updated\nupdated_url = f\"{public_url}/webhook-endpoint\"\nprint(updated_url)\nwebhook.update(url=updated_url)\n# Go to the following url and try one last time.\n# Any supported action should work (create, delete, update a label, or create, update, or delete a review)\nprint(f\"https://app.labelbox.com/projects/{PROJECT_ID}\")"
408+
"source": [
409+
"# url, topics, and status can all be updated\n",
410+
"updated_url = f\"{public_url}/webhook-endpoint\"\n",
411+
"print(updated_url)\n",
412+
"webhook.update(url=updated_url)\n",
413+
"# Go to the following url and try one last time.\n",
414+
"# Any supported action should work (create, delete, update a label, or create, update, or delete a review)\n",
415+
"print(f\"https://app.labelbox.com/projects/{PROJECT_ID}\")"
416+
]
297417
},
298418
{
299419
"cell_type": "markdown",
@@ -309,7 +429,21 @@
309429
"id": "distant-commitment",
310430
"metadata": {},
311431
"outputs": [],
312-
"source": "# DELETE:\nwebhook.update(status=Webhook.Status.INACTIVE.value)\n\n# FETCH ALL WEBHOOKS:\norg = client.get_organization()\nwebhooks = org.webhooks()\n\n# Run this to clear all.\n# WARNING!!! THIS WILL DELETE ALL WEBHOOKS FOR YOUR ORG\n# ONLY RUN THIS IS YOU KNOW WHAT YOU ARE DOING.\n# for webhook in webhooks:\n# print(webhook)\n# webhook.update(status = Webhook.Status.INACTIVE.value)"
432+
"source": [
433+
"# DELETE:\n",
434+
"webhook.update(status=Webhook.Status.INACTIVE.value)\n",
435+
"\n",
436+
"# FETCH ALL WEBHOOKS:\n",
437+
"org = client.get_organization()\n",
438+
"webhooks = org.webhooks()\n",
439+
"\n",
440+
"# Run this to clear all.\n",
441+
"# WARNING!!! THIS WILL DELETE ALL WEBHOOKS FOR YOUR ORG\n",
442+
"# ONLY RUN THIS IS YOU KNOW WHAT YOU ARE DOING.\n",
443+
"# for webhook in webhooks:\n",
444+
"# print(webhook)\n",
445+
"# webhook.update(status = Webhook.Status.INACTIVE.value)"
446+
]
313447
}
314448
],
315449
"metadata": {
@@ -333,4 +467,4 @@
333467
},
334468
"nbformat": 4,
335469
"nbformat_minor": 5
336-
}
470+
}

0 commit comments

Comments
 (0)