Skip to content

Commit 63d2b7d

Browse files
author
Martin Brecht-Precht
committed
Extracted the endpoint handling to the common PHP Url Util project.
1 parent fff4736 commit 63d2b7d

10 files changed

+63
-253
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,23 @@ $message->addHeader(new Header('Custom-Header', array('CustomHeaderValue')));
216216
$message->addAdditionalHeader(new Header('Custom-Header', array('AnotherCustomHeaderValue')));
217217
```
218218

219-
#### Configuring a Request instance and perform the HTTP request
219+
#### Configuring an endpoints URL, build the Request instance and perform the HTTP request
220+
221+
For more information about the usage of the URL object please take a look at the [PHP URL project](https://github.com/markenwerk/php-url-util).
220222

221223
```{php}
222224
use BasicHttpClient\Request\Authentication\BasicAuthentication;
223225
use BasicHttpClient\Request\Request;
226+
use Url\Url;
227+
228+
// Setting up the endpoints URL
229+
$url = new Url('https://john:[email protected]:443/path/to/resource?arg1=123#fragment');
224230
225231
// Configuring and performing a Request
226232
$request = new Request();
227233
$request
228234
->setUserAgent('PHP Basic HTTP Client Test 1.0')
229-
->setEndpoint('https://yourapihere-com-98yq3775xff0.runscope.net/')
230-
->setPort(443)
235+
->setUrl($url)
231236
->addAuthentication(new BasicAuthentication('username', 'password'))
232237
->setQueryParameters(
233238
array(
@@ -244,7 +249,7 @@ $request
244249
The resulting HTTP request would be the following.
245250

246251
```{http}
247-
POST /?paramName1=paramValue1&paramName2=paramValue2 HTTP/1.1
252+
POST /?arg1=123#fragment HTTP/1.1
248253
Host: yourapihere-com-98yq3775xff0.runscope.net
249254
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
250255
User-Agent: PHP Basic HTTP Client Test 1.0

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"require": {
2828
"php": ">=5.3",
2929
"lib-curl": "*",
30+
"markenwerk/url-util": "~1.0",
3031
"markenwerk/common-exceptions": "~2.0"
3132
},
3233
"require-dev": {

detaild.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BasicHttpClient\Request\Message\Message;
1010
use BasicHttpClient\Request\Request;
1111
use BasicHttpClient\Request\Transport\HttpsTransport;
12+
use Url\Url;
1213

1314
require_once('vendor/autoload.php');
1415

@@ -38,29 +39,22 @@
3839

3940
$message = new Message();
4041
$message
41-
->addHeader(new Header('Content-Type', array('application/json')))
42-
->addHeader(new Header('Accept', array('application/json', 'text/*')))
43-
->addHeader(new Header('Runscope-Bucket-Auth', array('7a64dde7-74d5-4eed-b170-a2ab406eff08')))
42+
->setHeader(new Header('Content-Type', array('application/json')))
43+
->setHeader(new Header('Accept', array('application/json', 'text/*')))
44+
->setHeader(new Header('Runscope-Bucket-Auth', array('7a64dde7-74d5-4eed-b170-a2ab406eff08')))
4445
->addCookie(new Cookie('PHPSESSID', '<MY_SESSION_ID>'))
4546
->setBody($messageBody);
4647

47-
$message->addHeader(new Header('Custom-Header', array('CustomHeaderValue')));
48-
$message->addAdditionalHeader(new Header('Custom-Header', array('AnotherCustomHeaderValue')));
48+
$message->setHeader(new Header('Custom-Header', array('CustomHeaderValue')));
49+
$message->addHeader(new Header('Custom-Header', array('AnotherCustomHeaderValue')));
50+
51+
$url = new Url('https://john:[email protected]:443/path/to/resource?arg1=123#fragment');
4952

5053
$request = new Request();
5154
$request
5255
->setUserAgent('PHP Basic HTTP Client Test 1.0')
53-
->setEndpoint('https://yourapihere-com-98yq3775xff0.runscope.net/')
54-
->setPort(443)
56+
->setUrl($url)
5557
->addAuthentication(new BasicAuthentication('username', 'password'))
56-
->setQueryParameters(
57-
array(
58-
'paramName1' => 'paramValue1',
59-
'paramName2' => 'paramValue2',
60-
'paramName3' => true,
61-
'paramName4' => 42,
62-
)
63-
)
6458
->setMethod(Request::REQUEST_METHOD_POST)
6559
->setTransport($transport)
6660
->setMessage($message)
@@ -85,5 +79,5 @@
8579
echo print_r($request->getEffectiveEndpoint(), true) . PHP_EOL;
8680
echo print_r($request->getEffectiveStatus(), true) . PHP_EOL;
8781
echo print_r($request->getEffectiveRawHeader(), true) . PHP_EOL;
88-
echo print_r($request->getEffectiveHeaders(), true) . PHP_EOL.PHP_EOL;
82+
echo print_r($request->getEffectiveHeaders(), true) . PHP_EOL . PHP_EOL;
8983

src/BasicHttpClient.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use BasicHttpClient\Request\Transport\HttpsTransport;
1010
use BasicHttpClient\Request\Transport\HttpTransport;
1111
use BasicHttpClient\Response\ResponseInterface;
12-
use BasicHttpClient\Util\UrlUtil;
12+
use Url\Url;
1313

1414
/**
1515
* Class BasicHttpClient
@@ -35,16 +35,16 @@ public function __construct($endpoint)
3535
$argumentType = (is_object($endpoint)) ? get_class($endpoint) : gettype($endpoint);
3636
throw new \InvalidArgumentException('Expected the endpoint as string. Got ' . $argumentType);
3737
}
38-
$urlUtil = new UrlUtil();
38+
$url = new Url($endpoint);
3939
$transport = new HttpTransport();
40-
if ($urlUtil->getScheme($endpoint) == 'HTTPS') {
40+
if (mb_strtoupper($url->getScheme()) == 'HTTPS') {
4141
$transport = new HttpsTransport();
4242
}
4343
$this->request = new Request();
4444
$this->request
4545
->setTransport($transport)
4646
->setMessage(new Message())
47-
->setEndpoint($endpoint);
47+
->setUrl(new Url($endpoint));
4848
}
4949

5050
/**
@@ -65,8 +65,9 @@ public function get(array $queryParameters = null)
6565
{
6666
$this->request
6767
->setMethod(RequestInterface::REQUEST_METHOD_GET)
68-
->setQueryParameters($queryParameters)
69-
->perform();
68+
->getUrl()
69+
->setQueryParametersFromArray($queryParameters);
70+
$this->request->perform();
7071
return $this->request->getResponse();
7172
}
7273

@@ -80,8 +81,9 @@ public function head(array $queryParameters = null)
8081
{
8182
$this->request
8283
->setMethod(RequestInterface::REQUEST_METHOD_HEAD)
83-
->setQueryParameters($queryParameters)
84-
->perform();
84+
->getUrl()
85+
->setQueryParametersFromArray($queryParameters);
86+
$this->request->perform();
8587
return $this->request->getResponse();
8688
}
8789

@@ -152,8 +154,9 @@ public function delete(array $queryParameters = null)
152154
{
153155
$this->request
154156
->setMethod(RequestInterface::REQUEST_METHOD_DELETE)
155-
->setQueryParameters($queryParameters)
156-
->perform();
157+
->getUrl()
158+
->setQueryParametersFromArray($queryParameters);
159+
$this->request->perform();
157160
return $this->request->getResponse();
158161
}
159162

src/Request/AbstractRequest.php

+16-117
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
use BasicHttpClient\Request\Transport\HttpsTransport;
1111
use BasicHttpClient\Request\Transport\HttpTransport;
1212
use BasicHttpClient\Response\ResponseInterface;
13-
use BasicHttpClient\Util\UrlUtil;
1413
use CommonException\NetworkException\Base\NetworkException;
1514
use CommonException\NetworkException\ConnectionTimeoutException;
1615
use CommonException\NetworkException\CurlException;
16+
use Url\UrlInterface;
1717

1818
/**
1919
* Class Request
@@ -28,25 +28,15 @@ abstract class AbstractRequest implements RequestInterface
2828
*/
2929
private $userAgent = 'PHP Basic HTTP Client 1.0';
3030

31-
/**
32-
* @var string
33-
*/
34-
private $endpoint;
35-
36-
/**
37-
* @var int
38-
*/
39-
private $port;
40-
4131
/**
4232
* @var string
4333
*/
4434
private $method = self::REQUEST_METHOD_GET;
4535

4636
/**
47-
* @var string[]
37+
* @var UrlInterface
4838
*/
49-
private $queryParameters = array();
39+
private $url;
5040

5141
/**
5242
* @var TransportInterface
@@ -114,54 +104,6 @@ public function setUserAgent($userAgent)
114104
return $this;
115105
}
116106

117-
/**
118-
* @return string
119-
*/
120-
public function getEndpoint()
121-
{
122-
return $this->endpoint;
123-
}
124-
125-
/**
126-
* @param string $endpoint
127-
* @return $this
128-
*/
129-
public function setEndpoint($endpoint)
130-
{
131-
$urlUtil = new UrlUtil();
132-
if (!$urlUtil->validateUrl($endpoint)) {
133-
throw new \InvalidArgumentException('The given endpoint is not a valid URL');
134-
}
135-
$this->endpoint = $endpoint;
136-
return $this;
137-
}
138-
139-
/**
140-
* @return int
141-
*/
142-
public function getPort()
143-
{
144-
return $this->port;
145-
}
146-
147-
/**
148-
* @return bool
149-
*/
150-
public function hasPort()
151-
{
152-
return !is_null($this->port);
153-
}
154-
155-
/**
156-
* @param int $port
157-
* @return $this
158-
*/
159-
public function setPort($port)
160-
{
161-
$this->port = $port;
162-
return $this;
163-
}
164-
165107
/**
166108
* @return string
167109
*/
@@ -181,60 +123,20 @@ public function setMethod($method)
181123
}
182124

183125
/**
184-
* @return string[]
185-
*/
186-
public function getQueryParameters()
187-
{
188-
return $this->queryParameters;
189-
}
190-
191-
/**
192-
* @return bool
193-
*/
194-
public function hasQueryParameters()
195-
{
196-
return count($this->queryParameters) > 0;
197-
}
198-
199-
/**
200-
* @param string $parameterName
201-
* @param string $parameterValue
202-
* @return $this
203-
*/
204-
public function addQueryParameter($parameterName, $parameterValue)
205-
{
206-
if (!is_string($parameterName) || !is_string($parameterValue)) {
207-
throw new \InvalidArgumentException('Query parameter names and values have to be a string.');
208-
}
209-
$this->queryParameters[$parameterName] = $parameterValue;
210-
return $this;
211-
}
212-
213-
/**
214-
* @param string[] $queryParameters
215-
* @return $this
126+
* @return UrlInterface
216127
*/
217-
public function setQueryParameters($queryParameters)
128+
public function getUrl()
218129
{
219-
foreach ($queryParameters as &$queryParameter) {
220-
if (!is_scalar($queryParameter)) {
221-
$argumentType = (is_object($queryParameter)) ? get_class($queryParameter) : gettype($queryParameter);
222-
throw new \InvalidArgumentException(
223-
'Expected the query parameters as array of scalar values. Got ' . $argumentType
224-
);
225-
}
226-
$queryParameter = (string)$queryParameter;
227-
}
228-
$this->queryParameters = $queryParameters;
229-
return $this;
130+
return $this->url;
230131
}
231132

232133
/**
134+
* @param UrlInterface $url
233135
* @return $this
234136
*/
235-
public function removeQueryParameters()
137+
public function setUrl(UrlInterface $url)
236138
{
237-
$this->queryParameters = array();
139+
$this->url = $url;
238140
return $this;
239141
}
240142

@@ -364,8 +266,8 @@ public function configureCurl($curl)
364266
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
365267
curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent());
366268
curl_setopt($curl, CURLOPT_URL, $this->calculateEndpoint());
367-
if ($this->hasPort()) {
368-
curl_setopt($curl, CURLOPT_PORT, $this->getPort());
269+
if ($this->getUrl()->hasPort()) {
270+
curl_setopt($curl, CURLOPT_PORT, $this->getUrl()->getPort());
369271
}
370272
// Request method
371273
curl_setopt($curl, CURLOPT_HTTPGET, true);
@@ -469,12 +371,7 @@ public function getEffectiveHeaders()
469371
*/
470372
protected function calculateEndpoint()
471373
{
472-
$endpoint = $this->getEndpoint();
473-
if ($this->hasQueryParameters()) {
474-
$glueCharacter = (strpos($endpoint, '?') === false) ? '?' : '&';
475-
$endpoint .= $glueCharacter . http_build_query($this->getQueryParameters());
476-
}
477-
return $endpoint;
374+
return $this->getUrl()->buildUrl();
478375
}
479376

480377
/**
@@ -483,8 +380,10 @@ protected function calculateEndpoint()
483380
*/
484381
protected function prePerform()
485382
{
486-
$urlUtil = new UrlUtil();
487-
if ($urlUtil->getScheme($this->getEndpoint()) == 'HTTPS' && !$this->getTransport() instanceof HttpsTransport) {
383+
if (
384+
mb_strtoupper($this->getUrl()->getScheme()) == 'HTTPS'
385+
&& !$this->getTransport() instanceof HttpsTransport
386+
) {
488387
throw new HttpRequestException('Transport misconfiguration. Use HttpsTransport for HTTPS requests.');
489388
}
490389
}

src/Request/Message/Message.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function setHeaders(array $headers)
9696
* @param HeaderInterface $header
9797
* @return $this
9898
*/
99-
public function addAdditionalHeader(HeaderInterface $header)
99+
public function addHeader(HeaderInterface $header)
100100
{
101101
$this->headers[] = $header;
102102
return $this;
@@ -106,7 +106,7 @@ public function addAdditionalHeader(HeaderInterface $header)
106106
* @param HeaderInterface $header
107107
* @return $this
108108
*/
109-
public function addHeader(HeaderInterface $header)
109+
public function setHeader(HeaderInterface $header)
110110
{
111111
$this->removeHeadersByName($header->getName());
112112
$this->headers[] = $header;

src/Request/Message/MessageInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public function setHeaders(array $headers);
4747
* @param HeaderInterface $header
4848
* @return $this
4949
*/
50-
public function addAdditionalHeader(HeaderInterface $header);
50+
public function addHeader(HeaderInterface $header);
5151

5252
/**
5353
* @param HeaderInterface $header
5454
* @return $this
5555
*/
56-
public function addHeader(HeaderInterface $header);
56+
public function setHeader(HeaderInterface $header);
5757

5858
/**
5959
* @param string $name

0 commit comments

Comments
 (0)