Skip to content

Commit

Permalink
Merge pull request #56 from BookOps-CAT/institutionlist-endpoint
Browse files Browse the repository at this point in the history
Institutionlist endpoints
  • Loading branch information
klinga authored Mar 31, 2022
2 parents af982ef + 712193a commit e3d0608
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 182 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ At the moment, BookOps-Worldcat supports requests to following OCLC's web servic
+ retrieve holding status of a single resource
+ set institution holdings for a batch of resources
+ unset institution holdings for a batch of resouces
+ set holdings for a single resource for multiple institutions
+ unset holdings for a single resource for multiple institutions


#### Basic usage:
Expand Down Expand Up @@ -149,7 +151,7 @@ with MetadataSession(authorization=token) as session:

## Changelog

Consult the [Changelog page](https://bookops-cat.github.io/bookops-worldcat/0.3/changelog/) for fixes and enhancements of each version.
Consult the [Changelog page](https://bookops-cat.github.io/bookops-worldcat/latest/changelog/) for fixes and enhancements of each version.

## Bugs/Requests

Expand All @@ -160,6 +162,5 @@ Please use [Github issue tracker](https://github.com/BookOps-CAT/bookops-worldca
+ Metadata API:
+ support for local holdings resources endpoints of the search functionality of the Metadata API
+ support for local bibliographic data endpoints
+ support for holdings batch actions for multiple institutions
+ record validation endpoints
+ methods to create and update bibliographic records
2 changes: 1 addition & 1 deletion bookops_worldcat/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = "bookops-worldcat"
__version__ = "0.4.1"
__version__ = "0.5.0"
__author__ = "Tomasz Kalata"
__author_email__ = "klingaroo@gmail.com"
37 changes: 35 additions & 2 deletions bookops_worldcat/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@
import requests

from . import __title__, __version__ # type: ignore
from bookops_worldcat.errors import WorldcatSessionError
from .authorize import WorldcatAccessToken
from .errors import WorldcatSessionError, WorldcatAuthorizationError


class WorldcatSession(requests.Session):
"""Base class, inherits all requests.Session methods"""

def __init__(
self,
authorization: WorldcatAccessToken,
agent: Optional[str] = None,
timeout: Optional[
Union[int, float, Tuple[int, int], Tuple[float, float]]
] = None,
) -> None:
requests.Session.__init__(self)
"""
Args:
authorization: WorldcatAccessToken instance
agent: "User-agent" parameter to attached to each
request in the session
timeout: how long to wait for server to send data
before giving up
"""
super().__init__()

self.authorization = authorization

if not isinstance(self.authorization, WorldcatAccessToken):
raise WorldcatSessionError(
"Argument 'authorization' must be 'WorldcatAccessToken' object."
)

if agent is None:
self.headers.update({"User-Agent": f"{__title__}/{__version__}"})
Expand All @@ -36,3 +53,19 @@ def __init__(
self.timeout = (5, 5)
else:
self.timeout = timeout # type: ignore

self._update_authorization()

def _get_new_access_token(self) -> None:
"""
Allows to continue sending request with new access token after
the previous one expired
"""
try:
self.authorization._request_token()
self._update_authorization()
except WorldcatAuthorizationError as exc:
raise WorldcatSessionError(exc)

def _update_authorization(self) -> None:
self.headers.update({"Authorization": f"Bearer {self.authorization.token_str}"})
7 changes: 6 additions & 1 deletion bookops_worldcat/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _parse_server_response(self, response: Response) -> None:
)
self.token_type = response.json()["token_type"]
else:
raise WorldcatAuthorizationError(response.json())
raise WorldcatAuthorizationError(response.content)

def _payload(self) -> Dict[str, str]:
"""Preps requests params"""
Expand Down Expand Up @@ -250,3 +250,8 @@ def is_expired(self) -> bool:
raise
except ValueError:
raise

def __repr__(self):
return (
f"access_token: '{self.token_str}', expires_at: '{self.token_expires_at}'"
)
Loading

0 comments on commit e3d0608

Please sign in to comment.