File: transports.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (58 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download
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
import abc
from typing import Any, Dict, Optional

from requests import Response, Session


class Transport(abc.ABC):
    __slots__ = ()

    @abc.abstractmethod
    def get(self, url: str, params: Dict, headers: Dict) -> bytes:
        """Send a GET request."""

    @abc.abstractmethod
    def post(self, url: str, data: Any, headers: Dict) -> bytes:
        """Send a POST request."""


class DefaultTransport(Transport):
    """
    Default transport based on the requests library.

    :param timeout: Read timeout
    """

    __slots__ = "timeout", "session"

    def __init__(self, timeout: float = 2.0, session: Optional[Session] = None):
        self.timeout = timeout
        self.session = session or Session()

    def get(self, url: str, params: Dict, headers: Dict) -> bytes:
        """
        :raises HTTPError: if status code is not valid for content unmarshalling.
        """
        res = self.session.get(
            url, params=params, headers=headers, timeout=self.timeout
        )
        return self.handle_response(res)

    def post(self, url: str, data: Any, headers: Dict) -> Any:
        """
        :raises HTTPError: if status code is not valid for content unmarshalling.
        """
        res = self.session.post(url, data=data, headers=headers, timeout=self.timeout)
        return self.handle_response(res)

    @classmethod
    def handle_response(cls, response: Response) -> bytes:
        """
        Status codes 200 or 500 means that we can unmarshall the response.

        :raises HTTPError: If the response status code is not 200 or 500
        """
        if response.status_code not in (200, 500):
            response.raise_for_status()

        return response.content