HttpClient.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace AliyunMNS\Http;
use AliyunMNS\Config;
use AliyunMNS\Constants;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\BaseRequest;
use AliyunMNS\Responses\BaseResponse;
use AliyunMNS\Signature\Signature;
use AliyunMNS\AsyncCallback;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\TransferException;
use AliyunMNS\Responses\MnsPromise;
class HttpClient
{
private $client;
private $region;
private $accountId;
private $accessId;
private $accessKey;
private $securityToken;
private $requestTimeout;
private $connectTimeout;
public function __construct($endPoint, $accessId,
$accessKey, $securityToken = NULL, Config $config = NULL)
{
if ($config == NULL)
{
$config = new Config;
}
$this->accessId = $accessId;
$this->accessKey = $accessKey;
$this->client = new \GuzzleHttp\Client([
'base_uri' => $endPoint,
'defaults' => [
'headers' => [
'Host' => $endPoint
],
'proxy' => $config->getProxy(),
'expect' => $config->getExpectContinue()
]
]);
$this->requestTimeout = $config->getRequestTimeout();
$this->connectTimeout = $config->getConnectTimeout();
$this->securityToken = $securityToken;
$this->endpoint = $endPoint;
$this->parseEndpoint();
}
public function getRegion()
{
return $this->region;
}
public function getAccountId()
{
return $this->accountId;
}
// This function is for SDK internal use
private function parseEndpoint()
{
$pieces = explode("//", $this->endpoint);
$host = end($pieces);
$host_pieces = explode(".", $host);
$this->accountId = $host_pieces[0];
$region_pieces = explode("-internal", $host_pieces[2]);
$this->region = $region_pieces[0];
}
private function addRequiredHeaders(BaseRequest &$request)
{
$body = $request->generateBody();
$queryString = $request->generateQueryString();
$request->setBody($body);
$request->setQueryString($queryString);
if ($body != NULL)
{
$request->setHeader(Constants::CONTENT_LENGTH, strlen($body));
}
$request->setHeader('Date', gmdate(Constants::GMT_DATE_FORMAT));
if (!$request->isHeaderSet(Constants::CONTENT_TYPE))
{
$request->setHeader(Constants::CONTENT_TYPE, 'text/xml');
}
$request->setHeader(Constants::MNS_VERSION_HEADER, Constants::MNS_VERSION);
if ($this->securityToken != NULL)
{
$request->setHeader(Constants::SECURITY_TOKEN, $this->securityToken);
}
$sign = Signature::SignRequest($this->accessKey, $request);
$request->setHeader(Constants::AUTHORIZATION,
Constants::MNS . " " . $this->accessId . ":" . $sign);
}
public function sendRequestAsync(BaseRequest $request,
BaseResponse &$response, AsyncCallback $callback = NULL)
{
$promise = $this->sendRequestAsyncInternal($request, $response, $callback);
return new MnsPromise($promise, $response);
}
public function sendRequest(BaseRequest $request, BaseResponse &$response)
{
$promise = $this->sendRequestAsync($request, $response);
return $promise->wait();
}
private function sendRequestAsyncInternal(BaseRequest &$request, BaseResponse &$response, AsyncCallback $callback = NULL)
{
$this->addRequiredHeaders($request);
$parameters = array('exceptions' => false, 'http_errors' => false);
$queryString = $request->getQueryString();
$body = $request->getBody();
if ($queryString != NULL) {
$parameters['query'] = $queryString;
}
if ($body != NULL) {
$parameters['body'] = $body;
}
$parameters['timeout'] = $this->requestTimeout;
$parameters['connect_timeout'] = $this->connectTimeout;
$request = new Request(strtoupper($request->getMethod()),
$request->getResourcePath(), $request->getHeaders());
try
{
if ($callback != NULL)
{
return $this->client->sendAsync($request, $parameters)->then(
function ($res) use (&$response, $callback) {
try {
$response->parseResponse($res->getStatusCode(), $res->getBody());
$callback->onSucceed($response);
} catch (MnsException $e) {
$callback->onFailed($e);
}
}
);
}
else
{
return $this->client->sendAsync($request, $parameters);
}
}
catch (TransferException $e)
{
$message = $e->getMessage();
if ($e->hasResponse()) {
$message = $e->getResponse()->getBody();
}
throw new MnsException($e->getCode(), $message, $e);
}
}
}
?>