Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix everything() method #201

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/pyzotero/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def build_url(base_url, path, args_dict=None):
return urlunparse(url_parts)


def merge_params(url, params):
"""This function strips query parameters, extracting them into a dict, then merging it with
the "params" dict, returning the truncated url and merged query params dict"""
parsed = urlparse(url)
# Extract query parameters from URL
incoming = parse_qs(parsed.query)
incoming = {k: v[0] for k, v in incoming.items()}

# Create new params dict by merging
merged = {**incoming, **params}

# Get base URL by zeroing out the query component
base_url = urlunparse(parsed._replace(query=""))

return base_url, merged


def token():
"""Return a unique 32-char write-token"""
return str(uuid.uuid4().hex)
Expand Down Expand Up @@ -458,7 +475,16 @@ def _retrieve_data(self, request=None, params=None):
if not self.url_params:
self.url_params = {}
merged_params = params | self.url_params
self.request = self.client.get(url=full_url, params=merged_params)
# our incoming url might be from the "links" dict, in which case it will contain url parameters.
# Unfortunately, httpx doesn't like to merge query paramaters in the url string and passed params
# so we strip the url params, combining them with our existing url_params
final_url, final_params = merge_params(full_url, merged_params)
self.request = self.client.get(
url=final_url,
params=final_params,
headers=self.default_headers(),
timeout=timeout,
)
self.request.encoding = "utf-8"
try:
self.request.raise_for_status()
Expand Down Expand Up @@ -894,7 +920,6 @@ def follow(self):
"""Return the result of the call to the URL in the 'Next' link"""
if n := self.links.get("next"):
newurl = self._striplocal(n)
print(newurl)
return newurl
return

Expand Down
Loading