|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Bridge for the Content API. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\BridgeBundle\Client; |
|
16
|
|
|
|
|
17
|
|
|
use GuzzleHttp\Client as BaseClient; |
|
18
|
|
|
use GuzzleHttp\Exception\ClientException as GuzzleClientException; |
|
19
|
|
|
use GuzzleHttp\Exception\ServerException as GuzzleServerException; |
|
20
|
|
|
use Superdesk\ContentApiSdk\Client\ClientInterface; |
|
21
|
|
|
use Superdesk\ContentApiSdk\Exception\ClientException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Request service that implements all method regarding basic request/response |
|
25
|
|
|
* handling. |
|
26
|
|
|
*/ |
|
27
|
|
|
class GuzzleClient extends BaseClient implements ClientInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function makeCall( |
|
33
|
|
|
$url, |
|
34
|
|
|
array $headers = [], |
|
35
|
|
|
array $options = [], |
|
36
|
|
|
$method = 'GET', |
|
37
|
|
|
$content = null |
|
38
|
|
|
) { |
|
39
|
|
|
$options['headers'] = $headers; |
|
40
|
|
|
|
|
41
|
|
|
if (in_array($method, ['POST'])) { |
|
42
|
|
|
$options['body'] = $content; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
$response = $this->request($method, $url, $options); |
|
47
|
|
|
} catch (GuzzleClientException $e) { |
|
48
|
|
|
// This is for 400 errors |
|
49
|
|
|
$response = $e->getResponse(); |
|
50
|
|
|
} catch (GuzzleServerException $e) { |
|
51
|
|
|
// This is for 500 errors |
|
52
|
|
|
$response = $e->getResponse(); |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
// Any other errors should trigger an exception |
|
55
|
|
|
throw new ClientException($e->getMessage(), $e->getCode(), $e); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$responseArray = [ |
|
59
|
|
|
'headers' => $response->getHeaders(), |
|
60
|
|
|
'status' => $response->getStatusCode(), |
|
61
|
|
|
'body' => (string) $response->getBody(), |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
return $responseArray; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|