-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from arXiv/add_resumption_token
Add resumption token
- Loading branch information
Showing
6 changed files
with
318 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Dict, Tuple | ||
import json | ||
import base64 | ||
|
||
from oaipmh.data.oai_errors import OAIBadResumptionToken | ||
from oaipmh.data.oai_properties import OAIParams | ||
|
||
class ResToken: | ||
def __init__(self, params: Dict[OAIParams, str], start_val: int): | ||
self.params = params | ||
self.start_val = start_val | ||
self.token_str = self.to_token() | ||
|
||
def to_token(self) -> str: | ||
data = { | ||
"params": self.params, | ||
"start_val": self.start_val | ||
} | ||
json_str = json.dumps(data) | ||
return base64.b64encode(json_str.encode("utf-8")).decode("utf-8") | ||
|
||
@classmethod | ||
def from_token(cls, encoded_str: str) -> Tuple[Dict[str, str], int]: | ||
try: | ||
json_str = base64.b64decode(encoded_str).decode("utf-8") | ||
data = json.loads(json_str) | ||
if not isinstance(data, dict) or set(data.keys()) != {"params", "start_val"}: | ||
raise OAIBadResumptionToken("Token structure is invalid.") | ||
if not isinstance(data["params"], dict) or not isinstance(data["start_val"], int): | ||
raise OAIBadResumptionToken("Token contains invalid data types.") | ||
return data["params"], data["start_val"] | ||
except (Exception): | ||
raise OAIBadResumptionToken("Token decoding failed or format is invalid.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.