-
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
Showing
4 changed files
with
243 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,8 @@ | ||
# Base HTTP Connector | ||
Provides basic http request functionality | ||
|
||
## Installation | ||
This project using composer. | ||
``` | ||
$ composer require fastbolt/connector | ||
``` |
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,34 @@ | ||
{ | ||
"name": "fastbolt/connector", | ||
"description": "Base HTTP Connector functionality", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"App\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"php": "7.2.*", | ||
"guzzlehttp/guzzle": "^6.5", | ||
"symfony/framework-bundle": "^4.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8.3" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Daniel Winter", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Daniel Hirtzbruch", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "stable" | ||
} |
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,159 @@ | ||
<?php | ||
/** | ||
* FBonline | ||
* | ||
* @copyright 2014-2017 Fastbolt Schraubengroßhandels GmbH (http://www.fastbolt.com) | ||
* @license commercial | ||
* @link https://fbonline.fastbolt.com | ||
* @package App\System\Connectors | ||
*/ | ||
|
||
|
||
namespace Connector; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Psr7\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response as HttpResponse; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
abstract class BaseConnector implements ConnectorInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $webServiceHost; | ||
|
||
/** | ||
* @var callable|null | ||
*/ | ||
protected $clientFactory; | ||
|
||
/** | ||
* @param string $path | ||
* @param string $requestMethod | ||
* @param array|null $parameters | ||
* @param string $acceptHeader | ||
* | ||
* @return string | ||
*/ | ||
public function request( | ||
string $path, | ||
string $requestMethod = Request::METHOD_POST, | ||
?array $parameters = [], | ||
string $acceptHeader = self::ACCEPT_HEADER_JSON | ||
): string { | ||
$clientFactory = $this->getClientFactory(); | ||
$client = $clientFactory(); | ||
$requestParameters = [ | ||
'verify' => false, | ||
'query' => $this->getCredentials(), | ||
'headers' => [ | ||
'Accept' => $acceptHeader, | ||
], | ||
]; | ||
|
||
foreach ($parameters as $parameterRealm => $realmParameters) { | ||
// [ 'json' => $data ] | ||
if (!\is_array($realmParameters)) { | ||
$requestParameters[$parameterRealm] = $realmParameters; | ||
|
||
continue; | ||
} | ||
foreach ($realmParameters as $parameterName => $parameterValue) { | ||
if (!isset($requestParameters[$parameterRealm])) { | ||
$requestParameters[$parameterRealm] = []; | ||
} | ||
$requestParameters[$parameterRealm][$parameterName] = $parameterValue; | ||
} | ||
} | ||
|
||
$url = ''; | ||
try { | ||
/** @var Response $response */ | ||
$response = $client->request( | ||
$requestMethod, | ||
$url = $this->getUrl($path), | ||
$requestParameters | ||
); | ||
} catch (RequestException $exception) { | ||
$response = $exception->getResponse(); | ||
throw new HttpException( | ||
$response ? $response->getStatusCode() : 0, | ||
sprintf( | ||
'Error connecting to %s (%s request to %s returned %s (%s)) (%s).', | ||
$this->getType(), | ||
$requestMethod, | ||
$url, | ||
$response ? $response->getStatusCode() : 'null', | ||
$response ? $response->getReasonPhrase() : 'empty response', | ||
$exception->getMessage() | ||
) | ||
); | ||
} | ||
|
||
if ($response->getStatusCode() !== HttpResponse::HTTP_OK) { | ||
throw new HttpException( | ||
$response->getStatusCode(), | ||
sprintf( | ||
'Error connecting to %s (%s request to %s returned %s (%s)).', | ||
$this->getType(), | ||
$requestMethod, | ||
$url, | ||
$response->getStatusCode(), | ||
$response->getReasonPhrase() | ||
) | ||
); | ||
} | ||
|
||
if ((string)($body = $response->getBody()->getContents()) === '') { | ||
throw new HttpException( | ||
$response->getStatusCode(), | ||
sprintf( | ||
'Error connecting to %s (%s request to %s returned %s (%s), but resulted in empty data.).', | ||
$this->getType(), | ||
$requestMethod, | ||
$url, | ||
$response->getStatusCode(), | ||
$response->getReasonPhrase() | ||
) | ||
); | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
protected function getClientFactory(): callable | ||
{ | ||
if (null === ($clientFactory = $this->clientFactory)) { | ||
$clientFactory = static function () { | ||
return new Client(['verify' => false]); | ||
}; | ||
} | ||
|
||
return $clientFactory; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
abstract protected function getCredentials(): array; | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @return string | ||
*/ | ||
protected function getUrl(string $path): string | ||
{ | ||
return sprintf( | ||
'%s/%s', | ||
rtrim($this->webServiceHost, '/'), | ||
ltrim($path, '/') | ||
); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
/** | ||
* FBonline | ||
* | ||
* @copyright 2014-2017 Fastbolt Schraubengroßhandels GmbH (http://www.fastbolt.com) | ||
* @license commercial | ||
* @link https://fbonline.fastbolt.com | ||
* @package App\System\Connectors | ||
*/ | ||
|
||
|
||
namespace Connector; | ||
|
||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
interface ConnectorInterface | ||
{ | ||
public const ACCEPT_HEADER_JSON = 'application/json'; | ||
|
||
public const ACCEPT_HEADER_XML = 'application/xml'; | ||
|
||
/** | ||
* @param string $path | ||
* @param string $requestMethod | ||
* @param array|null $parameters | ||
* @param string $acceptHeader | ||
* | ||
* @return string | ||
*/ | ||
public function request( | ||
string $path, | ||
string $requestMethod = Request::METHOD_POST, | ||
?array $parameters = [], | ||
string $acceptHeader = self::ACCEPT_HEADER_JSON | ||
): string; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getType(): string; | ||
} |