|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package: chapi |
|
4
|
|
|
* |
|
5
|
|
|
* @author: msiebeneicher |
|
6
|
|
|
* @since: 2015-07-28 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Chapi\Component\Http; |
|
11
|
|
|
|
|
12
|
|
|
use Chapi\Exception\HttpConnectionException; |
|
13
|
|
|
use GuzzleHttp\ClientInterface; |
|
14
|
|
|
|
|
15
|
|
|
class HttpGuzzlClient implements HttpClientInterface |
|
16
|
|
|
{ |
|
17
|
|
|
const DEFAULT_CONNECTION_TIMEOUT = 5; |
|
18
|
|
|
const DEFAULT_TIMEOUT = 30; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClientInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $oGuzzelClient; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $aGuzzelClientConfig; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ClientInterface $oGuzzelClient |
|
32
|
|
|
* @param array $aGuzzelClientConfig |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct( |
|
35
|
|
|
ClientInterface $oGuzzelClient, |
|
36
|
|
|
array $aGuzzelClientConfig = [] |
|
37
|
|
|
) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->oGuzzelClient = $oGuzzelClient; |
|
40
|
7 |
|
$this->aGuzzelClientConfig = $aGuzzelClientConfig; |
|
41
|
7 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $sUrl |
|
45
|
|
|
* @return HttpClientResponseInterface |
|
46
|
|
|
* @throws HttpConnectionException |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function get($sUrl) |
|
49
|
|
|
{ |
|
50
|
|
|
$_aRequestOptions = [ |
|
51
|
3 |
|
'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT, |
|
52
|
|
|
'timeout' => self::DEFAULT_TIMEOUT |
|
53
|
3 |
|
]; |
|
54
|
3 |
|
$_aRequestOptions = $this->addAuthOption($_aRequestOptions); |
|
55
|
|
|
|
|
56
|
|
|
try |
|
57
|
|
|
{ |
|
58
|
3 |
|
$_oResponse = $this->oGuzzelClient->get($sUrl, $_aRequestOptions); |
|
59
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
|
60
|
|
|
} |
|
61
|
1 |
|
catch (\Exception $oException) |
|
62
|
|
|
{ |
|
63
|
1 |
|
throw new HttpConnectionException( |
|
64
|
1 |
|
sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getBaseUrl() . $sUrl), |
|
65
|
1 |
|
0, |
|
66
|
|
|
$oException |
|
67
|
1 |
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $sUrl |
|
73
|
|
|
* @param mixed $mPostData |
|
74
|
|
|
* @return HttpGuzzlResponse |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function postJsonData($sUrl, $mPostData) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$_aRequestOptions = ['json' => $mPostData]; |
|
79
|
2 |
|
$_aRequestOptions = $this->addAuthOption($_aRequestOptions); |
|
80
|
|
|
|
|
81
|
2 |
|
$_oRequest = $this->oGuzzelClient->createRequest('post', $sUrl, $_aRequestOptions); |
|
82
|
2 |
|
$_oResponse = $this->oGuzzelClient->send($_oRequest); |
|
83
|
|
|
|
|
84
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $sUrl |
|
89
|
|
|
* @return HttpGuzzlResponse |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function delete($sUrl) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$_aRequestOptions = []; |
|
94
|
2 |
|
$_aRequestOptions = $this->addAuthOption($_aRequestOptions); |
|
95
|
|
|
|
|
96
|
2 |
|
$_oResponse = $this->oGuzzelClient->delete($sUrl, $_aRequestOptions); |
|
97
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Adds authentication headers according the Guzzle http options. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $aOptions |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
7 |
|
private function addAuthOption(array $aOptions) { |
|
107
|
7 |
|
if (array_key_exists('username', $this->aGuzzelClientConfig) |
|
108
|
7 |
|
&& $this->aGuzzelClientConfig['username'] |
|
109
|
7 |
|
&& array_key_exists('password', $this->aGuzzelClientConfig) |
|
110
|
7 |
|
&& $this->aGuzzelClientConfig['password'] |
|
111
|
3 |
|
) |
|
112
|
7 |
|
{ |
|
113
|
3 |
|
$aOptions['auth'] = [ |
|
114
|
3 |
|
$this->aGuzzelClientConfig['username'], |
|
115
|
3 |
|
$this->aGuzzelClientConfig['password'] |
|
116
|
3 |
|
]; |
|
117
|
3 |
|
} |
|
118
|
|
|
|
|
119
|
7 |
|
return $aOptions; |
|
120
|
|
|
} |
|
121
|
|
|
} |