Skip to content

bookops_worldcat.query

Handles requests to OCLC web services.

Query

Query(
    session: MetadataSession,
    prepared_request: PreparedRequest,
    timeout: Union[
        int,
        float,
        Tuple[int, int],
        Tuple[float, float],
        None,
    ] = (5, 5),
)

Sends a request to OCLC web service and unifies exceptions.

Query object handles automatic refresh of expired token before each request is made to the web service. Query.response attribute is requests.Response instance that can be parsed to extract information received from the web service.

PARAMETER DESCRIPTION
session

metadata_api.MetadataSession instance.

TYPE: MetadataSession

prepared_request

requests.PreparedRequest instance.

TYPE: PreparedRequest

timeout

How long to wait for server to send data before giving up. Accepts separate values for connect and read timeouts or a single value.

TYPE: Union[int, float, Tuple[int, int], Tuple[float, float], None] DEFAULT: (5, 5)

RAISES DESCRIPTION
WorldcatRequestError

If the request encounters any errors.

TypeError

If prepared_request arg is passed anything other than a requests.PreparedRequest object.

Source code in bookops_worldcat\query.py
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
def __init__(
    self,
    session: MetadataSession,
    prepared_request: PreparedRequest,
    timeout: Union[int, float, Tuple[int, int], Tuple[float, float], None] = (
        5,
        5,
    ),
) -> None:
    """Initializes Query object.

    Args:
        session:
            `metadata_api.MetadataSession` instance.
        prepared_request:
            `requests.PreparedRequest` instance.
        timeout:
            How long to wait for server to send data before giving up. Accepts
            separate values for connect and read timeouts or a single value.

    Raises:
        WorldcatRequestError:
            If the request encounters any errors.
        TypeError:
            If `prepared_request` arg is passed anything other than a
            `requests.PreparedRequest` object.
    """
    if not isinstance(prepared_request, PreparedRequest):
        raise TypeError("Invalid type for argument 'prepared_request'.")

    # make sure access token is still valid and if not request a new one
    if session.authorization.is_expired():
        session._get_new_access_token()

    try:
        self.response = session.send(prepared_request, timeout=timeout)
        self.response.raise_for_status()

    except HTTPError as exc:
        raise WorldcatRequestError(
            f"{exc}. Server response: "  # type: ignore
            f"{self.response.content.decode('utf-8')}"
        )
    except (Timeout, ConnectionError, RetryError):
        raise WorldcatRequestError(f"Connection Error: {sys.exc_info()[0]}")

    except Exception:
        raise WorldcatRequestError(f"Unexpected request error: {sys.exc_info()[0]}")