Skip to content

Commit c9d09eb

Browse files
authored
Merge pull request #5 from journy-io/array-null-property-values
Add support for array and null property values, add support for disabled api key
2 parents 5ea2c85 + 579f534 commit c9d09eb

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ $call = $client->upsertUser([
118118
"phone" => "123",
119119
"registered_at" => new \DateTimeImmutable("..."),
120120
"is_admin" => true,
121+
"key_with_empty_value" => "",
122+
"array_of_values" => ["value1", "value2"],
123+
"this_property_will_be_deleted" => null,
121124
],
122125
]);
123126
```
@@ -138,6 +141,9 @@ $call = $client->upsertAccount([
138141
"plan" => "Pro",
139142
"registered_at" => new \DateTimeImmutable("..."),
140143
"is_paying" => true,
144+
"key_with_empty_value" => "",
145+
"array_of_values" => ["value1", "value2"],
146+
"this_property_will_be_deleted" => null,
141147
],
142148

143149
// optional

src/Client.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,26 @@ private function getRemainingRequests(ResponseInterface $response): int
7777

7878
private function check(ResponseInterface $response)
7979
{
80+
$response->getBody()->rewind();
81+
$json = json_decode($response->getBody()->getContents(), true);
82+
8083
if ($response->getStatusCode() === 401) {
81-
$response->getBody()->rewind();
82-
$json = json_decode($response->getBody()->getContents(), true);
84+
return new CallResult(
85+
false,
86+
false,
87+
$this->getRemainingRequests($response),
88+
$this->getMaxRequests($response),
89+
[$json["message"]]
90+
);
91+
}
8392

93+
if ($response->getStatusCode() === 403) {
8494
return new CallResult(
8595
false,
8696
false,
8797
$this->getRemainingRequests($response),
8898
$this->getMaxRequests($response),
89-
[$json["message"]],
90-
null
99+
[$json["message"]]
91100
);
92101
}
93102

@@ -107,8 +116,7 @@ private function check(ResponseInterface $response)
107116
true,
108117
$this->getRemainingRequests($response),
109118
$this->getMaxRequests($response),
110-
["rate limited"],
111-
null
119+
[$json["message"]]
112120
);
113121
}
114122
}
@@ -329,7 +337,7 @@ public function link(array $arguments): CallResult
329337
"deviceId" => $arguments["deviceId"],
330338
"identification" => $this->userIdentifiersToArray(
331339
new UserIdentified(
332-
isset($arguments["userId"]) ? (string) $arguments["userId"] : null,
340+
isset($arguments["userId"]) ? (string) $arguments["userId"] : null,
333341
isset($arguments["email"]) ? (string) $arguments["email"] : null
334342
)
335343
),
@@ -405,7 +413,14 @@ private function formatProperties(array $properties): array
405413
$formatted = array();
406414

407415
foreach ($properties as $name => $value) {
408-
if (is_int($value) || is_float($value) || is_string($value) || is_bool($value)) {
416+
if (
417+
is_int($value) ||
418+
is_float($value) ||
419+
is_string($value) ||
420+
is_bool($value) ||
421+
is_array($value) ||
422+
is_null($value)
423+
) {
409424
$formatted[$name] = $value;
410425
continue;
411426
}
@@ -426,7 +441,7 @@ public function upsertUser(array $user): CallResult
426441
$payload = [
427442
"identification" => $this->userIdentifiersToArray(
428443
new UserIdentified(
429-
isset($user["userId"]) ? (string) $user["userId"] : null,
444+
isset($user["userId"]) ? (string) $user["userId"] : null,
430445
isset($user["email"]) ? (string) $user["email"] : null
431446
)
432447
),
@@ -485,7 +500,7 @@ public function upsertAccount(array $account): CallResult
485500
$payload = [
486501
"identification" => $this->accountIdentifiersToArray(
487502
new AccountIdentified(
488-
isset($account["accountId"]) ? (string) $account["accountId"] : null,
503+
isset($account["accountId"]) ? (string) $account["accountId"] : null,
489504
isset($account["domain"]) ? (string) $account["domain"] : null
490505
)
491506
),
@@ -501,7 +516,7 @@ function (array $user) {
501516
return [
502517
"identification" => $this->userIdentifiersToArray(
503518
new UserIdentified(
504-
isset($user["userId"]) ? (string) $user["userId"] : null,
519+
isset($user["userId"]) ? (string) $user["userId"] : null,
505520
isset($user["email"]) ? (string) $user["email"] : null
506521
)
507522
),

tests/ClientTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public function test_it_upserts_user()
140140
"number" => 22,
141141
"date" => $now,
142142
"phone" => new PhoneNumber("number"),
143+
"will_be_deleted" => null,
144+
"array_of_values" => ["a", "b"]
143145
],
144146
]
145147
)
@@ -164,6 +166,8 @@ public function test_it_upserts_user()
164166
"number" => 22,
165167
"date" => $now->format(DATE_ATOM),
166168
"phone" => "number",
169+
"will_be_deleted" => null,
170+
"array_of_values" => ["a", "b"]
167171
],
168172
],
169173
$payload
@@ -664,6 +668,19 @@ public function test_it_deals_with_unexpected_error()
664668
);
665669
}
666670

671+
public function test_it_deals_with_forbidden_error()
672+
{
673+
$factory = new Psr17Factory();
674+
$json = '{"message":"the api key is disabled","meta":{"status":403,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
675+
$http = new HttpClientFixed(new Response(403, [], $json));
676+
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]);
677+
678+
$this->assertEquals(
679+
new CallResult(false, false, 0, 0, ["the api key is disabled"], null),
680+
$client->addEvent(Event::forUser("login", UserIdentified::byUserId("1")))
681+
);
682+
}
683+
667684
public function test_it_deals_with_unauthorized_error()
668685
{
669686
$factory = new Psr17Factory();
@@ -704,12 +721,12 @@ public function test_it_adds_rate_limit_information()
704721
public function test_it_knows_when_rate_limited()
705722
{
706723
$factory = new Psr17Factory();
707-
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
724+
$json = '{"message":"you have sent too much requests","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}';
708725
$http = new HttpClientFixed(new Response(429, [], $json));
709726
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]);
710727

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

0 commit comments

Comments
 (0)