Skip to content
Merged
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
35 changes: 17 additions & 18 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ public function __construct(

public function verifyAuthToken(string $authToken): AuthResponse
{
return $this->makeApiCall(
return new AuthResponse($this->makeApiCall(
route: '/auth/verify',
data: [
params: [
'token' => $authToken,
],
type: AuthResponse::class,
);
]
));
}

/**
Expand All @@ -89,32 +88,32 @@ public function verifyAuthToken(string $authToken): AuthResponse
*/
public function attachRegistration(string $regToken, array $user): Credential
{
return $this->makeApiCall(
return new Credential($this->makeApiCall(
route: '/registration/attach',
data: [
params: [
'token' => $regToken,
'user' => $user,
],
type: Credential::class
);
]
));
}

/**
* @template T of object
* @param mixed[] $data
* @param class-string<T> $type
* @return T
* @internal This method is made public for cases where APIs do not have
* native SDK support, but is NOT considered part of the public, stable
* API and is not subject to SemVer.
*
* @param mixed[] $params
* @return mixed[]
*/
private function makeApiCall(string $route, array $data, string $type): object
public function makeApiCall(string $route, array $params): array
{
// TODO: PSR-xx
$json = json_encode($data, JSON_THROW_ON_ERROR);
$json = json_encode($params, JSON_THROW_ON_ERROR);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => sprintf('%s%s', $this->apiHost, $route),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json,
// CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Basic ' . base64_encode(':' . $this->secretKey),
Expand Down Expand Up @@ -147,7 +146,7 @@ private function makeApiCall(string $route, array $data, string $type): object
assert(is_string($response));
$decoded = json_decode($response, true, flags: JSON_THROW_ON_ERROR);
assert(is_array($decoded));
return new $type($decoded['result']);
return $decoded['result'];
} catch (JsonException) {
$this->error();
} finally {
Expand Down