@@ -87,7 +87,12 @@ def try_shorten(self, url, custom=None, key=None):
8787 return url
8888
8989 def expand (self , url ):
90- r = requests .get (url , allow_redirects = False )
90+ try :
91+ r = requests .get (url , allow_redirects = False )
92+ r .raise_for_status ()
93+ except RequestException as e :
94+ r = e .response
95+ raise ServiceError (r .status_code , r )
9196
9297 if 'location' in r .headers :
9398 return r .headers ['location' ]
@@ -127,7 +132,13 @@ def _decorate(impl):
127132class Isgd (Shortener ):
128133 def shorten (self , url , custom = None , key = None ):
129134 p = {'url' : url , 'shorturl' : custom , 'format' : 'json' }
130- r = requests .get ('http://is.gd/create.php' , params = p )
135+ try :
136+ r = requests .get ('http://is.gd/create.php' , params = p )
137+ r .raise_for_status ()
138+ except RequestException as e :
139+ r = e .response
140+ raise ServiceError (r .status_code , r )
141+
131142 j = r .json ()
132143
133144 if 'shorturl' in j :
@@ -137,7 +148,13 @@ def shorten(self, url, custom=None, key=None):
137148
138149 def expand (self , url ):
139150 p = {'shorturl' : url , 'format' : 'json' }
140- r = requests .get ('http://is.gd/forward.php' , params = p )
151+ try :
152+ r = requests .get ('http://is.gd/forward.php' , params = p )
153+ r .raise_for_status ()
154+ except RequestException as e :
155+ r = e .response
156+ raise ServiceError (r .status_code , r )
157+
141158 j = r .json ()
142159
143160 if 'url' in j :
@@ -152,7 +169,13 @@ def shorten(self, url, custom=None, key=None):
152169 h = {'content-type' : 'application/json' }
153170 k = {'key' : key }
154171 p = {'longUrl' : url }
155- r = requests .post ('https://www.googleapis.com/urlshortener/v1/url' , params = k , data = json .dumps (p ), headers = h )
172+ try :
173+ r = requests .post ('https://www.googleapis.com/urlshortener/v1/url' , params = k , data = json .dumps (p ), headers = h )
174+ r .raise_for_status ()
175+ except RequestException as e :
176+ r = e .response
177+ raise ServiceError (r .status_code , r )
178+
156179 j = r .json ()
157180
158181 if 'error' not in j :
@@ -162,7 +185,13 @@ def shorten(self, url, custom=None, key=None):
162185
163186 def expand (self , url ):
164187 p = {'shortUrl' : url }
165- r = requests .get ('https://www.googleapis.com/urlshortener/v1/url' , params = p )
188+ try :
189+ r = requests .get ('https://www.googleapis.com/urlshortener/v1/url' , params = p )
190+ r .raise_for_status ()
191+ except RequestException as e :
192+ r = e .response
193+ raise ServiceError (r .status_code , r )
194+
166195 j = r .json ()
167196
168197 if 'error' not in j :
@@ -175,7 +204,12 @@ def expand(self, url):
175204class Gitio (Shortener ):
176205 def shorten (self , url , custom = None , key = None ):
177206 p = {'url' : url , 'code' : custom }
178- r = requests .post ('http://git.io' , data = p )
207+ try :
208+ r = requests .post ('http://git.io' , data = p )
209+ r .raise_for_status ()
210+ except RequestException as e :
211+ r = e .response
212+ raise ServiceError (r .status_code , r )
179213
180214 if r .status_code == requests .codes .created :
181215 s = r .headers ['location' ]
@@ -190,13 +224,19 @@ def shorten(self, url, custom=None, key=None):
190224@_pastebin ('hastebin' )
191225class Hastebin (Pastebin ):
192226 def paste (self , data , ext ):
193- r = requests .post (HASTEBIN_SERVER + '/documents' , data = data )
194- j = r .json ()
195-
196- if r .status_code is requests .codes .ok :
197- return '{}/{}.{}' .format (HASTEBIN_SERVER , j ['key' ], ext )
227+ try :
228+ r = requests .post (HASTEBIN_SERVER + '/documents' , data = data )
229+ r .raise_for_status ()
230+ except RequestException as e :
231+ r = e .response
232+ raise ServiceError (r .status_code , r )
198233 else :
199- raise ServiceError (j ['message' ], r )
234+ j = r .json ()
235+
236+ if r .status_code is requests .codes .ok :
237+ return '{}/{}.{}' .format (HASTEBIN_SERVER , j ['key' ], ext )
238+ else :
239+ raise ServiceError (j ['message' ], r )
200240
201241
202242@_pastebin ('snoonet' )
@@ -211,6 +251,6 @@ def paste(self, data, ext):
211251 r .raise_for_status ()
212252 except RequestException as e :
213253 r = e .response
214- return ServiceError (r .status_code , r )
254+ raise ServiceError (r .status_code , r )
215255 else :
216256 return '{}' .format (r .url )
0 commit comments