Skip to content

Commit c77debe

Browse files
Alvaro Navarroalnacle
authored andcommitted
Avoid parsing body if response is 204. Fixes #109
1 parent 76438ae commit c77debe

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

amadeus/mixins/parser.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def error_for(self, status_code, parsed): # noqa: C901
2626
return NotFoundError
2727
if status_code >= 400:
2828
return ClientError
29+
if status_code == 204:
30+
return None
2931
if not parsed:
3032
return ParserError
3133

@@ -44,7 +46,14 @@ def _parse_data(self, client):
4446
self.result = None
4547
self.headers = {}
4648

47-
self.__parse_body_and_headers(self.http_response, client)
49+
self.__parse_headers(self.http_response, client)
50+
51+
# Avoid parsing body in 204 responses
52+
if self.status_code == 204:
53+
return
54+
55+
self.__parse_body(self.http_response, client)
56+
4857
self.result = self.__parse_json(client)
4958
if (self.result is not None):
5059
self.data = self.result.get('data', None)
@@ -57,12 +66,14 @@ def __raise_error(self, error_class, client):
5766
error._log(client)
5867
raise error
5968

60-
# Extract the body and headers
61-
def __parse_body_and_headers(self, http_response, client):
69+
def __parse_headers(self, http_response, client):
6270
if hasattr(http_response, 'getheaders'):
6371
self.headers = dict(http_response.getheaders()) or self.headers
6472
if hasattr(http_response, 'info'):
6573
self.headers = http_response.info() or self.headers
74+
75+
# Extract the body and headers
76+
def __parse_body(self, http_response, client):
6677
if hasattr(http_response, 'read'):
6778
self.body = http_response.read()
6879
if hasattr(self.body, 'decode'):

specs/mixins/parser_spec.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@
4040
response._parse(self.client)
4141
expect(response.data).to(equal({'a': 1}))
4242

43+
with it('should not parse body if status code equal to 204'):
44+
with Stub() as http_response:
45+
http_response.status_code = '204'
46+
http_response.getheaders().returns(
47+
[('Content-Type', 'application/vnd.amadeus+json')]
48+
)
49+
50+
response = Response(http_response, self.request)
51+
response._parse(self.client)
52+
expect(response.parsed).to(equal(False))
53+
expect(response.body).to(equal(None))
54+
expect(response.result).to(be(None))
55+
expect(response.data).to(be(None))
56+
4357
with it('should parse no body for other content types'):
4458
with Stub() as http_response:
4559
http_response.status_code = '200'
@@ -128,3 +142,10 @@
128142
expect(lambda: self.response._detect_error(self.client)).to(
129143
raise_error(ParserError)
130144
)
145+
146+
with it('should not raise parse error for 204 responses'):
147+
self.response.status_code = 204
148+
self.response.parsed = False
149+
expect(lambda: self.response._detect_error(self.client)).not_to(
150+
raise_error(ParserError)
151+
)

0 commit comments

Comments
 (0)