Skip to content

Commit

Permalink
Use a VaaS builder
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartdohmann committed Dec 16, 2024
1 parent 8b9cdbd commit 07ab026
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 57 deletions.
2 changes: 1 addition & 1 deletion php/examples/VaasExample/AuthenticationExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
tokenUrl: getenv("TOKEN_URL")
);

$vaas = (new Vaas())
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->build();

Expand Down
2 changes: 1 addition & 1 deletion php/examples/VaasExample/GetVerdictByFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
tokenUrl: getenv("TOKEN_URL")
);

$vaas = (new Vaas())
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->build();

Expand Down
2 changes: 1 addition & 1 deletion php/examples/VaasExample/GetVerdictByHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
tokenUrl: getenv("TOKEN_URL")
);

$vaas = (new Vaas())
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->build();

Expand Down
2 changes: 1 addition & 1 deletion php/examples/VaasExample/GetVerdictByUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
tokenUrl: getenv("TOKEN_URL")
);

$vaas = (new Vaas())
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->build();

Expand Down
60 changes: 10 additions & 50 deletions php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
use Amp\Cancellation;
use Amp\Future;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\StreamedContent;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use VaasSdk\Authentication\AuthenticatorInterface;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Exceptions\VaasClientException;
Expand All @@ -36,79 +34,41 @@ class Vaas
private AuthenticatorInterface $authenticator;
private VaasOptions $options;
private LoggerInterface $logger;

private function __construct() {}

public static function builder(): VaasBuilder {
return new VaasBuilder();
}

public static function createInstance(): Vaas {
return new self();
}

/**
* Optional parameters for the Vaas client like
* - the URL of the VaaS backend
* - whether to use the cache (default: true)
* - whether to use the G DATA cloud for hash lookups (default: true)
* - the timeout in seconds for the file upload to the VaaS backend (default: 300)
* @param VaasOptions $options Options for the Vaas client
* @return $this
*/
public function withOptions(VaasOptions $options): self
{
$this->options = $options;
return $this;
}

/**
* @param HttpClient $httpClient Your optional custom http client.
* @return $this
*/
public function withHttpClient(HttpClient $httpClient): self
{
$this->httpClient = $httpClient;
return $this;
}

/**
* Either use the `ClientCredentialsGrantAuthenticator` or `ResourceOwnerPasswordGrantAuthenticator`
* Use the `ClientCredentialsGrantAuthenticator` if you have a client id and client secret.
* Use the `ResourceOwnerPasswordGrantAuthenticator` if you have a username and password.
* Last one is the choice if you have registered yourself on https://vaas.gdata.de/login. In this case, the client id is `vaas-customer`.
* @param AuthenticatorInterface $authenticator The authenticator to use
* @return $this
*/
public function withAuthenticator(AuthenticatorInterface $authenticator): self
{
$this->authenticator = $authenticator;
return $this;
}

/**
* Set the logger to use
* @param LoggerInterface $logger The logger to use
* @return $this
*/
public function withLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}

/**
* Build the Vaas client
* @return $this
* @throws VaasClientException If the authenticator is not set
*/
public function build(): self
{
if (!isset($this->logger)) {
$this->logger = new NullLogger();
}
if (!isset($this->authenticator)) {
throw new VaasClientException('Authenticator is required');
}
if (!isset($this->httpClient)) {
$this->httpClient = HttpClientBuilder::buildDefault();
}
if (!isset($this->options)) {
$this->options = new VaasOptions();
}
return $this;
}

/**
* Get the verdict for a file by its SHA256 hash
* @param Sha256 $sha256 The SHA256 hash of the file to check
Expand Down
97 changes: 97 additions & 0 deletions php/src/vaas/VaasBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace VaasSdk;

use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use VaasSdk\Authentication\AuthenticatorInterface;
use VaasSdk\Exceptions\VaasClientException;
use VaasSdk\Options\VaasOptions;

class VaasBuilder
{
private HttpClient $httpClient;
private AuthenticatorInterface $authenticator;
private VaasOptions $options;
private LoggerInterface $logger;

/**
* Optional parameters for the Vaas client like
* - the URL of the VaaS backend
* - whether to use the cache (default: true)
* - whether to use the G DATA cloud for hash lookups (default: true)
* - the timeout in seconds for the file upload to the VaaS backend (default: 300)
* @param VaasOptions $options Options for the Vaas client
* @return $this
*/
public function withOptions(VaasOptions $options): self
{
$this->options = $options;
return $this;
}

/**
* @param HttpClient $httpClient Your optional custom http client.
* @return $this
*/
public function withHttpClient(HttpClient $httpClient): self
{
$this->httpClient = $httpClient;
return $this;
}

/**
* Either use the `ClientCredentialsGrantAuthenticator` or `ResourceOwnerPasswordGrantAuthenticator`
* Use the `ClientCredentialsGrantAuthenticator` if you have a client id and client secret.
* Use the `ResourceOwnerPasswordGrantAuthenticator` if you have a username and password.
* Last one is the choice if you have registered yourself on https://vaas.gdata.de/login. In this case, the client id is `vaas-customer`.
* @param AuthenticatorInterface $authenticator The authenticator to use
* @return $this
*/
public function withAuthenticator(AuthenticatorInterface $authenticator): self
{
$this->authenticator = $authenticator;
return $this;
}

/**
* Set the logger to use
* @param LoggerInterface $logger The logger to use
* @return $this
*/
public function withLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}

/**
* Build the Vaas client
* @return Vaas The Vaas client
* @throws VaasClientException If the authenticator is not set
*/
public function build(): Vaas
{
if (!isset($this->logger)) {
$this->logger = new NullLogger();
}
if (!isset($this->authenticator)) {
throw new VaasClientException('Authenticator is required');
}
if (!isset($this->httpClient)) {
$this->httpClient = HttpClientBuilder::buildDefault();
}
if (!isset($this->options)) {
$this->options = new VaasOptions();
}

$vaas = Vaas::createInstance();
$vaas->withHttpClient($this->httpClient);
$vaas->withAuthenticator($this->authenticator);
$vaas->withOptions($this->options);
$vaas->withLogger($this->logger);
return $vaas;
}
}
4 changes: 2 additions & 2 deletions php/tests/VaasTesting/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testClientCredentialsGrantAuthenticator_withInvalidCredentials_T
clientSecret: "invalid"
);

$authenticator->getTokenAsync()->await();
$authenticator->getTokenAsync();
}

public function testResourceOwnerPasswordGrantAuthenticator_withInvalidCredentials_ThrowsAccessDeniedException(): void
Expand All @@ -68,7 +68,7 @@ public function testResourceOwnerPasswordGrantAuthenticator_withInvalidCredentia
password: "invalid"
);

$authenticator->getTokenAsync()->await();
$authenticator->getTokenAsync();
}

public function testClientCredentialsGrantAuthenticator_withValidCredentials_ReturnsToken(): void
Expand Down
2 changes: 1 addition & 1 deletion php/tests/VaasTesting/VaasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function getVaas(bool $useCache = false, bool $useHashLookup = true): Va
$_ENV["TOKEN_URL"]
);

return (new Vaas())
return Vaas::builder()
->withAuthenticator($authenticator)
->withOptions($options)
->withLogger($this->logger)
Expand Down

0 comments on commit 07ab026

Please sign in to comment.