@@ -70,15 +70,18 @@ def _generate_sync_session(self, session: Type["requests.Session"]) -> Type["req
70
70
if self .api_key :
71
71
session .headers .update (
72
72
{"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" })
73
+
73
74
return session
74
75
75
76
async def _generate_async_session (self , session : Optional [aiohttp .ClientSession ] = None ) -> aiohttp .ClientSession :
76
77
""" We will update (or create) a :class:`aiohttp.ClientSession` instance with the auth we require. """
77
78
if not session :
78
79
session = aiohttp .ClientSession (raise_for_status = False )
80
+
79
81
if self .api_key :
80
82
session ._default_headers .update (
81
83
{"Authorization" : self .api_key , "User-Agent" : "Mystbin.py" })
84
+
82
85
session ._timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )
83
86
return session
84
87
@@ -103,6 +106,7 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
103
106
payload = {'meta' : [{'index' : 0 , 'syntax' : syntax }]}
104
107
response : Type ["requests.Response" ] = self .session .post (API_BASE_URL , files = {
105
108
'data' : content , 'meta' : (None , json .dumps (payload ), 'application/json' )}, timeout = CLIENT_TIMEOUT )
109
+
106
110
if response .status_code not in [200 , 201 ]:
107
111
raise APIError (response .status_code , response .text )
108
112
@@ -112,13 +116,15 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
112
116
""" Async post request. """
113
117
if not self .session and self ._are_we_async :
114
118
self .session = await self ._generate_async_session ()
119
+
115
120
multi_part_write = aiohttp .MultipartWriter ()
116
121
paste_content = multi_part_write .append (content )
117
122
paste_content .set_content_disposition ("form-data" , name = "data" )
118
123
paste_content = multi_part_write .append_json (
119
124
{'meta' : [{'index' : 0 , 'syntax' : syntax }]}
120
125
)
121
126
paste_content .set_content_disposition ("form-data" , name = "meta" )
127
+
122
128
async with self .session .post (API_BASE_URL , data = multi_part_write ) as response :
123
129
status_code = response .status
124
130
response_text = await response .text ()
@@ -139,31 +145,39 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
139
145
The ID of the paste you are going to retrieve.
140
146
"""
141
147
paste_id_match = MB_URL_RE .match (paste_id )
148
+
142
149
if not paste_id_match :
143
150
raise BadPasteID ("This is an invalid Mystb.in paste ID." )
151
+
144
152
paste_id = paste_id_match .group ('ID' )
145
153
syntax = paste_id_match .group ('syntax' )
154
+
146
155
if not self ._are_we_async :
147
156
return self ._perform_sync_get (paste_id , syntax )
157
+
148
158
return self ._perform_async_get (paste_id , syntax )
149
159
150
160
def _perform_sync_get (self , paste_id : str , syntax : str = None ) -> PasteData :
151
161
""" Sync get request. """
152
162
response : Type ["requests.Response" ] = self .session .get (
153
163
f"{ API_BASE_URL } /{ paste_id } " , timeout = CLIENT_TIMEOUT )
164
+
154
165
if response .status_code not in (200 , ):
155
166
raise BadPasteID ("This is an invalid Mystb.in paste ID." )
167
+
156
168
paste_data = response .json ()
157
169
return PasteData (paste_id , paste_data )
158
170
159
171
async def _perform_async_get (self , paste_id : str , syntax : str = None ) -> PasteData :
160
172
""" Async get request. """
161
173
if not self .session :
162
174
self .session : aiohttp .ClientSession = await self ._generate_async_session ()
175
+
163
176
async with self .session .get (f"{ API_BASE_URL } /{ paste_id } " , timeout = aiohttp .ClientTimeout (CLIENT_TIMEOUT )) as response :
164
177
if response .status not in (200 , ):
165
178
raise BadPasteID ("This is an invalid Mystb.in paste ID." )
166
179
paste_data = await response .json ()
180
+
167
181
return PasteData (paste_id , paste_data )
168
182
169
183
async def close (self ):
0 commit comments