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