Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
rettahc authored Jul 7, 2021
1 parent 4771875 commit 7c1d66c
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
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
```
34 changes: 34 additions & 0 deletions composer.json
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"
}
159 changes: 159 additions & 0 deletions src/Connector/BaseConnector.php
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, '/')
);
}
}
42 changes: 42 additions & 0 deletions src/Connector/ConnectorInterface.php
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;
}

0 comments on commit 7c1d66c

Please sign in to comment.