Skip to content

Commit 31443d4

Browse files
author
Martin Brecht-Precht
committed
Extended the project by performing JSON requests and handle their responses.
1 parent aa2109e commit 31443d4

13 files changed

+981
-596
lines changed

src/BasicHttpClient.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @package BasicHttpClient
1818
*/
19-
class BasicHttpClient
19+
class BasicHttpClient implements HttpClientInterface
2020
{
2121

2222
/**
@@ -57,7 +57,7 @@ public function getRequest()
5757
public function get()
5858
{
5959
$this->request
60-
->setMethod(Request::REQUEST_METHOD_GET)
60+
->setMethod(RequestInterface::REQUEST_METHOD_GET)
6161
->perform();
6262
return $this->request->getResponse();
6363
}
@@ -68,7 +68,7 @@ public function get()
6868
public function head()
6969
{
7070
$this->request
71-
->setMethod(Request::REQUEST_METHOD_HEAD)
71+
->setMethod(RequestInterface::REQUEST_METHOD_HEAD)
7272
->perform();
7373
return $this->request->getResponse();
7474
}
@@ -85,7 +85,7 @@ public function post(array $postData)
8585
->getMessage()
8686
->setBody($body);
8787
$this->request
88-
->setMethod(Request::REQUEST_METHOD_POST)
88+
->setMethod(RequestInterface::REQUEST_METHOD_POST)
8989
->perform();
9090
return $this->request->getResponse();
9191
}
@@ -102,7 +102,7 @@ public function put(array $putData)
102102
->getMessage()
103103
->setBody($body);
104104
$this->request
105-
->setMethod(Request::REQUEST_METHOD_PUT)
105+
->setMethod(RequestInterface::REQUEST_METHOD_PUT)
106106
->perform();
107107
return $this->request->getResponse();
108108
}
@@ -119,7 +119,7 @@ public function patch(array $patchData)
119119
->getMessage()
120120
->setBody($body);
121121
$this->request
122-
->setMethod(Request::REQUEST_METHOD_PATCH)
122+
->setMethod(RequestInterface::REQUEST_METHOD_PATCH)
123123
->perform();
124124
return $this->request->getResponse();
125125
}
@@ -130,7 +130,7 @@ public function patch(array $patchData)
130130
public function delete()
131131
{
132132
$this->request
133-
->setMethod(Request::REQUEST_METHOD_DELETE)
133+
->setMethod(RequestInterface::REQUEST_METHOD_DELETE)
134134
->perform();
135135
return $this->request->getResponse();
136136
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Exception;
4+
5+
/**
6+
* Class HttpRequestMessageException
7+
*
8+
* @package BasicHttpClient\Exception
9+
*/
10+
class HttpRequestMessageException extends \Exception
11+
{
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Exception;
4+
5+
/**
6+
* Class HttpResponseException
7+
*
8+
* @package BasicHttpClient\Exception
9+
*/
10+
class HttpResponseException extends \Exception
11+
{
12+
13+
}

src/HttpClientInterface.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace BasicHttpClient;
4+
5+
use BasicHttpClient\Request\RequestInterface;
6+
use BasicHttpClient\Response\ResponseInterface;
7+
8+
/**
9+
* Interface HttpClientInterface
10+
*
11+
* @package BasicHttpClient
12+
*/
13+
interface HttpClientInterface
14+
{
15+
16+
/**
17+
* @return RequestInterface
18+
*/
19+
public function getRequest();
20+
21+
/**
22+
* @return ResponseInterface
23+
*/
24+
public function get();
25+
26+
/**
27+
* @return ResponseInterface
28+
*/
29+
public function head();
30+
31+
/**
32+
* @param array $postData
33+
* @return ResponseInterface
34+
*/
35+
public function post(array $postData);
36+
37+
/**
38+
* @param array $putData
39+
* @return ResponseInterface
40+
*/
41+
public function put(array $putData);
42+
43+
/**
44+
* @param array $patchData
45+
* @return ResponseInterface
46+
*/
47+
public function patch(array $patchData);
48+
49+
/**
50+
* @return ResponseInterface
51+
*/
52+
public function delete();
53+
54+
}

src/JsonHttpClient.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace BasicHttpClient;
4+
5+
use BasicHttpClient\Request\Message\Header\Header;
6+
use BasicHttpClient\Request\RequestInterface;
7+
use BasicHttpClient\Request\Message\Body\JsonBody;
8+
use BasicHttpClient\Request\Message\Message;
9+
use BasicHttpClient\Request\JsonRequest;
10+
use BasicHttpClient\Request\Transport\HttpsTransport;
11+
use BasicHttpClient\Request\Transport\HttpTransport;
12+
use BasicHttpClient\Response\ResponseInterface;
13+
use BasicHttpClient\Util\UrlUtil;
14+
15+
/**
16+
* Class JsonHttpClient
17+
*
18+
* @package BasicHttpClient
19+
*/
20+
class JsonHttpClient implements HttpClientInterface
21+
{
22+
23+
/**
24+
* @var RequestInterface
25+
*/
26+
private $request;
27+
28+
/**
29+
* BasicHttpClient constructor.
30+
*
31+
* @param string $endpoint
32+
*/
33+
public function __construct($endpoint)
34+
{
35+
$urlUtil = new UrlUtil();
36+
$transport = new HttpTransport();
37+
if ($urlUtil->getScheme($endpoint) == 'HTTPS') {
38+
$transport = new HttpsTransport();
39+
}
40+
$message = new Message();
41+
$message
42+
->addHeader(new Header('Accept', array('application/json')))
43+
->addHeader(new Header('Content-Type', array('application/json')));
44+
$this->request = new JsonRequest();
45+
$this->request
46+
->setTransport($transport)
47+
->setMessage($message)
48+
->setEndpoint($endpoint);
49+
}
50+
51+
/**
52+
* @return RequestInterface
53+
*/
54+
public function getRequest()
55+
{
56+
return $this->request;
57+
}
58+
59+
/**
60+
* @return ResponseInterface
61+
*/
62+
public function get()
63+
{
64+
$this->request
65+
->setMethod(RequestInterface::REQUEST_METHOD_GET)
66+
->perform();
67+
return $this->request->getResponse();
68+
}
69+
70+
/**
71+
* @return ResponseInterface
72+
*/
73+
public function head()
74+
{
75+
$this->request
76+
->setMethod(RequestInterface::REQUEST_METHOD_HEAD)
77+
->perform();
78+
return $this->request->getResponse();
79+
}
80+
81+
/**
82+
* @param array $postData
83+
* @return ResponseInterface
84+
*/
85+
public function post(array $postData)
86+
{
87+
$this->request
88+
->getMessage()
89+
->setBody(new JsonBody($postData));
90+
$this->request
91+
->setMethod(RequestInterface::REQUEST_METHOD_POST)
92+
->perform();
93+
return $this->request->getResponse();
94+
}
95+
96+
/**
97+
* @param array $putData
98+
* @return ResponseInterface
99+
*/
100+
public function put(array $putData)
101+
{
102+
$this->request
103+
->getMessage()
104+
->setBody(new JsonBody($putData));
105+
$this->request
106+
->setMethod(RequestInterface::REQUEST_METHOD_PUT)
107+
->perform();
108+
return $this->request->getResponse();
109+
}
110+
111+
/**
112+
* @param array $patchData
113+
* @return ResponseInterface
114+
*/
115+
public function patch(array $patchData)
116+
{
117+
$this->request
118+
->getMessage()
119+
->setBody(new JsonBody($patchData));
120+
$this->request
121+
->setMethod(RequestInterface::REQUEST_METHOD_PATCH)
122+
->perform();
123+
return $this->request->getResponse();
124+
}
125+
126+
/**
127+
* @return ResponseInterface
128+
*/
129+
public function delete()
130+
{
131+
$this->request
132+
->setMethod(RequestInterface::REQUEST_METHOD_DELETE)
133+
->perform();
134+
return $this->request->getResponse();
135+
}
136+
137+
}

0 commit comments

Comments
 (0)