Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
.idea
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
}
],
"autoload": {
"classmap": [
"src/"
]
"psr-4": {
"Gorse\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.3"
},
"require": {
"php": "~8.2.0 || ~8.3.0",
"guzzlehttp/guzzle": "^7.0",
"ext-redis": "*"
}
Expand Down
31 changes: 31 additions & 0 deletions src/Feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Gorse;

readonly class Feedback implements \JsonSerializable
{
public function __construct(
public string $feedbackType,
public string $userId,
public string $itemId,
public string $timestamp,
public ?string $comment = null,
) {
}

public function jsonSerialize(): array
{
$result = [
'FeedbackType' => $this->feedbackType,
'UserId' => $this->userId,
'ItemId' => $this->itemId,
'Timestamp' => $this->timestamp,
];

if ($this->comment) {
$result['Comment'] = $this->comment;
}

return $result;
}
}
102 changes: 22 additions & 80 deletions src/Gorse.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,15 @@
<?php
<?php declare(strict_types=1);

use GuzzleHttp\Exception\GuzzleException;

class User implements JsonSerializable
{
public string $userId;
public array $labels;

public function __construct(string $userId, array $labels)
{
$this->userId = $userId;
$this->labels = $labels;
}

public function jsonSerialize(): array
{
return [
'UserId' => $this->userId,
'Labels' => $this->labels,
];
}

public static function fromJSON($json): User
{
return new User($json->UserId, $json->Labels);
}
}

class Feedback implements JsonSerializable
{
public string $feedback_type;
public string $user_id;
public string $item_id;
public string $timestamp;

public function __construct(string $feedback_type, string $user_id, string $item_id, string $timestamp)
{
$this->feedback_type = $feedback_type;
$this->user_id = $user_id;
$this->item_id = $item_id;
$this->timestamp = $timestamp;
}

public function jsonSerialize(): array
{
return [
'FeedbackType' => $this->feedback_type,
'UserId' => $this->user_id,
'ItemId' => $this->item_id,
'Timestamp' => $this->timestamp,
];
}
}

class RowAffected
{
public int $rowAffected;
namespace Gorse;

public static function fromJSON($json): RowAffected
{
$rowAffected = new RowAffected();
$rowAffected->rowAffected = $json->RowAffected;
return $rowAffected;
}
}
use GuzzleHttp\Exception\GuzzleException;

final class Gorse
final readonly class Gorse
{
private string $endpoint;
private string $apiKey;

function __construct(string $endpoint, string $apiKey)
{
$this->endpoint = $endpoint;
$this->apiKey = $apiKey;
function __construct(
private string $endpoint,
private string $apiKey
) {
}

/**
Expand All @@ -87,46 +23,52 @@ function insertUser(User $user): RowAffected
/**
* @throws GuzzleException
*/
function getUser(string $user_id): User
function getUser(string $userId): User
{
return User::fromJSON($this->request('GET', '/api/user/' . $user_id, null));
return User::fromJSON($this->request('GET', '/api/user/' . $userId));
}

/**
* @throws GuzzleException
*/
function deleteUser(string $user_id): RowAffected
function deleteUser(string $userId): RowAffected
{
return RowAffected::fromJSON($this->request('DELETE', '/api/user/' . $user_id, null));
return RowAffected::fromJSON($this->request('DELETE', '/api/user/' . $userId));
}

/**
* @param Feedback|Feedback[] $feedback
* @throws GuzzleException
*/
function insertFeedback(array $feedback): RowAffected
function insertFeedback(mixed $feedback): RowAffected
{
if ($feedback instanceof Feedback) {
$feedback = [$feedback];
}

return RowAffected::fromJSON($this->request('POST', '/api/feedback/', $feedback));
}

/**
* @throws GuzzleException
*/
function getRecommend(string $user_id): array
function getRecommend(string $userId): array
{
return $this->request('GET', '/api/recommend/' . $user_id, null);
return $this->request('GET', '/api/recommend/' . $userId);
}

/**
* @throws GuzzleException
*/
private function request(string $method, string $uri, $body)
private function request(string $method, string $uri, \JsonSerializable|array|null $body = null)
{
$client = new GuzzleHttp\Client(['base_uri' => $this->endpoint]);
$options = [GuzzleHttp\RequestOptions::HEADERS => ['X-API-Key' => $this->apiKey]];
if ($body != null) {
$options[GuzzleHttp\RequestOptions::JSON] = $body;
}
$response = $client->request($method, $uri, $options);

return json_decode($response->getBody());
}
}
16 changes: 16 additions & 0 deletions src/RowAffected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Gorse;

readonly class RowAffected
{
public function __construct(
public int $rowAffected
) {
}

public static function fromJSON(object $json): RowAffected
{
return new RowAffected($json->RowAffected);
}
}
26 changes: 26 additions & 0 deletions src/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Gorse;

readonly class User implements \JsonSerializable
{

public function __construct(
public string $userId,
public array $labels
) {
}

public function jsonSerialize(): array
{
return [
'UserId' => $this->userId,
'Labels' => $this->labels,
];
}

public static function fromJSON($json): User
{
return new User($json->UserId, $json->Labels);
}
}
3 changes: 3 additions & 0 deletions test/GorseTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Gorse\Feedback;
use Gorse\Gorse;
use Gorse\User;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\TestCase;
Expand Down