2626import json
2727from typing import TYPE_CHECKING , Awaitable , Callable , Optional , Type , Union
2828
29+ import aiohttp
2930if TYPE_CHECKING :
3031 import requests
3132
32- import aiohttp
33- import yarl
34-
3533from .constants import *
3634from .errors import *
3735from .objects import *
@@ -102,7 +100,7 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
102100 """ Sync post request. """
103101 payload = {'meta' : [{'index' : 0 , 'syntax' : syntax }]}
104102 response : Type ["requests.Response" ] = self .session .post (API_BASE_URL , files = {
105- 'data' : content , 'meta' : (None , json .dumps (payload ), 'application/json' )}, timeout = CLIENT_TIMEOUT )
103+ 'data' : content , 'meta' : (None , json .dumps (payload ), 'application/json' )}, timeout = CLIENT_TIMEOUT )
106104 if response .status_code not in [200 , 201 ]:
107105 raise APIError (response .status_code , response .text )
108106
@@ -122,7 +120,7 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
122120 async with self .session .post (API_BASE_URL , data = multi_part_write ) as response :
123121 status_code = response .status
124122 response_text = await response .text ()
125- if status_code not in [ 200 , 201 ] :
123+ if status_code not in ( 200 , ) :
126124 raise APIError (status_code , response_text )
127125 response_data = await response .json ()
128126
@@ -149,16 +147,20 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
149147
150148 def _perform_sync_get (self , paste_id : str , syntax : str = None ) -> PasteData :
151149 """ Sync get request. """
152- response = self .session .get (
150+ response : requests . Response = self .session .get (
153151 f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT )
152+ if response .status_code not in (200 , ):
153+ raise BadPasteID ("This is an invalid Mystb.in paste ID." )
154154 paste_data = response .json ()
155155 return PasteData (paste_id , paste_data )
156156
157157 async def _perform_async_get (self , paste_id : str , syntax : str = None ) -> PasteData :
158158 """ Async get request. """
159159 if not self .session :
160- self .session = await self ._generate_async_session ()
160+ self .session : aiohttp . ClientSession = await self ._generate_async_session ()
161161 async with self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )) as response :
162+ if response .status not in (200 , ):
163+ raise BadPasteID ("This is an invalid Mystb.in paste ID." )
162164 paste_data = await response .json ()
163165 return PasteData (paste_id , paste_data )
164166
0 commit comments