Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

final class BadRequestException extends DaDataException
{
public static function create(Response $response, Request $request): self
/**
* @param non-empty-string $source
*/
public static function create(Response $response, Request $request, string $source): self
{
return new self($response, $request, 'Bad request');
return new self($response, $request, $source, 'Bad request');
}
}
18 changes: 18 additions & 0 deletions src/Infrastructure/HttpClient/Exception/DaDataException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,37 @@ abstract class DaDataException extends \Exception implements ClientException

private Request $request;

/**
* @var non-empty-string
*/
private string $source;

/**
* @param non-empty-string $source
*/
final protected function __construct(
Response $response,
Request $request,
string $source,
string $message = '',
int $code = 0,
?\Throwable $previous = null
) {
$this->response = $response;
$this->request = $request;
$this->source = $source;

parent::__construct($message, $code, $previous);
}

/**
* @return non-empty-string
*/
final public function getSource(): string
{
return $this->source;
}

final public function getResponse(): Response
{
return $this->response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

final class ForbiddenException extends DaDataException
{
public static function create(Response $response, Request $request): self
/**
* @param non-empty-string $source
*/
public static function create(Response $response, Request $request, string $source): self
{
return new self($response, $request, 'Forbidden');
return new self($response, $request, $source, 'Forbidden');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

final class InternalServerErrorException extends DaDataException
{
public static function create(Response $response, Request $request): self
/**
* @param non-empty-string $source
*/
public static function create(Response $response, Request $request, string $source): self
{
return new self($response, $request, 'Internal Server Error');
return new self($response, $request, $source, 'Internal Server Error');
}
}
7 changes: 5 additions & 2 deletions src/Infrastructure/HttpClient/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

final class NotFoundException extends DaDataException
{
public static function create(Response $response, Request $request): self
/**
* @param non-empty-string $source
*/
public static function create(Response $response, Request $request, string $source): self
{
return new self($response, $request, 'Not Found');
return new self($response, $request, $source, 'Not Found');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

final class UnauthorizedException extends DaDataException
{
public static function create(Response $response, Request $request): self
/**
* @param non-empty-string $source
*/
public static function create(Response $response, Request $request, string $source): self
{
return new self($response, $request, 'Unauthorized');
return new self($response, $request, $source, 'Unauthorized');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\UnauthorizedException;
use Yiisoft\Http\Status;

final class ClientErrorMiddleware implements Middleware
final class ClientErrorMiddleware extends SourceAwareMiddleware
{
public function process(Request $request, ConfigurationClient $configuration, callable $next): Response
{
$response = $next($request, $configuration);
$statusCode = $response->getStatusCode();

if (Status::UNAUTHORIZED == $statusCode) {
throw UnauthorizedException::create($response, $request);
throw UnauthorizedException::create($response, $request, $this->source);
}

if (Status::FORBIDDEN == $statusCode) {
throw ForbiddenException::create($response, $request);
throw ForbiddenException::create($response, $request, $this->source);
}

if (Status::NOT_FOUND === $statusCode) {
throw NotFoundException::create($response, $request);
throw NotFoundException::create($response, $request, $this->source);
}

if ($statusCode >= Status::BAD_REQUEST && $statusCode <= Status::UNAVAILABLE_FOR_LEGAL_REASONS) {
throw BadRequestException::create($response, $request);
throw BadRequestException::create($response, $request, $this->source);
}

return $response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\InternalServerErrorException;
use Yiisoft\Http\Status;

final class InternalServerMiddleware implements Middleware
final class InternalServerMiddleware extends SourceAwareMiddleware
{
public function process(Request $request, ConfigurationClient $configuration, callable $next): Response
{
Expand All @@ -28,6 +28,6 @@ public function process(Request $request, ConfigurationClient $configuration, ca
return $response;
}

throw InternalServerErrorException::create($response, $request);
throw InternalServerErrorException::create($response, $request, $this->source);
}
}
37 changes: 37 additions & 0 deletions src/Infrastructure/HttpClient/Middleware/SourceAwareMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* DaData Client
*
* @author Vlad Shashkov <v.shashkov@pos-credit.ru>
* @copyright Copyright (c) 2026, The Vanta
*/
declare(strict_types=1);

namespace Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware;

abstract class SourceAwareMiddleware implements Middleware
{
/**
* @var non-empty-string
*/
protected string $source;

/**
* @param non-empty-string $source
*/
final public function __construct(string $source = 'none')
{
$this->source = $source;
}

/**
* @param non-empty-string $source
*
* @return static
*/
final public function withSource(string $source): self
{
return new static($source);
}
}
55 changes: 40 additions & 15 deletions src/RestClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware\InternalServerMiddleware;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware\Middleware;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware\PipelineMiddleware;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware\SourceAwareMiddleware;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware\UrlMiddleware;
use Vanta\Integration\DaData\Infrastructure\PropertyInfo\Extractor\PollyfillPhpStanExtractor;
use Vanta\Integration\DaData\Infrastructure\Serializer\CapitalMarkerNormalizer;
Expand Down Expand Up @@ -145,6 +146,20 @@ public static function create(PsrHttpClient $client, ?string $apiKey = null, ?st
return new self($client, $serializer, $apiKey, $secretKey, $middlewares);
}

/**
* @param non-empty-string $source
*/
private function withSource(string $source): self
{
return new self(
$this->client,
$this->serializer,
$this->apiKey,
$this->secretKey,
array_map(static fn (Middleware $m): Middleware => $m instanceof SourceAwareMiddleware ? $m->withSource($source) : $m, $this->middlewares),
);
}

public function addMiddleware(Middleware $middleware): self
{
return new self(
Expand Down Expand Up @@ -185,11 +200,13 @@ public function withClient(PsrHttpClient $client): self
*/
public function createCleanFullNameClient(string $url = 'https://cleaner.dadata.ru'): CleanFullNameClient
{
$new = $this->withSource('CLEAN-FULLNAME-CLIENT');

return new RestCleanFullNameClient(
$this->serializer,
$new->serializer,
new HttpClient(
new ConfigurationClient($this->apiKey, $this->secretKey, $url),
new PipelineMiddleware($this->middlewares, $this->client)
new ConfigurationClient($new->apiKey, $new->secretKey, $url),
new PipelineMiddleware($new->middlewares, $new->client)
)
);
}
Expand All @@ -199,11 +216,13 @@ public function createCleanFullNameClient(string $url = 'https://cleaner.dadata.
*/
public function createSuggestAddressClient(string $url = 'https://suggestions.dadata.ru'): SuggestAddressClient
{
$new = $this->withSource('SUGGEST-ADDRESSES-CLIENT');

return new RestSuggestAddressClient(
$this->serializer,
$new->serializer,
new HttpClient(
new ConfigurationClient($this->apiKey, $this->secretKey, $url),
new PipelineMiddleware($this->middlewares, $this->client)
new ConfigurationClient($new->apiKey, $new->secretKey, $url),
new PipelineMiddleware($new->middlewares, $new->client)
)
);
}
Expand All @@ -213,11 +232,13 @@ public function createSuggestAddressClient(string $url = 'https://suggestions.da
*/
public function createSuggestOrganizationClient(string $url = 'https://suggestions.dadata.ru'): SuggestOrganizationClient
{
$new = $this->withSource('SUGGEST-ORGANIZATION-CLIENT');

return new RestSuggestOrganizationClient(
$this->serializer,
$new->serializer,
new HttpClient(
new ConfigurationClient($this->apiKey, $this->secretKey, $url),
new PipelineMiddleware($this->middlewares, $this->client)
new ConfigurationClient($new->apiKey, $new->secretKey, $url),
new PipelineMiddleware($new->middlewares, $new->client)
)
);
}
Expand All @@ -227,11 +248,13 @@ public function createSuggestOrganizationClient(string $url = 'https://suggestio
*/
public function createSuggestFullNameClient(string $url = 'https://suggestions.dadata.ru'): RestSuggestFullNameClient
{
$new = $this->withSource('SUGGEST-FULLNAME-CLIENT');

return new RestSuggestFullNameClient(
$this->serializer,
$new->serializer,
new HttpClient(
new ConfigurationClient($this->apiKey, $this->secretKey, $url),
new PipelineMiddleware($this->middlewares, $this->client)
new ConfigurationClient($new->apiKey, $new->secretKey, $url),
new PipelineMiddleware($new->middlewares, $new->client)
)
);
}
Expand All @@ -241,11 +264,13 @@ public function createSuggestFullNameClient(string $url = 'https://suggestions.d
*/
public function createCleanAddressClient(string $url = 'https://cleaner.dadata.ru'): CleanAddressClient
{
$new = $this->withSource('CLEAN-ADDRESSES-CLIENT');

return new RestCleanAddressClient(
$this->serializer,
$new->serializer,
new HttpClient(
new ConfigurationClient($this->apiKey, $this->secretKey, $url),
new PipelineMiddleware($this->middlewares, $this->client)
new ConfigurationClient($new->apiKey, $new->secretKey, $url),
new PipelineMiddleware($new->middlewares, $new->client)
)
);
}
Expand Down
Loading