Skip to content

Commit 8352537

Browse files
committed
Handle non-OK, non-JSON responses
1 parent 9aa32dd commit 8352537

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

src/pipedream/proxy/raw_client.py

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,22 @@ def get(
105105
return HttpResponse(response=_stream_context, data=_create_stream_iterator(_response, _stream_context, _chunk_size))
106106
_stream_context.read()
107107
_response.__exit__(None, None, None)
108+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
108109
if _stream_context.status_code == 429:
109110
raise TooManyRequestsError(
110111
headers=dict(_stream_context.headers),
111112
body=typing.cast(
112113
typing.Optional[typing.Any],
113114
parse_obj_as(
114115
type_=typing.Optional[typing.Any], # type: ignore
115-
object_=_stream_context.json(),
116+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
116117
),
117118
),
118119
)
119-
_response_json = _stream_context.json()
120+
if "application/json" in _error_content_type:
121+
_response_body = _stream_context.json()
122+
else:
123+
_response_body = _stream_context.text
120124
except JSONDecodeError:
121125
_response.__exit__(None, None, None)
122126
raise ApiError(
@@ -125,7 +129,7 @@ def get(
125129
except Exception:
126130
_response.__exit__(None, None, None)
127131
raise
128-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
132+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
129133

130134
def post(
131135
self,
@@ -202,18 +206,22 @@ def post(
202206
return HttpResponse(response=_stream_context, data=_create_stream_iterator(_response, _stream_context, _chunk_size))
203207
_stream_context.read()
204208
_response.__exit__(None, None, None)
209+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
205210
if _stream_context.status_code == 429:
206211
raise TooManyRequestsError(
207212
headers=dict(_stream_context.headers),
208213
body=typing.cast(
209214
typing.Optional[typing.Any],
210215
parse_obj_as(
211216
type_=typing.Optional[typing.Any], # type: ignore
212-
object_=_stream_context.json(),
217+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
213218
),
214219
),
215220
)
216-
_response_json = _stream_context.json()
221+
if "application/json" in _error_content_type:
222+
_response_body = _stream_context.json()
223+
else:
224+
_response_body = _stream_context.text
217225
except JSONDecodeError:
218226
_response.__exit__(None, None, None)
219227
raise ApiError(
@@ -222,7 +230,7 @@ def post(
222230
except Exception:
223231
_response.__exit__(None, None, None)
224232
raise
225-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
233+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
226234

227235
def put(
228236
self,
@@ -299,18 +307,22 @@ def put(
299307
return HttpResponse(response=_stream_context, data=_create_stream_iterator(_response, _stream_context, _chunk_size))
300308
_stream_context.read()
301309
_response.__exit__(None, None, None)
310+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
302311
if _stream_context.status_code == 429:
303312
raise TooManyRequestsError(
304313
headers=dict(_stream_context.headers),
305314
body=typing.cast(
306315
typing.Optional[typing.Any],
307316
parse_obj_as(
308317
type_=typing.Optional[typing.Any], # type: ignore
309-
object_=_stream_context.json(),
318+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
310319
),
311320
),
312321
)
313-
_response_json = _stream_context.json()
322+
if "application/json" in _error_content_type:
323+
_response_body = _stream_context.json()
324+
else:
325+
_response_body = _stream_context.text
314326
except JSONDecodeError:
315327
_response.__exit__(None, None, None)
316328
raise ApiError(
@@ -319,7 +331,7 @@ def put(
319331
except Exception:
320332
_response.__exit__(None, None, None)
321333
raise
322-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
334+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
323335

324336
def delete(
325337
self,
@@ -388,18 +400,22 @@ def delete(
388400
return HttpResponse(response=_stream_context, data=_create_stream_iterator(_response, _stream_context, _chunk_size))
389401
_stream_context.read()
390402
_response.__exit__(None, None, None)
403+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
391404
if _stream_context.status_code == 429:
392405
raise TooManyRequestsError(
393406
headers=dict(_stream_context.headers),
394407
body=typing.cast(
395408
typing.Optional[typing.Any],
396409
parse_obj_as(
397410
type_=typing.Optional[typing.Any], # type: ignore
398-
object_=_stream_context.json(),
411+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
399412
),
400413
),
401414
)
402-
_response_json = _stream_context.json()
415+
if "application/json" in _error_content_type:
416+
_response_body = _stream_context.json()
417+
else:
418+
_response_body = _stream_context.text
403419
except JSONDecodeError:
404420
_response.__exit__(None, None, None)
405421
raise ApiError(
@@ -408,7 +424,7 @@ def delete(
408424
except Exception:
409425
_response.__exit__(None, None, None)
410426
raise
411-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
427+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
412428

413429
def patch(
414430
self,
@@ -485,18 +501,22 @@ def patch(
485501
return HttpResponse(response=_stream_context, data=_create_stream_iterator(_response, _stream_context, _chunk_size))
486502
_stream_context.read()
487503
_response.__exit__(None, None, None)
504+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
488505
if _stream_context.status_code == 429:
489506
raise TooManyRequestsError(
490507
headers=dict(_stream_context.headers),
491508
body=typing.cast(
492509
typing.Optional[typing.Any],
493510
parse_obj_as(
494511
type_=typing.Optional[typing.Any], # type: ignore
495-
object_=_stream_context.json(),
512+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
496513
),
497514
),
498515
)
499-
_response_json = _stream_context.json()
516+
if "application/json" in _error_content_type:
517+
_response_body = _stream_context.json()
518+
else:
519+
_response_body = _stream_context.text
500520
except JSONDecodeError:
501521
_response.__exit__(None, None, None)
502522
raise ApiError(
@@ -505,7 +525,7 @@ def patch(
505525
except Exception:
506526
_response.__exit__(None, None, None)
507527
raise
508-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
528+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
509529

510530

511531
class AsyncRawProxyClient:
@@ -579,18 +599,22 @@ async def get(
579599
return AsyncHttpResponse(response=_stream_context, data=_create_async_stream_iterator(_response, _stream_context, _chunk_size))
580600
await _stream_context.aread()
581601
await _response.__aexit__(None, None, None)
602+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
582603
if _stream_context.status_code == 429:
583604
raise TooManyRequestsError(
584605
headers=dict(_stream_context.headers),
585606
body=typing.cast(
586607
typing.Optional[typing.Any],
587608
parse_obj_as(
588609
type_=typing.Optional[typing.Any], # type: ignore
589-
object_=_stream_context.json(),
610+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
590611
),
591612
),
592613
)
593-
_response_json = _stream_context.json()
614+
if "application/json" in _error_content_type:
615+
_response_body = _stream_context.json()
616+
else:
617+
_response_body = _stream_context.text
594618
except JSONDecodeError:
595619
await _response.__aexit__(None, None, None)
596620
raise ApiError(
@@ -599,7 +623,7 @@ async def get(
599623
except Exception:
600624
await _response.__aexit__(None, None, None)
601625
raise
602-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
626+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
603627

604628
async def post(
605629
self,
@@ -676,18 +700,22 @@ async def post(
676700
return AsyncHttpResponse(response=_stream_context, data=_create_async_stream_iterator(_response, _stream_context, _chunk_size))
677701
await _stream_context.aread()
678702
await _response.__aexit__(None, None, None)
703+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
679704
if _stream_context.status_code == 429:
680705
raise TooManyRequestsError(
681706
headers=dict(_stream_context.headers),
682707
body=typing.cast(
683708
typing.Optional[typing.Any],
684709
parse_obj_as(
685710
type_=typing.Optional[typing.Any], # type: ignore
686-
object_=_stream_context.json(),
711+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
687712
),
688713
),
689714
)
690-
_response_json = _stream_context.json()
715+
if "application/json" in _error_content_type:
716+
_response_body = _stream_context.json()
717+
else:
718+
_response_body = _stream_context.text
691719
except JSONDecodeError:
692720
await _response.__aexit__(None, None, None)
693721
raise ApiError(
@@ -696,7 +724,7 @@ async def post(
696724
except Exception:
697725
await _response.__aexit__(None, None, None)
698726
raise
699-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
727+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
700728

701729
async def put(
702730
self,
@@ -773,18 +801,22 @@ async def put(
773801
return AsyncHttpResponse(response=_stream_context, data=_create_async_stream_iterator(_response, _stream_context, _chunk_size))
774802
await _stream_context.aread()
775803
await _response.__aexit__(None, None, None)
804+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
776805
if _stream_context.status_code == 429:
777806
raise TooManyRequestsError(
778807
headers=dict(_stream_context.headers),
779808
body=typing.cast(
780809
typing.Optional[typing.Any],
781810
parse_obj_as(
782811
type_=typing.Optional[typing.Any], # type: ignore
783-
object_=_stream_context.json(),
812+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
784813
),
785814
),
786815
)
787-
_response_json = _stream_context.json()
816+
if "application/json" in _error_content_type:
817+
_response_body = _stream_context.json()
818+
else:
819+
_response_body = _stream_context.text
788820
except JSONDecodeError:
789821
await _response.__aexit__(None, None, None)
790822
raise ApiError(
@@ -793,7 +825,7 @@ async def put(
793825
except Exception:
794826
await _response.__aexit__(None, None, None)
795827
raise
796-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
828+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
797829

798830
async def delete(
799831
self,
@@ -862,18 +894,22 @@ async def delete(
862894
return AsyncHttpResponse(response=_stream_context, data=_create_async_stream_iterator(_response, _stream_context, _chunk_size))
863895
await _stream_context.aread()
864896
await _response.__aexit__(None, None, None)
897+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
865898
if _stream_context.status_code == 429:
866899
raise TooManyRequestsError(
867900
headers=dict(_stream_context.headers),
868901
body=typing.cast(
869902
typing.Optional[typing.Any],
870903
parse_obj_as(
871904
type_=typing.Optional[typing.Any], # type: ignore
872-
object_=_stream_context.json(),
905+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
873906
),
874907
),
875908
)
876-
_response_json = _stream_context.json()
909+
if "application/json" in _error_content_type:
910+
_response_body = _stream_context.json()
911+
else:
912+
_response_body = _stream_context.text
877913
except JSONDecodeError:
878914
await _response.__aexit__(None, None, None)
879915
raise ApiError(
@@ -882,7 +918,7 @@ async def delete(
882918
except Exception:
883919
await _response.__aexit__(None, None, None)
884920
raise
885-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
921+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)
886922

887923
async def patch(
888924
self,
@@ -959,18 +995,22 @@ async def patch(
959995
return AsyncHttpResponse(response=_stream_context, data=_create_async_stream_iterator(_response, _stream_context, _chunk_size))
960996
await _stream_context.aread()
961997
await _response.__aexit__(None, None, None)
998+
_error_content_type = _stream_context.headers.get("content-type", "").lower()
962999
if _stream_context.status_code == 429:
9631000
raise TooManyRequestsError(
9641001
headers=dict(_stream_context.headers),
9651002
body=typing.cast(
9661003
typing.Optional[typing.Any],
9671004
parse_obj_as(
9681005
type_=typing.Optional[typing.Any], # type: ignore
969-
object_=_stream_context.json(),
1006+
object_=_stream_context.json() if "application/json" in _error_content_type else _stream_context.text,
9701007
),
9711008
),
9721009
)
973-
_response_json = _stream_context.json()
1010+
if "application/json" in _error_content_type:
1011+
_response_body = _stream_context.json()
1012+
else:
1013+
_response_body = _stream_context.text
9741014
except JSONDecodeError:
9751015
await _response.__aexit__(None, None, None)
9761016
raise ApiError(
@@ -979,4 +1019,4 @@ async def patch(
9791019
except Exception:
9801020
await _response.__aexit__(None, None, None)
9811021
raise
982-
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_json)
1022+
raise ApiError(status_code=_stream_context.status_code, headers=dict(_stream_context.headers), body=_response_body)

0 commit comments

Comments
 (0)