@@ -70,15 +70,18 @@ def _generate_sync_session(self, session: Type["requests.Session"]) -> Type["req
7070 if self .api_key :
7171 session .headers .update (
7272 {"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" })
73+
7374 return session
7475
7576 async def _generate_async_session (self , session : Optional [aiohttp .ClientSession ] = None ) -> aiohttp .ClientSession :
7677 """ We will update (or create) a :class:`aiohttp.ClientSession` instance with the auth we require. """
7778 if not session :
7879 session = aiohttp .ClientSession (raise_for_status = False )
80+
7981 if self .api_key :
8082 session ._default_headers .update (
8183 {"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" })
84+
8285 session ._timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )
8386 return session
8487
@@ -103,6 +106,7 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
103106 payload = {'meta' : [{'index' : 0 , 'syntax' : syntax }]}
104107 response : Type ["requests.Response" ] = self .session .post (API_BASE_URL , files = {
105108 'data' : content , 'meta' : (None , json .dumps (payload ), 'application/json' )}, timeout = CLIENT_TIMEOUT )
109+
106110 if response .status_code not in [200 , 201 ]:
107111 raise APIError (response .status_code , response .text )
108112
@@ -112,13 +116,15 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
112116 """ Async post request. """
113117 if not self .session and self ._are_we_async :
114118 self .session = await self ._generate_async_session ()
119+
115120 multi_part_write = aiohttp .MultipartWriter ()
116121 paste_content = multi_part_write .append (content )
117122 paste_content .set_content_disposition ("form-data" , name = "data" )
118123 paste_content = multi_part_write .append_json (
119124 {'meta' : [{'index' : 0 , 'syntax' : syntax }]}
120125 )
121126 paste_content .set_content_disposition ("form-data" , name = "meta" )
127+
122128 async with self .session .post (API_BASE_URL , data = multi_part_write ) as response :
123129 status_code = response .status
124130 response_text = await response .text ()
@@ -139,31 +145,39 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
139145 The ID of the paste you are going to retrieve.
140146 """
141147 paste_id_match = MB_URL_RE .match (paste_id )
148+
142149 if not paste_id_match :
143150 raise BadPasteID ("This is an invalid Mystb.in paste ID." )
151+
144152 paste_id = paste_id_match .group ('ID' )
145153 syntax = paste_id_match .group ('syntax' )
154+
146155 if not self ._are_we_async :
147156 return self ._perform_sync_get (paste_id , syntax )
157+
148158 return self ._perform_async_get (paste_id , syntax )
149159
150160 def _perform_sync_get (self , paste_id : str , syntax : str = None ) -> PasteData :
151161 """ Sync get request. """
152162 response : Type ["requests.Response" ] = self .session .get (
153163 f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT )
164+
154165 if response .status_code not in (200 , ):
155166 raise BadPasteID ("This is an invalid Mystb.in paste ID." )
167+
156168 paste_data = response .json ()
157169 return PasteData (paste_id , paste_data )
158170
159171 async def _perform_async_get (self , paste_id : str , syntax : str = None ) -> PasteData :
160172 """ Async get request. """
161173 if not self .session :
162174 self .session : aiohttp .ClientSession = await self ._generate_async_session ()
175+
163176 async with self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )) as response :
164177 if response .status not in (200 , ):
165178 raise BadPasteID ("This is an invalid Mystb.in paste ID." )
166179 paste_data = await response .json ()
180+
167181 return PasteData (paste_id , paste_data )
168182
169183 async def close (self ):
0 commit comments