Skip to content

whos_there.senders.base

[docs] module whos_there.senders.base

 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
import abc
import json
from typing import Any

import requests


class Sender(abc.ABC):
    def __init__(self) -> None:
        super().__init__()

    @abc.abstractmethod
    def send(self, text: str) -> Any:
        """Actually send the text to the desired output method.

        Args:
            text: Message to send.

        Returns:
            Optional return value.
        """
        pass

    def _send_json(self, url: str, data: Any) -> requests.Response:
        """Send JSON data to URL.

        Args:
            url: URL to send the data payload to.
            data: Payload data.

        Returns:
            Response of the HTTP request.
        """
        headers = {"Content-Type": "application/json"}
        payload = json.dumps(data)
        return requests.post(url=url, data=payload, headers=headers, timeout=5000)