2424from __future__ import annotations
2525
2626import json
27- from typing import TYPE_CHECKING , Awaitable , Optional , Union
27+ from typing import TYPE_CHECKING , Coroutine , Optional , Union
2828
2929import aiohttp
3030
@@ -45,41 +45,37 @@ class HTTPClient:
4545
4646 Attributes
4747 ----------
48- api_key: Optional[:class:`str`]
49- Your private API token to access the Mystb.in API.
50- Currently unobtainable.
5148 session: Optional[Union[:class:`aiohttp.ClientSession`, :class:`requests.Session`]]
5249 Optional session to be passed to the creation of the client.
5350 """
5451
55- __slots__ = ("api_key" , "session" , "_are_we_async" )
52+ __slots__ = (
53+ "session" ,
54+ "_are_we_async" ,
55+ )
5656
5757 def __init__ (
5858 self ,
5959 * ,
60- api_key : str = "" ,
6160 session : Optional [Union [aiohttp .ClientSession , requests .Session ]] = None ,
6261 ) -> None :
63- self .api_key = api_key
6462 self ._are_we_async = session is None or isinstance (session , aiohttp .ClientSession )
65- self .session = session
63+ self .session = session # type: ignore
6664
67- async def _generate_async_session (self ) -> aiohttp . ClientSession :
65+ async def _generate_async_session (self ) -> None :
6866 """
6967 This method will create a new and blank `aiohttp.ClientSession` instance for use.
7068 This method should not be called if a session is passed to the constructor.
7169 """
72- self .session = aiohttp .ClientSession ()
70+ self .session : aiohttp . ClientSession = aiohttp .ClientSession ()
7371
74- return self .session
75-
76- def post (self , content : str , syntax : str = None ) -> Union [Paste , Awaitable ]:
72+ def post (self , content : str , syntax : str = None ) -> Union [Paste , Coroutine [None , None , Paste ]]:
7773 """
7874 This will post to the Mystb.in API and return the url.
7975 Can pass an optional suffix for the syntax highlighting.
8076
8177 Parameters
82- ----------
78+ -----------
8379 content: :class:`str`
8480 The content you are posting to the Mystb.in API.
8581 syntax: :class:`str`
@@ -90,16 +86,19 @@ def post(self, content: str, syntax: str = None) -> Union[Paste, Awaitable]:
9086 return self ._perform_sync_post (content , syntax )
9187
9288 def _perform_sync_post (self , content : str , syntax : str = None ) -> Paste :
93- """ Sync post request. """
89+ assert not isinstance (self .session , aiohttp .ClientSession )
90+
9491 payload = {"meta" : [{"index" : 0 , "syntax" : syntax }]}
92+
93+ assert self .session is not None and not isinstance (self .session , aiohttp .ClientSession )
94+
9595 response : requests .Response = self .session .post (
9696 API_BASE_URL ,
9797 files = {
9898 "data" : content ,
9999 "meta" : (None , json .dumps (payload ), "application/json" ),
100100 },
101101 timeout = CLIENT_TIMEOUT ,
102- headers = {"Authorization" : self .api_key },
103102 )
104103
105104 if response .status_code not in [200 , 201 ]:
@@ -108,9 +107,10 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
108107 return Paste (response .json (), syntax )
109108
110109 async def _perform_async_post (self , content : str , syntax : str = None ) -> Paste :
111- """ Async post request. """
112110 if not self .session and self ._are_we_async :
113- self .session = await self ._generate_async_session ()
111+ await self ._generate_async_session ()
112+
113+ assert self .session is not None and isinstance (self .session , aiohttp .ClientSession )
114114
115115 multi_part_write = aiohttp .MultipartWriter ()
116116 paste_content = multi_part_write .append (content )
@@ -122,17 +122,18 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
122122 API_BASE_URL ,
123123 data = multi_part_write ,
124124 timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT ),
125- headers = {"Authorization" : self .api_key },
126125 ) as response :
127126 status_code = response .status
128127 response_text = await response .text ()
129- if status_code not in (200 ,):
128+
129+ if status_code != 200 :
130130 raise APIError (status_code , response_text )
131+
131132 response_data = await response .json ()
132133
133134 return Paste (response_data , syntax )
134135
135- def get (self , paste_id : str ) -> Union [PasteData , Awaitable ]:
136+ def get (self , paste_id : str ) -> Union [PasteData , Coroutine [ None , None , PasteData ] ]:
136137 """
137138 This will perform a GET request against the Mystb.in API and return the url.
138139 Must be passed a valid paste ID or URL.
@@ -155,19 +156,20 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
155156 return self ._perform_async_get (paste_id )
156157
157158 def _perform_sync_get (self , paste_id : str ) -> PasteData :
158- """ Sync get request. """
159- response : requests .Response = self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT )
159+ assert self .session is not None and not isinstance (self .session , aiohttp .ClientSession )
160160
161- if response .status_code not in (200 ,):
162- raise BadPasteID ("This is an invalid Mystb.in paste ID." )
161+ with self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT ) as response :
162+ if response .status_code not in (200 ,):
163+ raise BadPasteID ("This is an invalid Mystb.in paste ID." )
163164
164165 paste_data = response .json ()
165166 return PasteData (paste_id , paste_data )
166167
167168 async def _perform_async_get (self , paste_id : str ) -> PasteData :
168- """ Async get request. """
169169 if not self .session :
170- self .session : aiohttp .ClientSession = await self ._generate_async_session ()
170+ await self ._generate_async_session ()
171+
172+ assert self .session is not None and isinstance (self .session , aiohttp .ClientSession )
171173
172174 async with self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )) as response :
173175 if response .status not in (200 ,):
@@ -176,7 +178,7 @@ async def _perform_async_get(self, paste_id: str) -> PasteData:
176178
177179 return PasteData (paste_id , paste_data )
178180
179- async def close (self ):
180- """ Async only - close the session. """
181+ async def close (self ) -> None :
182+ """Async only - close the session."""
181183 if self .session and isinstance (self .session , aiohttp .ClientSession ):
182184 await self .session .close ()
0 commit comments