Skip to content

Commit daf7537

Browse files
committed
This is where it all begins...
1 parent 834b426 commit daf7537

33 files changed

+2218
-0
lines changed

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "lessmore92/api-consumer",
3+
"description": "Build REST API consumer easier than ever",
4+
"type": "package",
5+
"require": {
6+
"php": ">=5.5"
7+
},
8+
"license": "mit",
9+
"authors": [
10+
{
11+
"name": "Mojtaba Bahrami",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"minimum-stability": "dev",
16+
"autoload": {
17+
"psr-4": {
18+
"Lessmore92\\ApiConsumer\\": "src/"
19+
}
20+
}
21+
}

src/ApiConsumer.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* User: Lessmore92
4+
* Date: 12/12/2019
5+
* Time: 11:47 PM
6+
*/
7+
8+
namespace Lessmore92\ApiConsumer;
9+
10+
use Lessmore92\ApiConsumer\Builders\ApiBuilder;
11+
use Lessmore92\ApiConsumer\Builders\RequestBuilder;
12+
use Lessmore92\ApiConsumer\Builders\ResponseBuilder;
13+
use Lessmore92\ApiConsumer\Contracts\HttpClientInterface;
14+
use Lessmore92\ApiConsumer\Exceptions\ConfigApiNotReturnApiBuilder;
15+
use Lessmore92\ApiConsumer\Foundation\RequestDirector;
16+
use Lessmore92\ApiConsumer\HttpClients\Curl;
17+
use Lessmore92\ApiConsumer\Models\Api;
18+
19+
abstract class ApiConsumer
20+
{
21+
/**
22+
* @var Api
23+
*/
24+
protected $api;
25+
/**
26+
* @var HttpClientInterface
27+
*/
28+
protected $http;
29+
/**
30+
* @var RequestBuilder
31+
*/
32+
protected $request_builder;
33+
/**
34+
* @var ResponseBuilder
35+
*/
36+
protected $response_builder;
37+
38+
/**
39+
* @var RequestDirector
40+
*/
41+
private $request_director;
42+
43+
/**
44+
* ApiConsumer constructor.
45+
* @param HttpClientInterface|null $httpClient
46+
*/
47+
public function __construct(HttpClientInterface $httpClient = null)
48+
{
49+
if ($httpClient === null)
50+
{
51+
$httpClient = new Curl();
52+
}
53+
$this->http = $httpClient;
54+
$this->request_builder = new RequestBuilder();
55+
$this->response_builder = new ResponseBuilder();
56+
57+
$api = $this->ConfigApi();
58+
59+
if (!($api instanceof ApiBuilder))
60+
{
61+
throw new ConfigApiNotReturnApiBuilder('ConfigApi() must return an instance of \'Lessmore92\ApiConsumer\Builders\ApiBuilder\'');
62+
}
63+
64+
$this->api = $api->getApi();
65+
66+
$this->request_builder->setApi($this->api);
67+
68+
$this->request_director = new RequestDirector($this->request_builder, $this->response_builder, $httpClient);
69+
}
70+
71+
/**
72+
* @return ApiBuilder
73+
*/
74+
abstract protected function ConfigApi();
75+
76+
/**
77+
* @return RequestDirector
78+
*/
79+
public function Request()
80+
{
81+
return $this->request_director;
82+
}
83+
}

src/Builders/ApiBuilder.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* User: Lessmore92
4+
* Date: 12/13/2019
5+
* Time: 3:21 PM
6+
*/
7+
8+
namespace Lessmore92\ApiConsumer\Builders;
9+
10+
use Lessmore92\ApiConsumer\Contracts\AbstractApiBuilder;
11+
use Lessmore92\ApiConsumer\Exceptions\ApiKeyParamNameMustBeDefine;
12+
use Lessmore92\ApiConsumer\Models\Api;
13+
14+
class ApiBuilder extends AbstractApiBuilder
15+
{
16+
private $api;
17+
18+
public function __construct()
19+
{
20+
$this->api = new Api();
21+
}
22+
23+
/**
24+
* @return Api
25+
*/
26+
public function getApi()
27+
{
28+
return $this->api;
29+
}
30+
31+
/**
32+
* @param string $api_key
33+
* @param string $api_key_param_name
34+
* @return $this
35+
*/
36+
public function setHeaderApiKey($api_key, $api_key_param_name = 'x-api-key')
37+
{
38+
return $this->setApiKey($api_key, $api_key_param_name, 'header');
39+
}
40+
41+
private function setApiKey($api_key, $api_key_param_name = 'x-api-key', $api_key_place = 'header')
42+
{
43+
if (trim($api_key_param_name) === '')
44+
{
45+
throw new ApiKeyParamNameMustBeDefine('api_key_param_name must be defined, empty string not allowed');
46+
}
47+
$this->api->api_key_place = $api_key_place;
48+
$this->api->api_key_param_name = $api_key_param_name;
49+
$this->api->api_key = $api_key;
50+
return $this;
51+
}
52+
53+
/**
54+
* @param string $api_key
55+
* @param string $api_key_param_name
56+
* @return $this
57+
*/
58+
public function setQueryApiKey($api_key, $api_key_param_name = 'api_key')
59+
{
60+
return $this->setApiKey($api_key, $api_key_param_name, 'query_string');
61+
}
62+
63+
/**
64+
* @param string $url
65+
* @return $this
66+
*/
67+
public function setBaseUrl($url)
68+
{
69+
$this->api->base_url = $url;
70+
return $this;
71+
}
72+
}

src/Builders/RequestBuilder.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* User: Lessmore92
4+
* Date: 12/13/2019
5+
* Time: 3:21 PM
6+
*/
7+
8+
namespace Lessmore92\ApiConsumer\Builders;
9+
10+
11+
use Lessmore92\ApiConsumer\Contracts\AbstractRequestBuilder;
12+
use Lessmore92\ApiConsumer\Exceptions\RequestMethodNotSupported;
13+
use Lessmore92\ApiConsumer\Models\Api;
14+
use Lessmore92\ApiConsumer\Models\Request;
15+
16+
class RequestBuilder extends AbstractRequestBuilder
17+
{
18+
/**
19+
* @var Api
20+
*/
21+
private $api;
22+
23+
/**
24+
* @var Request
25+
*/
26+
private $request;
27+
28+
/**
29+
* RequestBuilder constructor.
30+
* @param Api|null $api
31+
*/
32+
public function __construct(Api $api = null)
33+
{
34+
$this->request = new Request();
35+
$this->api = $api;
36+
37+
$this->init();
38+
}
39+
40+
private function init()
41+
{
42+
if ($this->api === null)
43+
{
44+
return;
45+
}
46+
47+
if ($this->api->base_url !== '')
48+
{
49+
$this->request->url = $this->api->base_url;
50+
}
51+
52+
if ($this->api->api_key !== '')
53+
{
54+
if ($this->api->api_key_place === Api::API_KEY_IN_QUERY_STRING)
55+
{
56+
$this->request->addQueryString($this->api->api_key_param_name, $this->api->api_key);
57+
}
58+
else if ($this->api->api_key_place === Api::API_KEY_IN_HEADER)
59+
{
60+
$this->request->addHeader($this->api->api_key_param_name, $this->api->api_key);
61+
}
62+
//TODO throw exception api key place not valid
63+
}
64+
}
65+
66+
public function setApi($api)
67+
{
68+
$this->api = $api;
69+
$this->init();
70+
}
71+
72+
/**
73+
* @return Request
74+
*/
75+
public function buildRequest()
76+
{
77+
$request = unserialize(serialize($this->request));
78+
$this->reset();
79+
return $request;
80+
}
81+
82+
private function reset()
83+
{
84+
$this->endPoint('');
85+
$this->setBody('');
86+
$this->request->clearOnetimeHeaders();
87+
$this->request->clearOnetimeQueryStrings();
88+
}
89+
90+
/**
91+
* @param string $endpoint
92+
* @return RequestBuilder
93+
*/
94+
public function endPoint($endpoint)
95+
{
96+
$this->request->path = $endpoint;
97+
return $this;
98+
}
99+
100+
/**
101+
* @param string $body
102+
* @return RequestBuilder
103+
*/
104+
public function setBody($body)
105+
{
106+
$this->request->is_json = false;
107+
$this->request->body = $body;
108+
return $this;
109+
}
110+
111+
/**
112+
* @param array $body
113+
* @return RequestBuilder
114+
*/
115+
public function setJsonBody($body)
116+
{
117+
$this->request->is_json = true;
118+
$this->request->json_body = $body;
119+
return $this;
120+
}
121+
122+
/**
123+
*
124+
* @param string $key
125+
* @param string $value
126+
* @param bool $onetime if true header remove after buildRequest() called
127+
* @return RequestBuilder
128+
*/
129+
public function addHeader($key, $value, $onetime = false)
130+
{
131+
if ($onetime)
132+
{
133+
$this->request->addOnetimeHeader($key, $value);
134+
}
135+
else
136+
{
137+
$this->request->addHeader($key, $value);
138+
}
139+
return $this;
140+
}
141+
142+
/**
143+
*
144+
* @param string $key
145+
* @param bool $onetime if true header removed from onetime headers
146+
* @return RequestBuilder
147+
*/
148+
public function removeHeader($key, $onetime = false)
149+
{
150+
if ($onetime)
151+
{
152+
$this->request->removeOnetimeHeader($key);
153+
}
154+
else
155+
{
156+
$this->request->removeHeader($key);
157+
}
158+
return $this;
159+
}
160+
161+
/**
162+
* @param string $key
163+
* @param string $value
164+
* @param bool $onetime if true query_string remove after buildRequest() called
165+
* @return RequestBuilder
166+
*/
167+
public function addQueryString($key, $value, $onetime = false)
168+
{
169+
if ($onetime)
170+
{
171+
$this->request->addOnetimeQueryString($key, $value);
172+
}
173+
else
174+
{
175+
$this->request->addQueryString($key, $value);
176+
}
177+
return $this;
178+
}
179+
180+
/**
181+
*
182+
* @param string $key
183+
* @param bool $onetime if true query_string removed from onetime query_strings
184+
* @return RequestBuilder
185+
*/
186+
public function removeQueryString($key, $onetime = false)
187+
{
188+
if ($onetime)
189+
{
190+
$this->request->removeOnetimeQueryString($key);
191+
}
192+
else
193+
{
194+
$this->request->removeQueryString($key);
195+
}
196+
return $this;
197+
}
198+
199+
/**
200+
* @param string $method
201+
* @return RequestBuilder
202+
*/
203+
public function setMethod($method)
204+
{
205+
if (!defined('\Lessmore92\ApiConsumer\Contracts\RequestModelInterface::REQUEST_METHOD_' . strtoupper($method)))
206+
{
207+
throw new RequestMethodNotSupported(sprintf('Request method %s not supported.', $method));
208+
}
209+
$this->request->method = $method;
210+
return $this;
211+
}
212+
}

0 commit comments

Comments
 (0)