diff --git a/src/Gorse.php b/src/Gorse.php index cd16322..e980702 100644 --- a/src/Gorse.php +++ b/src/Gorse.php @@ -76,46 +76,326 @@ function __construct(string $endpoint, string $apiKey) $this->apiKey = $apiKey; } + /** + * @throws GuzzleException + */ + function insertFeedback(array $feedbacks): RowAffected + { + return RowAffected::fromJSON($this->request('POST', '/api/feedback', $feedbacks)); + } + + /** + * @throws GuzzleException + */ + function putFeedback(array $feedbacks): RowAffected + { + return RowAffected::fromJSON($this->request('PUT', '/api/feedback', $feedbacks)); + } + + /** + * @throws GuzzleException + */ + function getFeedback(string $cursor, int $n): array + { + return $this->request('GET', '/api/feedback?cursor=' . $cursor . '&n=' . $n, null); + } + + /** + * @throws GuzzleException + */ + function getFeedbacksWithType(string $feedbackType, string $cursor, int $n): array + { + return $this->request('GET', '/api/feedback/' . $feedbackType . '?cursor=' . $cursor . '&n=' . $n, null); + } + + /** + * @throws GuzzleException + */ + function getFeedbackWithUserItem(string $userId, string $itemId): array + { + return $this->request('GET', '/api/feedback/' . $userId . '/' . $itemId, null); + } + + /** + * @throws GuzzleException + */ + function getFeedbackWithTypeUserItem(string $feedbackType, string $userId, string $itemId): Feedback + { + return Feedback::fromJSON($this->request('GET', '/api/feedback/' . $feedbackType . '/' . $userId . '/' . $itemId, null)); + } + + /** + * @throws GuzzleException + */ + function delFeedback(string $feedbackType, string $userId, string $itemId): Feedback + { + return Feedback::fromJSON($this->request('DELETE', '/api/feedback/' . $feedbackType . '/' . $userId . '/' . $itemId, null)); + } + + /** + * @throws GuzzleException + */ + function delFeedbackWithUserItem(string $userId, string $itemId): array + { + return $this->request('DELETE', '/api/feedback/' . $userId . '/' . $itemId, null); + } + + /** + * @throws GuzzleException + */ + function getItemFeedbacks(string $itemId) + { + return $this->request('GET', "/api/item/$itemId/feedback", null); + } + + /** + * @throws GuzzleException + */ + function getItemFeedbacksWithType(string $itemId, string $feedbackType) + { + return $this->request('GET', "/api/item/$itemId/feedback/$feedbackType", null); + } + + /** + * @throws GuzzleException + */ + function getUserFeedbacks(string $userId) + { + return $this->request('GET', "/api/user/$userId/feedback", null); + } + + /** + * @throws GuzzleException + */ + function getUserFeedbacksWithType(string $userId, string $feedbackType) + { + return $this->request('GET', "/api/user/$userId/feedback/$feedbackType", null); + } + + /** + * @throws GuzzleException + */ + function getItemLatest(string $userId, int $n, int $offset) + { + return $this->request('GET', "/api/latest?user-id=$userId&n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemLatestWithCategory(string $userId, string $category, int $n, int $offset) + { + return $this->request('GET', "/api/latest?user-id=$userId&category=$category&n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemPopular(string $userId, int $n, int $offset) + { + return $this->request('GET', "/api/popular?user-id=$userId&n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemPopularWithCategory(string $userId, string $category, int $n, int $offset) + { + return $this->request('GET', "/api/popular/$category?user-id=$userId&n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemRecommend(string $userId, array $categories, string $writeBackType, string $writeBackDelay, int $n, int $offset) + { + $queryCategories = http_build_query(['category' => $categories], '', '&'); + return $this->request('GET', "/api/recommend/$userId?write-back-type=$writeBackType&write-back-delay=$writeBackDelay&n=$n&offset=$offset&$queryCategories", null); + } + + /** + * @throws GuzzleException + */ + function getItemRecommendWithCategory(string $userId, string $category, string $writeBackType, string $writeBackDelay, int $n, int $offset) + { + return $this->request('GET', "/api/recommend/$userId/$category?write-back-type=$writeBackType&write-back-delay=$writeBackDelay&n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getSessionItemRecommend(array $feedbacks, int $n, int $offset) + { + return $this->request('POST', "/api/session/recommend?n=$n&offset=$offset", $feedbacks); + } + + /** + * @throws GuzzleException + */ + function getSessionItemRecommendWithCategory(array $feedbacks, string $category, int $n, int $offset) + { + return $this->request('POST', "/api/session/recommend/$category?n=$n&offset=$offset", $feedbacks); + } + + /** + * @throws GuzzleException + */ + function getUserNeighbors(string $userId, int $n, int $offset) + { + return $this->request('GET', "/api/user/$userId/neighbors?n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemNeighbors(string $itemId, int $n, int $offset) + { + return $this->request('GET', "/api/item/$itemId/neighbors?n=$n&offset=$offset", null); + } + + /** + * @throws GuzzleException + */ + function getItemNeighborsWithCategory(string $itemId, string $category, int $n, int $offset) + { + return $this->request('GET', "/api/item/$itemId/neighbors/$category?n=$n&offset=$offset", null); + } + /** * @throws GuzzleException */ function insertUser(User $user): RowAffected { - return RowAffected::fromJSON($this->request('POST', '/api/user/', $user)); + return RowAffected::fromJSON($this->request('POST', '/api/user', $user->jsonSerialize())); } - + /** * @throws GuzzleException */ - function getUser(string $user_id): User + function insertUsers(array $users): RowAffected { - return User::fromJSON($this->request('GET', '/api/user/' . $user_id, null)); + $usersData = array_map(function($user) { + return $user->jsonSerialize(); + }, $users); + return RowAffected::fromJSON($this->request('POST', '/api/users', $usersData)); } - + /** * @throws GuzzleException */ - function deleteUser(string $user_id): RowAffected + function updateUser(string $userId, array $userPatch): RowAffected { - return RowAffected::fromJSON($this->request('DELETE', '/api/user/' . $user_id, null)); + return RowAffected::fromJSON($this->request('PATCH', "/api/user/$userId", $userPatch)); } - + /** * @throws GuzzleException */ - function insertFeedback(array $feedback): RowAffected + function getUser(string $userId): User { - return RowAffected::fromJSON($this->request('POST', '/api/feedback/', $feedback)); + return User::fromJSON($this->request('GET', "/api/user/$userId", null)); } - + + /** + * @throws GuzzleException + */ + function getUsers(string $cursor = '', int $n = 0) + { + $queryParams = http_build_query(['cursor' => $cursor, 'n' => $n]); + return $this->request('GET', "/api/users?$queryParams", null); + } + + /** + * @throws GuzzleException + */ + function deleteUser(string $userId): RowAffected + { + return RowAffected::fromJSON($this->request('DELETE', "/api/user/$userId", null)); + } + + /** + * @throws GuzzleException + */ + function insertItem(array $item): RowAffected + { + return RowAffected::fromJSON($this->request('POST', '/api/item', $item)); + } + + /** + * @throws GuzzleException + */ + function insertItems(array $items): RowAffected + { + return RowAffected::fromJSON($this->request('POST', '/api/items', $items)); + } + + /** + * @throws GuzzleException + */ + function updateItem(string $itemId, array $itemPatch): RowAffected + { + return RowAffected::fromJSON($this->request('PATCH', "/api/item/$itemId", $itemPatch)); + } + + /** + * @throws GuzzleException + */ + function getItem(string $itemId) + { + return $this->request('GET', "/api/item/$itemId", null); + } + + /** + * @throws GuzzleException + */ + function getItems(string $cursor = '', int $n = 0) + { + $queryParams = http_build_query(['cursor' => $cursor, 'n' => $n]); + return $this->request('GET', "/api/items?$queryParams", null); + } + /** * @throws GuzzleException */ - function getRecommend(string $user_id): array + function deleteItem(string $itemId): RowAffected { - return $this->request('GET', '/api/recommend/' . $user_id, null); + return RowAffected::fromJSON($this->request('DELETE', "/api/item/$itemId", null)); + } + + /** + * @throws GuzzleException + */ + function putItemCategory(string $itemId, string $category): RowAffected + { + return RowAffected::fromJSON($this->request('PUT', "/api/item/$itemId/category/$category", null)); + } + + /** + * @throws GuzzleException + */ + function delItemCategory(string $itemId, string $category): RowAffected + { + return RowAffected::fromJSON($this->request('DELETE', "/api/item/$itemId/category/$category", null)); + } + + + /** + * @throws GuzzleException + */ + function healthLive() + { + return $this->request('GET', "/api/health/live", null); + } + + /** + * @throws GuzzleException + */ + function healthReady() + { + return $this->request('GET', "/api/health/ready", null); } + /** * @throws GuzzleException */ @@ -129,4 +409,4 @@ private function request(string $method, string $uri, $body) $response = $client->request($method, $uri, $options); return json_decode($response->getBody()); } -} \ No newline at end of file +}