@@ -56,42 +56,17 @@ class HTTPClient:
56
56
def __init__ (
57
57
self ,
58
58
* ,
59
- api_key : str = None ,
59
+ api_key : str = "" ,
60
60
session : Optional [Union [aiohttp .ClientSession , requests .Session ]] = None ,
61
61
) -> None :
62
62
self .api_key = api_key
63
63
self ._are_we_async = session is None or isinstance (
64
64
session , aiohttp .ClientSession
65
65
)
66
66
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
68
68
)
69
69
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
-
95
70
def post (self , content : str , syntax : str = None ) -> Union [Paste , Awaitable ]:
96
71
"""
97
72
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:
118
93
"meta" : (None , json .dumps (payload ), "application/json" ),
119
94
},
120
95
timeout = CLIENT_TIMEOUT ,
96
+ headers = {"Authorization" : self .api_key }
121
97
)
122
98
123
99
if response .status_code not in [200 , 201 ]:
@@ -138,7 +114,12 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
138
114
)
139
115
paste_content .set_content_disposition ("form-data" , name = "meta" )
140
116
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 :
142
123
status_code = response .status
143
124
response_text = await response .text ()
144
125
if status_code not in (200 ,):
@@ -163,14 +144,13 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
163
144
raise BadPasteID ("This is an invalid Mystb.in paste ID." )
164
145
165
146
paste_id = paste_id_match .group ("ID" )
166
- syntax = paste_id_match .group ("syntax" )
167
147
168
148
if not self ._are_we_async :
169
- return self ._perform_sync_get (paste_id , syntax )
149
+ return self ._perform_sync_get (paste_id )
170
150
171
- return self ._perform_async_get (paste_id , syntax )
151
+ return self ._perform_async_get (paste_id )
172
152
173
- def _perform_sync_get (self , paste_id : str , syntax : str = None ) -> PasteData :
153
+ def _perform_sync_get (self , paste_id : str ) -> PasteData :
174
154
""" Sync get request. """
175
155
response : requests .Response = self .session .get (
176
156
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:
182
162
paste_data = response .json ()
183
163
return PasteData (paste_id , paste_data )
184
164
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 :
186
166
""" Async get request. """
187
167
if not self .session :
188
168
self .session : aiohttp .ClientSession = await self ._generate_async_session ()
0 commit comments