Skip to content

refactor: introduce async method and wip inline types #100

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

Merged
merged 15 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions run_checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@
exit(1);
})->wait();

$webhookSecret = "secret";
$webhookData = "data";
$webhookHeader = "v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db";
$isValidWebhookSign = \Fingerprint\ServerAPI\Webhook\WebhookVerifier::IsValidWebhookSignature($webhookHeader, $webhookData, $webhookSecret);
if($isValidWebhookSign) {
fwrite(STDOUT, sprintf("\n\nVerified webhook signature\n"));
} else {
fwrite(STDERR, sprintf("\n\nWebhook signature verification failed\n"));
exit(1);
}

// Enable the deprecated ArrayAccess return type warning again if needed
error_reporting(error_reporting() | E_DEPRECATED);

Expand Down
72 changes: 12 additions & 60 deletions src/Api/FingerprintApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,17 @@ public function getEvent(string $request_id): array

switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\EventResponse'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 403:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorEvent403Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 404:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorEvent404Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;
}
Expand Down Expand Up @@ -204,29 +192,17 @@ function ($e) {

switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\EventResponse'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 403:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorEvent403Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 404:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorEvent404Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;
}
Expand Down Expand Up @@ -299,29 +275,17 @@ public function getVisits(string $visitor_id, ?string $request_id = null, ?strin

switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 403:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorVisits403'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 429:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ManyRequestsResponse'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;
}
Expand Down Expand Up @@ -380,29 +344,17 @@ function ($e) {

switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\Response'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 403:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ErrorVisits403'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;

case 429:
$data = ObjectSerializer::deserialize(
$response,
'\Fingerprint\ServerAPI\Model\ManyRequestsResponse'
);
$e->setResponseObject($data);
$e->setResponseObject($response);

break;
}
Expand Down
74 changes: 0 additions & 74 deletions src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

namespace Fingerprint\ServerAPI;

use DateTime;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -98,23 +97,6 @@ public static function sanitizeForSerialization(mixed $data, ?string $format = n
return (string) $data;
}

/**
* Sanitize filename by removing path.
* e.g. ../../sun.gif becomes sun.gif.
*
* @param string $filename filename to be sanitized
*
* @return string the sanitized filename
*/
public static function sanitizeFilename(string $filename): string
{
if (preg_match('/.*[\/\\\](.*)$/', $filename, $match)) {
return $match[1];
}

return $filename;
}

/**
* Take value and turn it into a string suitable for inclusion in
* the path, by url-encoding.
Expand Down Expand Up @@ -148,38 +130,6 @@ public static function toQueryValue(array|\DateTime|string $object, ?string $for
return self::toString($object, $format);
}

/**
* Take value and turn it into a string suitable for inclusion in
* the header. If it's a string, pass through unchanged
* If it's a datetime object, format it in RFC3339.
*
* @param string $value a string which will be part of the header
*
* @return string the header string
*/
public static function toHeaderValue(string $value): string
{
return self::toString($value);
}

/**
* Take value and turn it into a string suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in RFC3339.
*
* @param \SplFileObject|string $value the value of the form parameter
*
* @return string the form string
*/
public static function toFormValue(\SplFileObject|string $value): string
{
if ($value instanceof \SplFileObject) {
return $value->getRealPath();
}

return self::toString($value);
}

/**
* Take value and turn it into a string suitable for inclusion in
* the parameter. If it's a string, pass through unchanged
Expand All @@ -200,30 +150,6 @@ public static function toString(\DateTime|string $value, ?string $format = null)
return $value;
}

/**
* Serialize an array to a string.
*
* @param array $collection collection to serialize to a string
* @param string $collectionFormat the format use for serialization (csv,
* ssv, tsv, pipes, multi)
* @param bool $allowCollectionFormatMulti allow collection format to be a multidimensional array
*/
public static function serializeCollection(array $collection, string $collectionFormat, ?bool $allowCollectionFormatMulti = false): string
{
if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) {
// http_build_query() almost does the job for us. We just
// need to fix the result of multidimensional arrays.
return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
}

return match ($collectionFormat) {
'pipes' => implode('|', $collection),
'tsv' => implode("\t", $collection),
'ssv' => implode(' ', $collection),
default => implode(',', $collection),
};
}

/**
* Deserialize a JSON string into an object.
*
Expand Down
12 changes: 2 additions & 10 deletions template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ use \GuzzleHttp\Exception\GuzzleException;
{{#responses}}
{{#dataType}}
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
$data = ObjectSerializer::deserialize(
$response,
'{{dataType}}'
);
$e->setResponseObject($data);
$e->setResponseObject($response);
break;
{{/dataType}}
{{/responses}}
Expand Down Expand Up @@ -198,11 +194,7 @@ use \GuzzleHttp\Exception\GuzzleException;
{{#responses}}
{{#dataType}}
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
$data = ObjectSerializer::deserialize(
$response,
'{{dataType}}'
);
$e->setResponseObject($data);
$e->setResponseObject($response);
break;
{{/dataType}}
{{/responses}}
Expand Down
Loading