Skip to content

Commit

Permalink
added unsubscribe_all method to Webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Teekeks committed Oct 7, 2020
1 parent 136f65c commit 980c1f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,13 @@ Stopping the webhook:
```python
hook.stop()
```

### Unsubscribing from any remaining active Webhook topic

Should your management of webhook subscriptions fail (due to a crash or something else) and there is a active webhook remaining after your program closed, you may use the following:

```python
hook.unsubscribe_all(twitch)
```

The parameter is a ``twitchAPI.twitch.Twitch`` instance with app authentication.
1 change: 0 additions & 1 deletion twitchAPI/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,3 @@ def make_dict_field_enum(data: dict,
return [make_dict_field_enum(d, fields, _enum, default) for d in data]
else:
return make_dict_field_enum(data, fields, _enum, default)

14 changes: 13 additions & 1 deletion twitchAPI/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ def stop(self):
# HELPER
# ==================================================================================================================

def unsubscribe_all(self, twitch: 'twitchAPI.twitch.Twitch') -> None:
"""Unsubscribe from all active Webhooks
:param twitch: App authorized instance of twitchAPI.twitch.Twitch
:rtype None
"""
data = twitch.get_webhook_subscriptions()
for d in data.get('data', []):
self._subscribe(d.get('callback'), d.get('topic'), mode="unsubscribe", callback_full=False)

def __build_request_header(self):
headers = {
"Client-ID": self.__client_id
Expand Down Expand Up @@ -140,12 +150,14 @@ def __add_callable(self, uuid: UUID, callback_func: Union[Callable, None]) -> No
arr.append(callback_func)
self.__callbacks[uuid] = arr

def _subscribe(self, callback_path: str, topic_url: str, mode: str = "subscribe"):
def _subscribe(self, callback_path: str, topic_url: str, mode: str = "subscribe", callback_full=True):
""""Subscribe to Twitch Topic"""
data = {'hub.callback': self.callback_url + callback_path,
'hub.mode': mode,
'hub.topic': topic_url,
'hub.lease_seconds': self.subscribe_least_seconds}
if not callback_full:
data['hub.callback'] = callback_path
if self.secret is not None:
data['hub.secret'] = self.secret
result = self.__api_post_request(TWITCH_API_BASE_URL + "webhooks/hub", data=data)
Expand Down

0 comments on commit 980c1f3

Please sign in to comment.