- Work done to allow more flexibility on based on whatever custom attributes were provided.

This commit is contained in:
Dave Mc Nicoll 2023-11-13 14:21:39 -05:00
parent b3e792e20f
commit bea01badb7
5 changed files with 21 additions and 8 deletions

View File

@ -29,7 +29,7 @@ class Rest implements AdapterInterface
public function connect(): object public function connect(): object
{ {
return new CurlClient($this->headers, $this->options, ( new CurlAuthorization())->fromAuthenticationEnum($this->auth, $this->username ?? false, $this->password ?? false, $this->token ?? false) ); return new CurlClient($this->headers, $this->options, ( new CurlAuthorization() )->fromAuthenticationEnum($this->auth, $this->username ?? false, $this->password ?? false, $this->token ?? false) );
} }
public function buildDataSourceName(): string public function buildDataSourceName(): string
@ -39,7 +39,7 @@ class Rest implements AdapterInterface
public function setup(array $configuration): void public function setup(array $configuration): void
{ {
$this->auth = AuthenticationEnum::from($configuration['auth'] ?: AuthenticationEnum::Basic->value); $this->auth = AuthenticationEnum::from($configuration['auth'] ?? AuthenticationEnum::Basic->value);
$this->url = rtrim($configuration['url'], '/'); $this->url = rtrim($configuration['url'], '/');
foreach([ 'username', 'password', 'token', 'headers', 'options' ] as $conf) { foreach([ 'username', 'password', 'token', 'headers', 'options' ] as $conf) {

View File

@ -31,7 +31,7 @@ class ApiRepository extends \Ulmus\Repository
parent::__construct($entity, $alias, $adapter); parent::__construct($entity, $alias, $adapter);
} }
public function loadOne(): ?object public function loadOne(): ? object
{ {
$response = $this->executeRequest(Attribute\Obj\Api\Read::class); $response = $this->executeRequest(Attribute\Obj\Api\Read::class);
@ -58,11 +58,19 @@ class ApiRepository extends \Ulmus\Repository
{ {
$attribute = $this->getApiAttribute($attributeClass); $attribute = $this->getApiAttribute($attributeClass);
if ($attribute === null) {
throw new \RuntimeException(sprintf("Could not find attribute class '%s' for class '%s'", $attributeClass, $this->entityClass));
}
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method, $data); $request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method, $data);
$request = $this->callApiRequestCallback($request, $attribute); $request = $this->callApiRequestCallback($request, $attribute);
$this->lastResponse = $response = $this->adapter->adapter()->connect()->fromRequest($request); $rest = $this->adapter->adapter()->connect();
$rest->timeout = $attribute->timeout;
$this->lastResponse = $response = $rest->fromRequest($request);
$response = $this->callApiResponseCallback($response, $attribute); $response = $this->callApiResponseCallback($response, $attribute);
@ -126,7 +134,7 @@ class ApiRepository extends \Ulmus\Repository
return new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : JsonStream::fromContent($body), $headers); return new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : JsonStream::fromContent($body), $headers);
} }
public function getApiAttribute(string $type) : object public function getApiAttribute(string $type) : ? object
{ {
return $this->entityClass::resolveEntity()->getAttributeImplementing($type); return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
} }

View File

@ -10,5 +10,6 @@ abstract class ApiAction
public function __construct( public function __construct(
public string $url, public string $url,
public MethodEnum $method, public MethodEnum $method,
public int $timeout = 5,
) {} ) {}
} }

View File

@ -9,6 +9,5 @@ class CurlClient extends CurlTransport
{ {
# Handle file uploading here # Handle file uploading here
} }

View File

@ -10,7 +10,11 @@ use Ulmus\Api\Response\{ Response, JsonResponse };
abstract class CurlTransport { abstract class CurlTransport {
public int $timeout = 5; # Seconds
public int $timeout = 30;
# Seconds
public int $connectTimeout = 300;
public int $maximumRedirections = 10; public int $maximumRedirections = 10;
@ -81,7 +85,8 @@ abstract class CurlTransport {
CURLOPT_URL => $url, CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => HttpHeaderEnum::compileHeaders($headers), CURLOPT_HTTPHEADER => HttpHeaderEnum::compileHeaders($headers),
CURLOPT_MAXREDIRS => $this->maximumRedirections, CURLOPT_MAXREDIRS => $this->maximumRedirections,
CURLOPT_TIMEOUT_MS => $this->timeout * 1000, CURLOPT_TIMEOUT => $this->timeout * 1000,
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,