diff --git a/README.md b/README.md index bb2f167..1b91e47 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,20 @@ return [ 'host' => "127.0.0.1", 'port' => "59002", ], + ], + 'http' => [ + // JSON datasource basic authentication credetials + 'source' => [ + 'username' => 'username', + 'password' => 'fubar', + // Basic authentication protocol: basic, digest, or ntlm. + // Protocol is optional. If not mentioned, basic is considered. + 'protocol' => 'ntlm', + ], + 'destination' => [ + 'username' => 'username', + 'password' => 'puffball', + ] ] ]; ``` @@ -99,14 +113,14 @@ source: data: https://dev.legacy-system.com/api/v1/users # The pointer where all the list of items resides. Refer https://github.com/halaxa/json-machine#what-is-json-pointer-anyway for examples selector: "/results/-/employees" - # Basic authentication credentials to access the endpoint. This is optional if the endpoint is publicly accessible. - auth_basic: "foo:bar" + # Datasource basic authentication credentials. Note that the key is same as mentioned in the test specification. This is optional if the endpoint is publicly accessible. + credential: source # The endpoint that returns the destination dataset. destination: type: json data: https://dev.new-system.com/api/v1/users selector: "/results/-/employees" - auth_basic: "foo:bar" + credential: destination tests: - sourceField: name diff --git a/src/Destination/Database.php b/src/DataSource/Database.php similarity index 96% rename from src/Destination/Database.php rename to src/DataSource/Database.php index 0d406bd..7010fb4 100644 --- a/src/Destination/Database.php +++ b/src/DataSource/Database.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace Mdtt\Destination; +namespace Mdtt\DataSource; use Iterator; -use Mdtt\DataSource\DataSource; use Mdtt\Exception\SetupException; use Mdtt\Utility\DataSource\Database as DbDatabase; use mysqli_result; diff --git a/src/Destination/Json.php b/src/DataSource/Json.php similarity index 54% rename from src/Destination/Json.php rename to src/DataSource/Json.php index 9621a85..60dc73c 100644 --- a/src/Destination/Json.php +++ b/src/DataSource/Json.php @@ -2,11 +2,10 @@ declare(strict_types=1); -namespace Mdtt\Destination; +namespace Mdtt\DataSource; use Iterator; use JsonMachine\Items; -use Mdtt\DataSource\DataSource; use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility; class Json extends DataSource @@ -14,6 +13,33 @@ class Json extends DataSource private string $selector; private JsonDataSourceUtility $jsonDataSourceUtility; private Items|null $items; + private ?string $username; + private ?string $password; + private ?string $protocol; + + /** + * @param string|null $username + */ + public function setUsername(?string $username): void + { + $this->username = $username; + } + + /** + * @param string|null $password + */ + public function setPassword(?string $password): void + { + $this->password = $password; + } + + /** + * @param string|null $protocol + */ + public function setProtocol(?string $protocol): void + { + $this->protocol = $protocol; + } public function __construct( string $data, @@ -25,21 +51,19 @@ public function __construct( $this->jsonDataSourceUtility = $jsonDataSourceUtility; } - /** - * @return string - */ - public function getSelector(): string - { - return $this->selector; - } - /** * @inheritDoc */ public function getIterator(): Iterator { if (!isset($this->items)) { - $this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector); + $this->items = $this->jsonDataSourceUtility->getItems( + $this->data, + $this->selector, + $this->username, + $this->password, + $this->protocol + ); } foreach ($this->items as $item) { diff --git a/src/Definition/Validate/DataSource/Validator.php b/src/Definition/Validate/DataSource/Validator.php index 92b852c..c14674f 100644 --- a/src/Definition/Validate/DataSource/Validator.php +++ b/src/Definition/Validate/DataSource/Validator.php @@ -37,43 +37,49 @@ public function validate(string $type, array $rawDataSourceDefinition): DataSour if ($dataSourceType === "database") { $this->doValidateDatabase($rawDataSourceDefinition); - if ($type === "source") { - return new \Mdtt\Source\Database( - $rawDataSourceDefinition['data'], - $rawDataSourceDefinition['database'] - ); - } - - if ($type === "destination") { - return new \Mdtt\Destination\Database( - $rawDataSourceDefinition['data'], - $rawDataSourceDefinition['database'] - ); - } + return new \Mdtt\DataSource\Database( + $rawDataSourceDefinition['data'], + $rawDataSourceDefinition['database'] + ); } if ($dataSourceType === "json") { $this->doValidateJson($rawDataSourceDefinition); - - if (isset($rawDataSourceDefinition['auth_basic'])) { - $this->jsonDataSourceUtility->setAuthBasicCredential($rawDataSourceDefinition['auth_basic']); + $username = null; + $password = null; + $protocol = null; + + if (isset($rawDataSourceDefinition['credential'])) { + $specification = require "tests/mdtt/spec.php"; + + $httpSpecification = $specification['http']; + /** @var string $credentialKey */ + $credentialKey = $rawDataSourceDefinition['credential']; + + if (!isset( + $httpSpecification[$credentialKey]['username'], + $httpSpecification[$credentialKey]['password'] + )) { + throw new SetupException( + "Basic auth username and password are not provided, but credential is specified." + ); + } + + $username = $httpSpecification[$credentialKey]['username']; + $password = $httpSpecification[$credentialKey]['password']; + $protocol = $httpSpecification[$credentialKey]['protocol'] ?? null; } - if ($type === "source") { - return new \Mdtt\Source\Json( - $rawDataSourceDefinition['data'], - $rawDataSourceDefinition['selector'], - $this->jsonDataSourceUtility, - ); - } + $datasource = new \Mdtt\DataSource\Json( + $rawDataSourceDefinition['data'], + $rawDataSourceDefinition['selector'], + $this->jsonDataSourceUtility, + ); + $datasource->setUsername($username); + $datasource->setPassword($password); + $datasource->setProtocol($protocol); - if ($type === "destination") { - return new \Mdtt\Destination\Json( - $rawDataSourceDefinition['data'], - $rawDataSourceDefinition['selector'], - $this->jsonDataSourceUtility - ); - } + return $datasource; } throw new SetupException(sprintf("Unexpected data source type %s and data source definition passed.", $type)); diff --git a/src/Source/Database.php b/src/Source/Database.php deleted file mode 100644 index 59681d6..0000000 --- a/src/Source/Database.php +++ /dev/null @@ -1,58 +0,0 @@ -databaseKey = $databaseKey; - } - - /** - * @inheritDoc - */ - public function getIterator(): Iterator - { - $specification = require "tests/mdtt/spec.php"; - - /** @var array> $databases */ - $databases = $specification['databases']; - if (!isset( - $databases[$this->databaseKey]['database'], - $databases[$this->databaseKey]['username'], - $databases[$this->databaseKey]['password'], - $databases[$this->databaseKey]['host'], - $databases[$this->databaseKey]['port'] - )) { - throw new SetupException("Source database not specified correctly"); - } - - if (!isset($this->resultSet)) { - $this->resultSet = DbDataSource::prepareResultSet( - $databases[$this->databaseKey]['database'], - $databases[$this->databaseKey]['username'], - $databases[$this->databaseKey]['password'], - $databases[$this->databaseKey]['host'], - (int)$databases[$this->databaseKey]['port'], - $this->data - ); - } - - while ($row = $this->resultSet->fetch_assoc()) { - yield $row; - } - } -} diff --git a/src/Source/Json.php b/src/Source/Json.php deleted file mode 100644 index 8c576e1..0000000 --- a/src/Source/Json.php +++ /dev/null @@ -1,49 +0,0 @@ -selector = $selector; - $this->jsonDataSourceUtility = $jsonDataSourceUtility; - } - - /** - * @return string - */ - public function getSelector(): string - { - return $this->selector; - } - - /** - * @inheritDoc - */ - public function getIterator(): Iterator - { - if (!isset($this->items)) { - $this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector); - } - - foreach ($this->items as $item) { - yield $item; - } - } -} diff --git a/src/Utility/DataSource/Json.php b/src/Utility/DataSource/Json.php index d2bca5f..a70812c 100644 --- a/src/Utility/DataSource/Json.php +++ b/src/Utility/DataSource/Json.php @@ -4,7 +4,6 @@ namespace Mdtt\Utility\DataSource; -use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\StreamWrapper; use JsonMachine\Exception\InvalidArgumentException; use JsonMachine\Items; @@ -16,33 +15,28 @@ class Json { private HttpClient $httpClient; - private ?string $authBasicCredential; public function __construct(HttpClient $httpClient) { $this->httpClient = $httpClient; } - /** - * @param string $authBasicCredential - */ - public function setAuthBasicCredential(string $authBasicCredential): void - { - $this->authBasicCredential = $authBasicCredential; - } - - public function getItems(string $data, string $selector): Items - { + public function getItems( + string $data, + string $selector, + ?string $username, + ?string $password, + ?string $protocol + ): Items { $httpClient = $this->httpClient->getClient(); - $request = new Request('GET', $data); + $headers = []; - if (isset($this->authBasicCredential)) { - $credential = explode(':', $this->authBasicCredential); - $request->withHeader('auth', $credential); + if (isset($username, $password)) { + $headers['auth'] = [$username, $password, $protocol ?? 'basic']; } try { - $response = $httpClient->sendRequest($request); + $response = $httpClient->request('GET', $data, $headers); } catch (ClientExceptionInterface $e) { throw new ExecutionException($e->getMessage()); } diff --git a/src/Utility/HttpClient.php b/src/Utility/HttpClient.php index ac8bf0e..6c96971 100644 --- a/src/Utility/HttpClient.php +++ b/src/Utility/HttpClient.php @@ -5,7 +5,7 @@ namespace Mdtt\Utility; use GuzzleHttp\Client; -use Psr\Http\Client\ClientInterface; +use GuzzleHttp\ClientInterface; class HttpClient {