@@ -56,42 +56,17 @@ class HTTPClient:
5656 def __init__ (
5757 self ,
5858 * ,
59- api_key : str = None ,
59+ api_key : str = "" ,
6060 session : Optional [Union [aiohttp .ClientSession , requests .Session ]] = None ,
6161 ) -> None :
6262 self .api_key = api_key
6363 self ._are_we_async = session is None or isinstance (
6464 session , aiohttp .ClientSession
6565 )
6666 self .session = (
67- self ._generate_sync_session (session ) if not self ._are_we_async else None
67+ self ._generate_sync_session (session ) if not self ._are_we_async else session
6868 )
6969
70- def _generate_sync_session (self , session : requests .Session ) -> requests .Session :
71- """ We will update a :class:`requests.Session` instance with the auth we require. """
72- # the passed session was found to be 'sync'.
73- if self .api_key :
74- session .headers .update (
75- {"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" }
76- )
77-
78- return session
79-
80- async def _generate_async_session (
81- self , session : Optional [aiohttp .ClientSession ] = None
82- ) -> aiohttp .ClientSession :
83- """ We will update (or create) a :class:`aiohttp.ClientSession` instance with the auth we require. """
84- if not session :
85- session = aiohttp .ClientSession (raise_for_status = False )
86-
87- if self .api_key :
88- session ._default_headers .update (
89- {"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" }
90- )
91-
92- session ._timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )
93- return session
94-
9570 def post (self , content : str , syntax : str = None ) -> Union [Paste , Awaitable ]:
9671 """
9772 This will post to the Mystb.in API and return the url.
@@ -118,6 +93,7 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
11893 "meta" : (None , json .dumps (payload ), "application/json" ),
11994 },
12095 timeout = CLIENT_TIMEOUT ,
96+ headers = {"Authorization" : self .api_key }
12197 )
12298
12399 if response .status_code not in [200 , 201 ]:
@@ -138,7 +114,12 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
138114 )
139115 paste_content .set_content_disposition ("form-data" , name = "meta" )
140116
141- async with self .session .post (API_BASE_URL , data = multi_part_write ) as response :
117+ async with self .session .post (
118+ API_BASE_URL ,
119+ data = multi_part_write ,
120+ timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT ),
121+ headers = {"Authorization" : self .api_key },
122+ ) as response :
142123 status_code = response .status
143124 response_text = await response .text ()
144125 if status_code not in (200 ,):
@@ -163,14 +144,13 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
163144 raise BadPasteID ("This is an invalid Mystb.in paste ID." )
164145
165146 paste_id = paste_id_match .group ("ID" )
166- syntax = paste_id_match .group ("syntax" )
167147
168148 if not self ._are_we_async :
169- return self ._perform_sync_get (paste_id , syntax )
149+ return self ._perform_sync_get (paste_id )
170150
171- return self ._perform_async_get (paste_id , syntax )
151+ return self ._perform_async_get (paste_id )
172152
173- def _perform_sync_get (self , paste_id : str , syntax : str = None ) -> PasteData :
153+ def _perform_sync_get (self , paste_id : str ) -> PasteData :
174154 """ Sync get request. """
175155 response : requests .Response = self .session .get (
176156 f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT
@@ -182,7 +162,7 @@ def _perform_sync_get(self, paste_id: str, syntax: str = None) -> PasteData:
182162 paste_data = response .json ()
183163 return PasteData (paste_id , paste_data )
184164
185- async def _perform_async_get (self , paste_id : str , syntax : str = None ) -> PasteData :
165+ async def _perform_async_get (self , paste_id : str ) -> PasteData :
186166 """ Async get request. """
187167 if not self .session :
188168 self .session : aiohttp .ClientSession = await self ._generate_async_session ()
0 commit comments