-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
125 lines (92 loc) · 3.42 KB
/
__init__.py
File metadata and controls
125 lines (92 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import logging
import json
from urllib.parse import urlencode
log = logging.getLogger("socketdev")
class Webhooks:
def __init__(self, api):
self.api = api
def list(self, org_slug: str, **query_params) -> dict:
"""
List webhooks.
Args:
org_slug: Organization slug
**query_params: Optional query parameters
Returns:
dict containing list of webhooks
"""
path = f"orgs/{org_slug}/webhooks"
if query_params:
path += "?" + urlencode(query_params)
response = self.api.do_request(path=path)
if response.status_code == 200:
return response.json()
log.error(f"Error listing webhooks: {response.status_code}")
log.error(response.text)
return {}
def create(self, org_slug: str, **kwargs) -> dict:
"""
Create a new webhook.
Args:
org_slug: Organization slug
**kwargs: Webhook configuration parameters
Returns:
dict containing the created webhook
"""
path = f"orgs/{org_slug}/webhooks"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method="POST", payload=payload)
if response.status_code in (200, 201):
return response.json()
log.error(f"Error creating webhook: {response.status_code}")
log.error(response.text)
return {}
def get(self, org_slug: str, webhook_id: str) -> dict:
"""
Get a specific webhook.
Args:
org_slug: Organization slug
webhook_id: Webhook ID
Returns:
dict containing webhook details
"""
path = f"orgs/{org_slug}/webhooks/{webhook_id}"
response = self.api.do_request(path=path)
if response.status_code == 200:
return response.json()
log.error(f"Error getting webhook: {response.status_code}")
log.error(response.text)
return {}
def update(self, org_slug: str, webhook_id: str, **kwargs) -> dict:
"""
Update a webhook.
Args:
org_slug: Organization slug
webhook_id: Webhook ID
**kwargs: Webhook configuration parameters to update
Returns:
dict containing the updated webhook
"""
path = f"orgs/{org_slug}/webhooks/{webhook_id}"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method="PUT", payload=payload)
if response.status_code == 200:
return response.json()
log.error(f"Error updating webhook: {response.status_code}")
log.error(response.text)
return {}
def delete(self, org_slug: str, webhook_id: str) -> dict:
"""
Delete a webhook.
Args:
org_slug: Organization slug
webhook_id: Webhook ID
Returns:
dict containing the deletion response
"""
path = f"orgs/{org_slug}/webhooks/{webhook_id}"
response = self.api.do_request(path=path, method="DELETE")
if response.status_code == 200:
return response.json()
log.error(f"Error deleting webhook: {response.status_code}")
log.error(response.text)
return {}