Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protobuf & JSONSchema support #50

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
return (new PhpCsFixer\Config())
->setRules(
[
'@PSR2' => true,
'no_unused_imports' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG PHP_VERSION=7.4

FROM php:${PHP_VERSION}-cli-alpine

ARG XDEBUG_VERSION=2.9.8
ARG XDEBUG_VERSION=3.1.1

COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN composer --version
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ major version upgrades will have incompatibilities that will be released in the
<?php

use GuzzleHttp\Client;
use FlixTech\SchemaRegistryApi\Registry\PromisingRegistry;
use FlixTech\SchemaRegistryApi\Registry\GuzzlePromiseAsyncRegistry;
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;

$registry = new PromisingRegistry(
$registry = new GuzzlePromiseAsyncRegistry(
new Client(['base_uri' => 'registry.example.com'])
);

Expand Down Expand Up @@ -140,12 +140,12 @@ $schemaId = $registry->schemaId('test-subject', $schema)->wait();
```php
<?php

use FlixTech\SchemaRegistryApi\Registry\BlockingRegistry;
use FlixTech\SchemaRegistryApi\Registry\PromisingRegistry;
use FlixTech\SchemaRegistryApi\Registry\Decorators\BlockingDecorator;
use FlixTech\SchemaRegistryApi\Registry\GuzzlePromiseAsyncRegistry;
use GuzzleHttp\Client;

$registry = new BlockingRegistry(
new PromisingRegistry(
$registry = new BlockingDecorator(
new GuzzlePromiseAsyncRegistry(
new Client(['base_uri' => 'registry.example.com'])
)
);
Expand Down Expand Up @@ -173,30 +173,30 @@ It supports both async and sync APIs.
```php
<?php

use FlixTech\SchemaRegistryApi\Registry\BlockingRegistry;
use FlixTech\SchemaRegistryApi\Registry\PromisingRegistry;
use FlixTech\SchemaRegistryApi\Registry\CachedRegistry;
use FlixTech\SchemaRegistryApi\Registry\Decorators\BlockingDecorator;
use FlixTech\SchemaRegistryApi\Registry\GuzzlePromiseAsyncRegistry;
use FlixTech\SchemaRegistryApi\Registry\Decorators\CachingDecorator;
use FlixTech\SchemaRegistryApi\Registry\Cache\AvroObjectCacheAdapter;
use FlixTech\SchemaRegistryApi\Registry\Cache\DoctrineCacheAdapter;
use Doctrine\Common\Cache\ArrayCache;
use GuzzleHttp\Client;

$asyncApi = new PromisingRegistry(
$asyncApi = new GuzzlePromiseAsyncRegistry(
new Client(['base_uri' => 'registry.example.com'])
);

$syncApi = new BlockingRegistry($asyncApi);
$syncApi = new BlockingDecorator($asyncApi);

$doctrineCachedSyncApi = new CachedRegistry(
$doctrineCachedSyncApi = new CachingDecorator(
$asyncApi,
new DoctrineCacheAdapter(
new ArrayCache()
)
);

// All adapters support both APIs, for async APIs additional fulfillment callbacks will be registered.
$avroObjectCachedAsyncApi = new CachedRegistry(
$syncApi,
$avroObjectCachedAsyncApi = new CachingDecorator(
$asyncApi,
new AvroObjectCacheAdapter()
);

Expand All @@ -212,7 +212,7 @@ $sha1HashFunction = static function (AvroSchema $schema) {
};

// Pass the hash function as optional 3rd parameter to the CachedRegistry constructor
$avroObjectCachedAsyncApi = new CachedRegistry(
$avroObjectCachedAsyncApi = new CachingDecorator(
$syncApi,
new AvroObjectCacheAdapter(),
$sha1HashFunction
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"php": "^7.4|^8.0|8.1.*",
"ext-curl": "*",
"ext-json": "*",
"psr/http-client": "~1.0",
"guzzlehttp/guzzle": "^7.0",
"guzzlehttp/promises": "^1.4.0",
"guzzlehttp/psr7": "^1.7",
Expand Down
3 changes: 0 additions & 3 deletions src/AsynchronousRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use FlixTech\SchemaRegistryApi\Schema\AvroReference;
use GuzzleHttp\Promise\PromiseInterface;

/**
* {@inheritdoc}
*/
interface AsynchronousRegistry extends Registry
{
/**
Expand Down
32 changes: 32 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace FlixTech\SchemaRegistryApi;

final class Constants
{
public const COMPATIBILITY_NONE = 'NONE';
public const COMPATIBILITY_BACKWARD = 'BACKWARD';
public const COMPATIBILITY_BACKWARD_TRANSITIVE = 'BACKWARD_TRANSITIVE';
public const COMPATIBILITY_FORWARD = 'FORWARD';
public const COMPATIBILITY_FORWARD_TRANSITIVE = 'FORWARD_TRANSITIVE';
public const COMPATIBILITY_FULL = 'FULL';
public const COMPATIBILITY_FULL_TRANSITIVE = 'FULL_TRANSITIVE';
public const VERSION_LATEST = 'latest';
public const ACCEPT = 'Accept';
public const ACCEPT_HEADER = [self::ACCEPT => 'application/vnd.schemaregistry.v1+json'];
public const CONTENT_TYPE_HEADER = [self::CONTENT_TYPE => 'application/vnd.schemaregistry.v1+json'];
public const CONTENT_TYPE = 'Content-Type';
public const AVRO_TYPE = 'AVRO';
public const JSON_TYPE = 'JSON';
public const PROTOBUF_TYPE = 'PROTOBUF';

private function __construct()
{
}

private function __clone()
{
}
}
55 changes: 51 additions & 4 deletions src/Constants/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,62 @@

namespace FlixTech\SchemaRegistryApi\Constants;

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_NONE instead
*/
const COMPATIBILITY_NONE = 'NONE';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_BACKWARD instead
*/
const COMPATIBILITY_BACKWARD = 'BACKWARD';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_BACKWARD instead
*/
const COMPATIBILITY_BACKWARD_TRANSITIVE = 'BACKWARD_TRANSITIVE';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_FORWARD instead
*/
const COMPATIBILITY_FORWARD = 'FORWARD';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_FORWARD_TRANSITIVE instead
*/
const COMPATIBILITY_FORWARD_TRANSITIVE = 'FORWARD_TRANSITIVE';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_FULL instead
*/
const COMPATIBILITY_FULL = 'FULL';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::COMPATIBILITY_FULL_TRANSITIVE instead
*/
const COMPATIBILITY_FULL_TRANSITIVE = 'FULL_TRANSITIVE';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::VERSION_LATEST instead
*/
const VERSION_LATEST = 'latest';
const ACCEPT_HEADER_KEY = 'Accept';
const ACCEPT_HEADER = [ACCEPT_HEADER_KEY => 'application/vnd.schemaregistry.v1+json'];
const CONTENT_TYPE_HEADER_KEY = 'Content-Type';
const CONTENT_TYPE_HEADER = [CONTENT_TYPE_HEADER_KEY => 'application/vnd.schemaregistry.v1+json'];

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::ACCEPT instead
*/
const ACCEPT = 'Accept';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::ACCEPT_HEADER instead
*/
const ACCEPT_HEADER = [ACCEPT => 'application/vnd.schemaregistry.v1+json'];

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::CONTENT_TYPE instead
*/
const CONTENT_TYPE = 'Content-Type';

/**
* @deprecated Use \FlixTech\SchemaRegistryApi\Constants::CONTENT_TYPE_HEADER instead
*/
const CONTENT_TYPE_HEADER = [CONTENT_TYPE => 'application/vnd.schemaregistry.v1+json'];
5 changes: 2 additions & 3 deletions src/Exception/AbstractSchemaRegistryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace FlixTech\SchemaRegistryApi\Exception;

use LogicException;
use RuntimeException;
use RuntimeException as PHPRuntimeException;

abstract class AbstractSchemaRegistryException extends RuntimeException implements SchemaRegistryException
abstract class AbstractSchemaRegistryException extends PHPRuntimeException implements SchemaRegistryException
{
public const ERROR_CODE = 0;

Expand Down
61 changes: 22 additions & 39 deletions src/Exception/ExceptionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace FlixTech\SchemaRegistryApi\Exception;

use Exception;
use FlixTech\SchemaRegistryApi\Json;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use function array_key_exists;
use function sprintf;

Expand Down Expand Up @@ -38,60 +37,35 @@ private function __construct()
/**
* Maps a RequestException to the internal SchemaRegistryException types.
*
* @param RequestException $exception
* @param ResponseInterface $response
*
* @return SchemaRegistryException
*
* @throws RuntimeException
*/
public function __invoke(RequestException $exception): SchemaRegistryException
public function __invoke(ResponseInterface $response): SchemaRegistryException
{
$response = $this->guardAgainstMissingResponse($exception);
$decodedBody = $this->guardAgainstMissingErrorCode($response);
$this->guardAgainstValidHTPPCode($response);

$decodedBody = Json::decodeResponse($response);
$this->guardAgainstMissingErrorCode($decodedBody);
$errorCode = $decodedBody[self::ERROR_CODE_FIELD_NAME];
$errorMessage = $decodedBody[self::ERROR_MESSAGE_FIELD_NAME];

return $this->mapErrorCodeToException($errorCode, $errorMessage);
}

private function guardAgainstMissingResponse(RequestException $exception): ResponseInterface
public function isHttpError(ResponseInterface $response): bool
{
$response = $exception->getResponse();

if (!$response) {
throw new RuntimeException('RequestException has no response to inspect', 0, $exception);
}

return $response;
return $response->getStatusCode() >= 400 && $response->getStatusCode() < 600;
}

/**
* @param ResponseInterface $response
* @return array<mixed, mixed>
* @param array<int|string,mixed> $decodedBody
*/
private function guardAgainstMissingErrorCode(ResponseInterface $response): array
private function guardAgainstMissingErrorCode(array $decodedBody): void
{
try {
$decodedBody = \GuzzleHttp\json_decode((string) $response->getBody(), true);

if (!is_array($decodedBody) || !array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
throw new RuntimeException(
sprintf(
'Invalid message body received - cannot find "error_code" field in response body "%s"',
(string) $response->getBody()
)
);
}

return $decodedBody;
} catch (Exception $e) {
if (!is_array($decodedBody) || !array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
throw new RuntimeException(
sprintf(
'Invalid message body received - cannot find "error_code" field in response body "%s"',
(string) $response->getBody()
),
$e->getCode(),
$e
'Invalid message body received - cannot find "error_code" field in response body.'
);
}
}
Expand Down Expand Up @@ -133,4 +107,13 @@ private function mapErrorCodeToException(int $errorCode, string $errorMessage):
throw new RuntimeException(sprintf('Unknown error code "%d"', $errorCode));
}
}

private function guardAgainstValidHTPPCode(ResponseInterface $response): void
{
if (!$this->isHttpError($response)) {
throw new RuntimeException(
sprintf('Cannot process response without invalid HTTP code %d', $response->getStatusCode()),
);
}
}
}
17 changes: 17 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace FlixTech\SchemaRegistryApi\Exception;

use LogicException as PHPLogicException;

class LogicException extends PHPLogicException implements SchemaRegistryException
{
public const ERROR_CODE = 99997;

public static function errorCode(): int
{
return self::ERROR_CODE;
}
}
17 changes: 17 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace FlixTech\SchemaRegistryApi\Exception;

use RuntimeException as PHPRuntimeException;

class RuntimeException extends PHPRuntimeException implements SchemaRegistryException
{
public const ERROR_CODE = 99998;

public static function errorCode(): int
{
return self::ERROR_CODE;
}
}
Loading