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 requesting urls containing parameters with parameters dict #2929

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 23 additions & 11 deletions github/Requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,28 @@ def get_graphql_prefix(path: Optional[str]) -> str:
path = Requester.remove_suffix(path, "/v3")
return path + "/graphql"

@staticmethod
def addParametersToUrl(
url: str,
parameters: Dict[str, Any],
) -> str:
# union parameters in url with given parameters, where url has precedence
scheme, netloc, url, params, query, fragment = urllib.parse.urlparse(url)
url_params = urllib.parse.parse_qs(query)
parameters = dict(parameters, **url_params)
parameter_list = [
(key, value)
for key, values in parameters.items()
for value in (values if isinstance(values, list) else [values])
]
# remove query from url
url = urllib.parse.urlunparse((scheme, netloc, url, params, "", fragment))

if len(parameter_list) == 0:
return url
else:
return f"{url}?{urllib.parse.urlencode(parameter_list)}"

def close(self) -> None:
"""
Close the connection to the server.
Expand Down Expand Up @@ -798,7 +820,7 @@ def __requestEncode(
requestHeaders["User-Agent"] = self.__userAgent

url = self.__makeAbsoluteUrl(url)
url = self.__addParametersToUrl(url, parameters)
url = Requester.addParametersToUrl(url, parameters)

encoded_input = None
if input is not None:
Expand Down Expand Up @@ -934,16 +956,6 @@ def __makeAbsoluteUrl(self, url: str) -> str:
url += f"?{o.query}"
return url

def __addParametersToUrl(
self,
url: str,
parameters: Dict[str, Any],
) -> str:
if len(parameters) == 0:
return url
else:
return f"{url}?{urllib.parse.urlencode(parameters)}"

def __createConnection(
self,
) -> Union[HTTPRequestsConnectionClass, HTTPSRequestsConnectionClass]:
Expand Down
36 changes: 36 additions & 0 deletions tests/PaginatedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# #
################################################################################

from datetime import datetime, timezone

from github.PaginatedList import PaginatedList as PaginatedListImpl

from . import Framework
Expand Down Expand Up @@ -326,6 +328,40 @@ def testCustomPerPageWithGetPage(self):
self.g.per_page = 100
self.assertEqual(len(self.repo.get_issues().get_page(2)), 100)

def testCustomPerPageIteration(self):
self.g.per_page = 3
repo = self.g.get_repo("PyGithub/PyGithub")
comments = repo.get_issue(1136).get_comments()
self.assertEqual(
[
datetime(2019, 8, 10, 18, 16, 46, tzinfo=timezone.utc),
datetime(2024, 1, 6, 16, 4, 34, tzinfo=timezone.utc),
datetime(2024, 1, 6, 17, 34, 11, tzinfo=timezone.utc),
datetime(2024, 3, 20, 15, 24, 15, tzinfo=timezone.utc),
datetime(2024, 3, 21, 10, 55, 14, tzinfo=timezone.utc),
datetime(2024, 3, 21, 14, 2, 22, tzinfo=timezone.utc),
datetime(2024, 3, 24, 13, 58, 57, tzinfo=timezone.utc),
],
[comment.created_at for comment in comments],
)

def testCustomPerPageReversedIteration(self):
self.g.per_page = 3
repo = self.g.get_repo("PyGithub/PyGithub")
comments = repo.get_issue(1136).get_comments().reversed
self.assertEqual(
[
datetime(2024, 3, 24, 13, 58, 57, tzinfo=timezone.utc),
datetime(2024, 3, 21, 14, 2, 22, tzinfo=timezone.utc),
datetime(2024, 3, 21, 10, 55, 14, tzinfo=timezone.utc),
datetime(2024, 3, 20, 15, 24, 15, tzinfo=timezone.utc),
datetime(2024, 1, 6, 17, 34, 11, tzinfo=timezone.utc),
datetime(2024, 1, 6, 16, 4, 34, tzinfo=timezone.utc),
datetime(2019, 8, 10, 18, 16, 46, tzinfo=timezone.utc),
],
[comment.created_at for comment in comments],
)

def testNoFirstPage(self):
self.assertFalse(next(iter(self.list), None))

Expand Down