Skip to content

Commit a93cb6a

Browse files
authored
Guess content type (#347)
* Guess content type * Fixed tests * Fixed typo * Fixed test * Updated comment * update comment * Updated phpunit coverage filter
1 parent 83caa62 commit a93cb6a

File tree

2 files changed

+4
-27
lines changed

2 files changed

+4
-27
lines changed

src/Exception/Http/HttpExceptionTrait.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,11 @@ public function __construct(ResponseInterface $response)
4343
$this->response = $response;
4444
$code = $response->getInfo('http_code');
4545
$url = $response->getInfo('url');
46+
$content = $response->getContent(false);
4647
$message = sprintf('HTTP %d returned for "%s".', $code, $url);
4748

48-
$httpCodeFound = false;
49-
$isJson = false;
50-
foreach (array_reverse($response->getInfo('response_headers')) as $h) {
51-
if (0 === strpos($h, 'HTTP/')) {
52-
if ($httpCodeFound) {
53-
break;
54-
}
55-
56-
$message = sprintf('%s returned for "%s".', $h, $url);
57-
$httpCodeFound = true;
58-
}
59-
60-
if (0 === stripos($h, 'content-type:')) {
61-
if (preg_match('/\bjson\b/i', $h)) {
62-
$isJson = true;
63-
}
64-
65-
if ($httpCodeFound) {
66-
break;
67-
}
68-
}
69-
}
70-
71-
$content = $response->getContent(false);
72-
if ($isJson && $body = json_decode($content, true)) {
49+
// Try json_decode it first, fallback to XML
50+
if ($body = json_decode($content, true)) {
7351
$this->parseJson($body);
7452
} else {
7553
try {

src/Result.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use AsyncAws\Core\Exception\Http\NetworkException;
99

1010
/**
11-
* The result promise is always returned from every API call. Remember to call `resolve()` to
12-
* make sure the request is actually sent.
11+
* Base class for all return values from a Api Client methods. Example: `FooClient::bar(): Result`.
1312
*/
1413
class Result
1514
{

0 commit comments

Comments
 (0)