Pyzotero: a Python client for the Zotero API pyzotero.readthedocs.io
zotero

Merge pull request #246 from urschrei/shugel/push-kkkmxlzropss

Fix locale parameter being ignored when using add_parameters

authored by urschrei.eurosky.social and committed by

GitHub a102d761 a091179a

+41 -11
+12 -4
src/pyzotero/zotero.py
··· 470 470 params = {} 471 471 if not self.url_params: 472 472 self.url_params = {} 473 - merged_params = {**params, **self.url_params} 473 + merged_params = {**self.url_params, **params} 474 474 # our incoming url might be from the "links" dict, in which case it will contain url parameters. 475 475 # Unfortunately, httpx doesn't like to merge query paramaters in the url string and passed params 476 476 # so we strip the url params, combining them with our existing url_params ··· 580 580 581 581 Also ensure that only valid format/content combinations are requested 582 582 """ 583 - self.url_params = None 583 + # Preserve constructor-level parameters (like locale) while allowing method-level overrides 584 + if self.url_params is None: 585 + self.url_params = {} 586 + 587 + # Store existing params to preserve things like locale 588 + preserved_params = self.url_params.copy() 589 + 584 590 # we want JSON by default 585 591 if not params.get("format"): 586 592 params["format"] = "json" ··· 597 603 del params["limit"] 598 604 # bib format can't have a limit 599 605 if params.get("format") == "bib": 600 - del params["limit"] 601 - self.url_params = params 606 + params.pop("limit", None) 607 + 608 + # Merge preserved params with new params (new params override existing ones) 609 + self.url_params = {**preserved_params, **params} 602 610 603 611 def _build_query(self, query_string, no_params=False): 604 612 """Set request parameters. Will always add the user ID if it hasn't
+29 -7
tests/test_zotero.py
··· 104 104 zot = z.Zotero("myuserID", "user", "myuserkey") 105 105 _ = zot.items() 106 106 req = zot.request 107 - self.assertEqual(str(req.url).find("locale"), 44) 107 + self.assertIn("locale=en-US", str(req.url)) 108 + 109 + @httpretty.activate 110 + def testLocalePreservedWithMethodParams(self): 111 + """Should preserve locale when methods provide their own parameters""" 112 + HTTPretty.register_uri( 113 + HTTPretty.GET, 114 + "https://api.zotero.org/users/myuserID/items/top", 115 + content_type="application/json", 116 + body=self.items_doc, 117 + ) 118 + # Test with non-default locale 119 + zot = z.Zotero("myuserID", "user", "myuserkey", locale="de-DE") 120 + # Call top() with limit which internally adds parameters 121 + _ = zot.top(limit=1) 122 + req = zot.request 123 + # Check that locale is preserved in the URL 124 + self.assertIn("locale=de-DE", str(req.url)) 125 + # Also verify the method parameter is present 126 + self.assertIn("limit=1", str(req.url)) 108 127 109 128 @httpretty.activate 110 129 def testRequestBuilderLimitNone(self): ··· 368 387 body=self.tags_doc, 369 388 ) 370 389 _ = zot.tags(limit=1) 371 - self.assertEqual( 372 - "https://api.zotero.org/users/myuserID/tags?locale=en-US&limit=1&format=json", 373 - zot.request.url, 390 + # Check that all expected parameters are present 391 + url_str = str(zot.request.url) 392 + self.assertIn("locale=en-US", url_str) 393 + self.assertIn("limit=1", url_str) 394 + self.assertIn("format=json", url_str) 395 + self.assertTrue( 396 + url_str.startswith("https://api.zotero.org/users/myuserID/tags?") 374 397 ) 375 398 376 399 @httpretty.activate ··· 408 431 self.assertEqual("smart_cities", groups_data[0]["data"]["name"]) 409 432 410 433 def testParamsReset(self): 411 - """Should successfully reset URL parameters after a query string 412 - is built 413 - """ 434 + """Should preserve existing URL parameters when add_parameters is called multiple times""" 414 435 zot = z.Zotero("myuserID", "user", "myuserkey") 415 436 zot.add_parameters(start=5, limit=10) 416 437 zot._build_query("/whatever") 417 438 zot.add_parameters(start=2) 439 + # Should get default limit=100 since no limit specified in second call 418 440 self.assertEqual( 419 441 parse_qs("start=2&format=json&limit=100"), 420 442 parse_qs(urlencode(zot.url_params, doseq=True)),