elmage /
textng-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Elmage\TextNg; |
||
| 4 | |||
| 5 | use Elmage\TextNg\Authentication\ApiKeyAuthentication; |
||
| 6 | use Elmage\TextNg\Enum\Param; |
||
| 7 | use Elmage\TextNg\Exception\HttpException; |
||
| 8 | use GuzzleHttp\ClientInterface; |
||
| 9 | use GuzzleHttp\Exception\RequestException; |
||
| 10 | use GuzzleHttp\Psr7\Request; |
||
| 11 | use GuzzleHttp\RequestOptions; |
||
| 12 | use Psr\Http\Message\RequestInterface; |
||
| 13 | use Psr\Http\Message\ResponseInterface; |
||
| 14 | use Psr\Log\LoggerInterface; |
||
| 15 | |||
| 16 | class HttpClient |
||
| 17 | { |
||
| 18 | private $apiUrl; |
||
| 19 | private $apiVersion; |
||
| 20 | private $auth; |
||
| 21 | private $transport; |
||
| 22 | |||
| 23 | /** @var string Text Message Sender */ |
||
| 24 | private $sender; |
||
| 25 | |||
| 26 | /** @var LoggerInterface */ |
||
| 27 | private $logger; |
||
| 28 | |||
| 29 | /** @var RequestInterface */ |
||
| 30 | private $lastRequest; |
||
| 31 | |||
| 32 | /** @var ResponseInterface */ |
||
| 33 | private $lastResponse; |
||
| 34 | |||
| 35 | 8 | public function __construct( |
|
| 36 | string $apiUrl, |
||
| 37 | string $apiVersion, |
||
| 38 | ApiKeyAuthentication $auth, |
||
| 39 | string $sender, |
||
| 40 | ClientInterface $transport |
||
| 41 | ) { |
||
| 42 | 8 | $this->apiUrl = rtrim($apiUrl, '/'); |
|
| 43 | 8 | $this->apiVersion = $apiVersion; |
|
| 44 | 8 | $this->auth = $auth; |
|
| 45 | 8 | $this->sender = $sender; |
|
| 46 | 8 | $this->transport = $transport; |
|
| 47 | 8 | } |
|
| 48 | |||
| 49 | public function getLogger() |
||
| 50 | { |
||
| 51 | return $this->logger; |
||
| 52 | } |
||
| 53 | |||
| 54 | 2 | public function setLogger(LoggerInterface $logger = null) |
|
| 55 | { |
||
| 56 | 2 | $this->logger = $logger; |
|
| 57 | 2 | } |
|
| 58 | |||
| 59 | 1 | public function getSender() |
|
| 60 | { |
||
| 61 | 1 | return $this->sender; |
|
| 62 | } |
||
| 63 | |||
| 64 | 1 | public function setSender(string $sender) |
|
| 65 | { |
||
| 66 | 1 | $this->sender = $sender; |
|
| 67 | 1 | } |
|
| 68 | |||
| 69 | /** @return ResponseInterface */ |
||
| 70 | 2 | public function get($path, array $params = array()): ResponseInterface |
|
| 71 | { |
||
| 72 | 2 | return $this->request('GET', $path, $params); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** @return ResponseInterface */ |
||
| 76 | 2 | public function post($path, array $params = array()): ResponseInterface |
|
| 77 | { |
||
| 78 | 2 | return $this->request('POST', $path, $params); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** @return ResponseInterface */ |
||
| 82 | 1 | public function put($path, array $params = array()): ResponseInterface |
|
| 83 | { |
||
| 84 | 1 | return $this->request('PUT', $path, $params); |
|
| 85 | } |
||
| 86 | |||
| 87 | /** @return ResponseInterface */ |
||
| 88 | 1 | public function delete($path, array $params = array()): ResponseInterface |
|
| 89 | { |
||
| 90 | 1 | return $this->request('DELETE', $path, $params); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $method http request method |
||
| 95 | * @param string $path url path |
||
| 96 | * @param array $params request parameters |
||
| 97 | */ |
||
| 98 | 6 | private function request(string $method, string $path, array $params = array()): ResponseInterface |
|
| 99 | { |
||
| 100 | 6 | $method = strtoupper($method); |
|
| 101 | |||
| 102 | 6 | $params[Param::API_KEY] = $this->auth->getApiKey(); |
|
| 103 | |||
| 104 | 6 | if ($method === 'GET') { |
|
| 105 | 2 | $path = $this->prepareQueryString($path, $params); |
|
| 106 | } |
||
| 107 | |||
| 108 | 6 | $request = new Request($method, $this->prepareUrl($path)); |
|
| 109 | |||
| 110 | 6 | return $this->send($request, $params); |
|
| 111 | } |
||
| 112 | |||
| 113 | 6 | private function send(RequestInterface $request, array $params = array()): ResponseInterface |
|
| 114 | { |
||
| 115 | 6 | $this->lastRequest = $request; |
|
| 116 | |||
| 117 | 6 | $options = $this->prepareOptions( |
|
| 118 | 6 | $params |
|
| 119 | ); |
||
| 120 | |||
| 121 | try { |
||
| 122 | 6 | $this->lastResponse = $response = $this->transport->send($request, $options); |
|
| 123 | } catch (RequestException $e) { |
||
| 124 | throw HttpException::wrap($e); |
||
| 125 | } |
||
| 126 | |||
| 127 | 6 | if ($this->logger) { |
|
| 128 | //$this->logWarnings($response); TODO LOG warnings |
||
| 129 | } |
||
| 130 | |||
| 131 | 6 | return $response; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $path //request path |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 136 | * @param array $params pointer to an array of parameters |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 2 | private function prepareQueryString(string $path, array &$params = array()): string |
|
| 141 | { |
||
| 142 | 2 | if (count($params) < 1) { |
|
| 143 | return $path; |
||
| 144 | } |
||
| 145 | |||
| 146 | 2 | $path .= false === strpos($path, '?') ? '?' : '&'; |
|
| 147 | 2 | $path .= http_build_query($params, '', '&'); |
|
| 148 | |||
| 149 | 2 | return $path; |
|
| 150 | } |
||
| 151 | |||
| 152 | 6 | private function prepareOptions(array $params = array()) |
|
| 153 | { |
||
| 154 | 6 | $options = array(); |
|
| 155 | |||
| 156 | 6 | if (count($params) > 0) { |
|
| 157 | 6 | $options[RequestOptions::FORM_PARAMS] = $params; |
|
| 158 | 6 | $options[RequestOptions::VERIFY] = false; |
|
| 159 | 6 | $body = json_encode($params); |
|
|
0 ignored issues
–
show
|
|||
| 160 | } else { |
||
| 161 | $body = ''; |
||
| 162 | } |
||
| 163 | |||
| 164 | $defaultHeaders = array( |
||
| 165 | 6 | 'User-Agent' => 'elmage/textng/php/'.Client::VERSION, |
|
| 166 | 6 | 'Content-Type' => 'application/x-www-form-urlencoded', |
|
| 167 | 6 | 'Accept' => 'text/plain, application/json', 'text/html', |
|
| 168 | ); |
||
| 169 | |||
| 170 | 6 | $options[RequestOptions::HEADERS] = $defaultHeaders; |
|
| 171 | |||
| 172 | 6 | return $options; |
|
| 173 | } |
||
| 174 | |||
| 175 | 6 | private function prepareUrl(string $path): string |
|
| 176 | { |
||
| 177 | 6 | return $this->apiUrl.'/'.ltrim($path, '/'); |
|
| 178 | } |
||
| 179 | } |
||
| 180 |