Skip to content

bookops_worldcat.query

Handles actual requests to OCLC services

Query

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

Sends a request to OClC service and unifies received excepitons

Query.response attribute is requests.Response instance that can be parsed to exctract received information from the web service.

prepared_request:               `requests.models.PreparedRequest` instance
timeout:                        how long to wait for server to send data
                                before giving up
Source code in bookops_worldcat\query.py
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
def __init__(
    self,
    session: Session,
    prepared_request: PreparedRequest,
    timeout: Optional[
        Union[int, float, Tuple[int, int], Tuple[float, float]]
    ] = None,
) -> None:
    """
    Args:
        session:                        `requests.Session` instance
        prepared_request:               `requests.models.PreparedRequest` instance
        timeout:                        how long to wait for server to send data
                                        before giving up

    Raises:
        WorldcatRequestError

    """
    if not isinstance(prepared_request, PreparedRequest):
        raise AttributeError("Invalid type for argument 'prepared_request'.")

    self.response = None

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

        if "/ih/data" in prepared_request.url:
            if self.response.status_code == 409:
                # HTTP 409 code returns when trying to set/unset
                # holdings on already set/unset record
                # It is reasonable not to raise any exceptions
                # in this case
                pass  # pragma: no cover
            else:
                self.response.raise_for_status()
        else:
            self.response.raise_for_status()

    except HTTPError as exc:
        raise WorldcatRequestError(f"{exc}")
    except (Timeout, ConnectionError):
        raise WorldcatRequestError(f"Connection Error: {sys.exc_info()[0]}")
    except:
        raise WorldcatRequestError(f"Unexpected request error: {sys.exc_info()[0]}")