From 051a1f574faf81ba34d13818ddb69ebd65c6b43a Mon Sep 17 00:00:00 2001 From: yupix Date: Sun, 12 May 2024 12:32:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20url=E3=81=AE=E3=83=91=E3=82=B9=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=81=97=E3=81=AA=E3=81=84=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=20close=20#138?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mipac/http.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mipac/http.py b/mipac/http.py index e5c6eff..d7b921f 100644 --- a/mipac/http.py +++ b/mipac/http.py @@ -2,9 +2,9 @@ from __future__ import annotations import json import logging -import re import sys from typing import Any, Literal +import urllib.parse import aiohttp @@ -71,18 +71,20 @@ class HTTPClient: user_agent = ( "Misskey Bot (https://github.com/yupix/MiPA {0})" + "Python/{1[0]}.{1[1]} aiohttp/{2}" ) - self._url: str = url + parsed_url = urllib.parse.urlparse(url) + + if parsed_url.scheme not in ["http", "https"] or parsed_url.netloc is None: + raise Exception("Server URL cannot be retrieved or protocol (http / https) is missing") + + protocol = True if parsed_url.scheme == "https" else False + self._token: str | None = token self.user_agent = user_agent.format(__version__, sys.version_info, aiohttp.__version__) self._session = aiohttp.ClientSession(ws_response_class=MisskeyClientWebSocketResponse) - match_domain = re.search(r"https?:\/\/([^\/]+)", self._url) - match_protocol = re.search(r"^(http|https)", self._url) - if match_domain is None or match_protocol is None: - raise Exception("Server URL cannot be retrieved or protocol (http / https) is missing") - protocol = True if match_protocol.group(1) == "https" else False + self._url = f"{parsed_url.scheme}://{parsed_url.netloc}" config.from_dict( - host=match_domain.group(1), + host=parsed_url.netloc, is_ssl=protocol, )