Skip to content

Commit

Permalink
feat: トークンを使用しなくても使用できるように
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Mar 19, 2023
1 parent e9db4f9 commit 6079056
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions mipac/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


class Client:
def __init__(self, url: str, token: str) -> None:
def __init__(self, url: str, token: str | None = None) -> None:
self.__url: str = url
self.__token: str = token
self.__token: str | None = token
self.config: Config = config
self.http: HTTPClient = HTTPClient(url, token)

Expand Down
24 changes: 13 additions & 11 deletions mipac/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def __init__(self, method: Literal['GET', 'POST'], path: ENDPOINTS):


class HTTPClient:
def __init__(self, url: str, token: str) -> None:
def __init__(self, url: str, token: str | None = None) -> None:
user_agent = (
'Misskey Bot (https://github.com/yupix/MiPA {0})' + 'Python/{1[0]}.{1[1]} aiohttp/{2}'
)
self.user_agent = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
self._session: aiohttp.ClientSession = MISSING
self._url: str = url
self._token: str = token
self._token: str | None = token

@property
def session(self) -> aiohttp.ClientSession:
Expand All @@ -81,7 +81,8 @@ async def request(
key = 'json' if 'json' in kwargs or 'data' not in kwargs else 'data'
if not kwargs.get(key):
kwargs[key] = {}
kwargs[key]['i'] = self._token
if self._token:
kwargs[key]['i'] = self._token

replace_list = kwargs.pop('replace_list', {})

Expand All @@ -107,7 +108,7 @@ async def request(
async def close_session(self) -> None:
await self._session.close()

async def login(self) -> IUserDetailed:
async def login(self) -> IUserDetailed | None:
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:
Expand All @@ -117,10 +118,11 @@ async def login(self) -> IUserDetailed:
host=match_domain.group(1), is_ssl=protocol,
)
self._session = aiohttp.ClientSession(ws_response_class=MisskeyClientWebSocketResponse)
data: IUserDetailed = await self.request(Route('POST', '/api/i'), auth=True)
if config.use_version_autodetect:
meta: IMeta = await self.request(Route('POST', '/api/meta'), auth=True)
use_version = int(meta['version'].split('.')[0])
if isinstance(use_version, int) and use_version in (13, 12, 11):
config.use_version = use_version
return data
if self._token:
data: IUserDetailed = await self.request(Route('POST', '/api/i'), auth=True)
if config.use_version_autodetect:
meta: IMeta = await self.request(Route('POST', '/api/meta'), auth=True)
use_version = int(meta['version'].split('.')[0])
if isinstance(use_version, int) and use_version in (13, 12, 11):
config.use_version = use_version
return data

0 comments on commit 6079056

Please sign in to comment.