From b85da0d38d492eafdd0efdb0cafa295a4df77853 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Mon, 13 Jun 2022 11:46:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(requester):=20=E2=9C=A8=20allow=20params?= =?UTF-8?q?=20in=20get=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/requester.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/utils/requester.py b/utils/requester.py index 36025d6..69aa2a4 100644 --- a/utils/requester.py +++ b/utils/requester.py @@ -5,9 +5,11 @@ # 发布 CurseForgeModpackDownloader 是希望它能有用,但是并无保障;甚至连可销售和符合某个特定的目的都不保证。请参看 GNU 通用公共许可证,了解详情。 # 你应该随程序获得一份 GNU 通用公共许可证的复本。如果没有,请看 。 -from urllib.parse import quote +from urllib.parse import quote, urlencode from urllib.request import Request, urlopen +from typing import Any, Dict + class Response: def __init__(self, response): @@ -34,8 +36,20 @@ class Requester: } @classmethod - def get(cls, url: str) -> Response: + def get(cls, url: str, params: Dict[str, Any] = None) -> Response: + """ + Request using get method. + :param url: URL. + :param params: Parametric. + :return: A Response object. + """ url = quote(url, safe=':/') + + # add params + if params: + url = url + '?' + urlencode(params) + + # send request request = Request(url, headers=cls.HEADERS, method='GET') return Response(urlopen(request))