Skip to content

Commit

Permalink
Merge pull request #5 from journy-io/array-null-property-values
Browse files Browse the repository at this point in the history
Add support for array and null property values, add support for disabled api key
  • Loading branch information
hansott authored Jun 29, 2021
2 parents 5ea2c85 + 579f534 commit c9d09eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ $call = $client->upsertUser([
"phone" => "123",
"registered_at" => new \DateTimeImmutable("..."),
"is_admin" => true,
"key_with_empty_value" => "",
"array_of_values" => ["value1", "value2"],
"this_property_will_be_deleted" => null,
],
]);
```
Expand All @@ -138,6 +141,9 @@ $call = $client->upsertAccount([
"plan" => "Pro",
"registered_at" => new \DateTimeImmutable("..."),
"is_paying" => true,
"key_with_empty_value" => "",
"array_of_values" => ["value1", "value2"],
"this_property_will_be_deleted" => null,
],

// optional
Expand Down
37 changes: 26 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,26 @@ private function getRemainingRequests(ResponseInterface $response): int

private function check(ResponseInterface $response)
{
$response->getBody()->rewind();
$json = json_decode($response->getBody()->getContents(), true);

if ($response->getStatusCode() === 401) {
$response->getBody()->rewind();
$json = json_decode($response->getBody()->getContents(), true);
return new CallResult(
false,
false,
$this->getRemainingRequests($response),
$this->getMaxRequests($response),
[$json["message"]]
);
}

if ($response->getStatusCode() === 403) {
return new CallResult(
false,
false,
$this->getRemainingRequests($response),
$this->getMaxRequests($response),
[$json["message"]],
null
[$json["message"]]
);
}

Expand All @@ -107,8 +116,7 @@ private function check(ResponseInterface $response)
true,
$this->getRemainingRequests($response),
$this->getMaxRequests($response),
["rate limited"],
null
[$json["message"]]
);
}
}
Expand Down Expand Up @@ -329,7 +337,7 @@ public function link(array $arguments): CallResult
"deviceId" => $arguments["deviceId"],
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
isset($arguments["userId"]) ? (string) $arguments["userId"] : null,
isset($arguments["userId"]) ? (string) $arguments["userId"] : null,
isset($arguments["email"]) ? (string) $arguments["email"] : null
)
),
Expand Down Expand Up @@ -405,7 +413,14 @@ private function formatProperties(array $properties): array
$formatted = array();

foreach ($properties as $name => $value) {
if (is_int($value) || is_float($value) || is_string($value) || is_bool($value)) {
if (
is_int($value) ||
is_float($value) ||
is_string($value) ||
is_bool($value) ||
is_array($value) ||
is_null($value)
) {
$formatted[$name] = $value;
continue;
}
Expand All @@ -426,7 +441,7 @@ public function upsertUser(array $user): CallResult
$payload = [
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
isset($user["userId"]) ? (string) $user["userId"] : null,
isset($user["userId"]) ? (string) $user["userId"] : null,
isset($user["email"]) ? (string) $user["email"] : null
)
),
Expand Down Expand Up @@ -485,7 +500,7 @@ public function upsertAccount(array $account): CallResult
$payload = [
"identification" => $this->accountIdentifiersToArray(
new AccountIdentified(
isset($account["accountId"]) ? (string) $account["accountId"] : null,
isset($account["accountId"]) ? (string) $account["accountId"] : null,
isset($account["domain"]) ? (string) $account["domain"] : null
)
),
Expand All @@ -501,7 +516,7 @@ function (array $user) {
return [
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
isset($user["userId"]) ? (string) $user["userId"] : null,
isset($user["userId"]) ? (string) $user["userId"] : null,
isset($user["email"]) ? (string) $user["email"] : null
)
),
Expand Down
21 changes: 19 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public function test_it_upserts_user()
"number" => 22,
"date" => $now,
"phone" => new PhoneNumber("number"),
"will_be_deleted" => null,
"array_of_values" => ["a", "b"]
],
]
)
Expand All @@ -164,6 +166,8 @@ public function test_it_upserts_user()
"number" => 22,
"date" => $now->format(DATE_ATOM),
"phone" => "number",
"will_be_deleted" => null,
"array_of_values" => ["a", "b"]
],
],
$payload
Expand Down Expand Up @@ -664,6 +668,19 @@ public function test_it_deals_with_unexpected_error()
);
}

public function test_it_deals_with_forbidden_error()
{
$factory = new Psr17Factory();
$json = '{"message":"the api key is disabled","meta":{"status":403,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
$http = new HttpClientFixed(new Response(403, [], $json));
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]);

$this->assertEquals(
new CallResult(false, false, 0, 0, ["the api key is disabled"], null),
$client->addEvent(Event::forUser("login", UserIdentified::byUserId("1")))
);
}

public function test_it_deals_with_unauthorized_error()
{
$factory = new Psr17Factory();
Expand Down Expand Up @@ -704,12 +721,12 @@ public function test_it_adds_rate_limit_information()
public function test_it_knows_when_rate_limited()
{
$factory = new Psr17Factory();
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
$json = '{"message":"you have sent too much requests","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
$http = new HttpClientFixed(new Response(429, [], $json));
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]);

$this->assertEquals(
new CallResult(false, true, 0, 0, ["rate limited"], null),
new CallResult(false, true, 0, 0, ["you have sent too much requests"], null),
$client->addEvent(Event::forUser("login", UserIdentified::byUserId("1")))
);
}
Expand Down

0 comments on commit c9d09eb

Please sign in to comment.