-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
834b426
commit daf7537
Showing
33 changed files
with
2,218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "lessmore92/api-consumer", | ||
"description": "Build REST API consumer easier than ever", | ||
"type": "package", | ||
"require": { | ||
"php": ">=5.5" | ||
}, | ||
"license": "mit", | ||
"authors": [ | ||
{ | ||
"name": "Mojtaba Bahrami", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"autoload": { | ||
"psr-4": { | ||
"Lessmore92\\ApiConsumer\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* User: Lessmore92 | ||
* Date: 12/12/2019 | ||
* Time: 11:47 PM | ||
*/ | ||
|
||
namespace Lessmore92\ApiConsumer; | ||
|
||
use Lessmore92\ApiConsumer\Builders\ApiBuilder; | ||
use Lessmore92\ApiConsumer\Builders\RequestBuilder; | ||
use Lessmore92\ApiConsumer\Builders\ResponseBuilder; | ||
use Lessmore92\ApiConsumer\Contracts\HttpClientInterface; | ||
use Lessmore92\ApiConsumer\Exceptions\ConfigApiNotReturnApiBuilder; | ||
use Lessmore92\ApiConsumer\Foundation\RequestDirector; | ||
use Lessmore92\ApiConsumer\HttpClients\Curl; | ||
use Lessmore92\ApiConsumer\Models\Api; | ||
|
||
abstract class ApiConsumer | ||
{ | ||
/** | ||
* @var Api | ||
*/ | ||
protected $api; | ||
/** | ||
* @var HttpClientInterface | ||
*/ | ||
protected $http; | ||
/** | ||
* @var RequestBuilder | ||
*/ | ||
protected $request_builder; | ||
/** | ||
* @var ResponseBuilder | ||
*/ | ||
protected $response_builder; | ||
|
||
/** | ||
* @var RequestDirector | ||
*/ | ||
private $request_director; | ||
|
||
/** | ||
* ApiConsumer constructor. | ||
* @param HttpClientInterface|null $httpClient | ||
*/ | ||
public function __construct(HttpClientInterface $httpClient = null) | ||
{ | ||
if ($httpClient === null) | ||
{ | ||
$httpClient = new Curl(); | ||
} | ||
$this->http = $httpClient; | ||
$this->request_builder = new RequestBuilder(); | ||
$this->response_builder = new ResponseBuilder(); | ||
|
||
$api = $this->ConfigApi(); | ||
|
||
if (!($api instanceof ApiBuilder)) | ||
{ | ||
throw new ConfigApiNotReturnApiBuilder('ConfigApi() must return an instance of \'Lessmore92\ApiConsumer\Builders\ApiBuilder\''); | ||
} | ||
|
||
$this->api = $api->getApi(); | ||
|
||
$this->request_builder->setApi($this->api); | ||
|
||
$this->request_director = new RequestDirector($this->request_builder, $this->response_builder, $httpClient); | ||
} | ||
|
||
/** | ||
* @return ApiBuilder | ||
*/ | ||
abstract protected function ConfigApi(); | ||
|
||
/** | ||
* @return RequestDirector | ||
*/ | ||
public function Request() | ||
{ | ||
return $this->request_director; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* User: Lessmore92 | ||
* Date: 12/13/2019 | ||
* Time: 3:21 PM | ||
*/ | ||
|
||
namespace Lessmore92\ApiConsumer\Builders; | ||
|
||
use Lessmore92\ApiConsumer\Contracts\AbstractApiBuilder; | ||
use Lessmore92\ApiConsumer\Exceptions\ApiKeyParamNameMustBeDefine; | ||
use Lessmore92\ApiConsumer\Models\Api; | ||
|
||
class ApiBuilder extends AbstractApiBuilder | ||
{ | ||
private $api; | ||
|
||
public function __construct() | ||
{ | ||
$this->api = new Api(); | ||
} | ||
|
||
/** | ||
* @return Api | ||
*/ | ||
public function getApi() | ||
{ | ||
return $this->api; | ||
} | ||
|
||
/** | ||
* @param string $api_key | ||
* @param string $api_key_param_name | ||
* @return $this | ||
*/ | ||
public function setHeaderApiKey($api_key, $api_key_param_name = 'x-api-key') | ||
{ | ||
return $this->setApiKey($api_key, $api_key_param_name, 'header'); | ||
} | ||
|
||
private function setApiKey($api_key, $api_key_param_name = 'x-api-key', $api_key_place = 'header') | ||
{ | ||
if (trim($api_key_param_name) === '') | ||
{ | ||
throw new ApiKeyParamNameMustBeDefine('api_key_param_name must be defined, empty string not allowed'); | ||
} | ||
$this->api->api_key_place = $api_key_place; | ||
$this->api->api_key_param_name = $api_key_param_name; | ||
$this->api->api_key = $api_key; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $api_key | ||
* @param string $api_key_param_name | ||
* @return $this | ||
*/ | ||
public function setQueryApiKey($api_key, $api_key_param_name = 'api_key') | ||
{ | ||
return $this->setApiKey($api_key, $api_key_param_name, 'query_string'); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return $this | ||
*/ | ||
public function setBaseUrl($url) | ||
{ | ||
$this->api->base_url = $url; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
<?php | ||
/** | ||
* User: Lessmore92 | ||
* Date: 12/13/2019 | ||
* Time: 3:21 PM | ||
*/ | ||
|
||
namespace Lessmore92\ApiConsumer\Builders; | ||
|
||
|
||
use Lessmore92\ApiConsumer\Contracts\AbstractRequestBuilder; | ||
use Lessmore92\ApiConsumer\Exceptions\RequestMethodNotSupported; | ||
use Lessmore92\ApiConsumer\Models\Api; | ||
use Lessmore92\ApiConsumer\Models\Request; | ||
|
||
class RequestBuilder extends AbstractRequestBuilder | ||
{ | ||
/** | ||
* @var Api | ||
*/ | ||
private $api; | ||
|
||
/** | ||
* @var Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* RequestBuilder constructor. | ||
* @param Api|null $api | ||
*/ | ||
public function __construct(Api $api = null) | ||
{ | ||
$this->request = new Request(); | ||
$this->api = $api; | ||
|
||
$this->init(); | ||
} | ||
|
||
private function init() | ||
{ | ||
if ($this->api === null) | ||
{ | ||
return; | ||
} | ||
|
||
if ($this->api->base_url !== '') | ||
{ | ||
$this->request->url = $this->api->base_url; | ||
} | ||
|
||
if ($this->api->api_key !== '') | ||
{ | ||
if ($this->api->api_key_place === Api::API_KEY_IN_QUERY_STRING) | ||
{ | ||
$this->request->addQueryString($this->api->api_key_param_name, $this->api->api_key); | ||
} | ||
else if ($this->api->api_key_place === Api::API_KEY_IN_HEADER) | ||
{ | ||
$this->request->addHeader($this->api->api_key_param_name, $this->api->api_key); | ||
} | ||
//TODO throw exception api key place not valid | ||
} | ||
} | ||
|
||
public function setApi($api) | ||
{ | ||
$this->api = $api; | ||
$this->init(); | ||
} | ||
|
||
/** | ||
* @return Request | ||
*/ | ||
public function buildRequest() | ||
{ | ||
$request = unserialize(serialize($this->request)); | ||
$this->reset(); | ||
return $request; | ||
} | ||
|
||
private function reset() | ||
{ | ||
$this->endPoint(''); | ||
$this->setBody(''); | ||
$this->request->clearOnetimeHeaders(); | ||
$this->request->clearOnetimeQueryStrings(); | ||
} | ||
|
||
/** | ||
* @param string $endpoint | ||
* @return RequestBuilder | ||
*/ | ||
public function endPoint($endpoint) | ||
{ | ||
$this->request->path = $endpoint; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $body | ||
* @return RequestBuilder | ||
*/ | ||
public function setBody($body) | ||
{ | ||
$this->request->is_json = false; | ||
$this->request->body = $body; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $body | ||
* @return RequestBuilder | ||
*/ | ||
public function setJsonBody($body) | ||
{ | ||
$this->request->is_json = true; | ||
$this->request->json_body = $body; | ||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @param string $value | ||
* @param bool $onetime if true header remove after buildRequest() called | ||
* @return RequestBuilder | ||
*/ | ||
public function addHeader($key, $value, $onetime = false) | ||
{ | ||
if ($onetime) | ||
{ | ||
$this->request->addOnetimeHeader($key, $value); | ||
} | ||
else | ||
{ | ||
$this->request->addHeader($key, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @param bool $onetime if true header removed from onetime headers | ||
* @return RequestBuilder | ||
*/ | ||
public function removeHeader($key, $onetime = false) | ||
{ | ||
if ($onetime) | ||
{ | ||
$this->request->removeOnetimeHeader($key); | ||
} | ||
else | ||
{ | ||
$this->request->removeHeader($key); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string $value | ||
* @param bool $onetime if true query_string remove after buildRequest() called | ||
* @return RequestBuilder | ||
*/ | ||
public function addQueryString($key, $value, $onetime = false) | ||
{ | ||
if ($onetime) | ||
{ | ||
$this->request->addOnetimeQueryString($key, $value); | ||
} | ||
else | ||
{ | ||
$this->request->addQueryString($key, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $key | ||
* @param bool $onetime if true query_string removed from onetime query_strings | ||
* @return RequestBuilder | ||
*/ | ||
public function removeQueryString($key, $onetime = false) | ||
{ | ||
if ($onetime) | ||
{ | ||
$this->request->removeOnetimeQueryString($key); | ||
} | ||
else | ||
{ | ||
$this->request->removeQueryString($key); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @return RequestBuilder | ||
*/ | ||
public function setMethod($method) | ||
{ | ||
if (!defined('\Lessmore92\ApiConsumer\Contracts\RequestModelInterface::REQUEST_METHOD_' . strtoupper($method))) | ||
{ | ||
throw new RequestMethodNotSupported(sprintf('Request method %s not supported.', $method)); | ||
} | ||
$this->request->method = $method; | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.