Skip to content

Instantly share code, notes, and snippets.

@bhaktaraz
Created July 17, 2022 08:59
Show Gist options
  • Save bhaktaraz/5301efcc24ee126e85fb9daf6c805578 to your computer and use it in GitHub Desktop.
Save bhaktaraz/5301efcc24ee126e85fb9daf6c805578 to your computer and use it in GitHub Desktop.
gorse.io php client
<?php
namespace App\Services;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
/**
* PHP client for Gorse https://gorse.io/
*
* @author Bhaktaraz Bhatta <[email protected]>
*/
class GorseClient
{
public function __construct(private ContainerBagInterface $params){}
public function execute($url, $method, $parameters = [])
{
$client = new Client();
$response = $client->request(
$method,
$this->params->get('georse_api_base_url') . $url,
[
'body' => json_encode($parameters),
'headers' => ['Content-Type' => 'application/json', 'X-API-Key' => $this->params->get('gorse_api_key')]
]
);
return $response->getBody()->getContents();
}
// Item APIs
/**
* @param $parameters
* @return string
*
* Insert an item. Overwrite if the item exists.
*/
public function insertItem($parameters)
{
return $this->execute('item', 'POST', $parameters);
}
/**
* @return string
*
* Get an item.
*/
public function getItem($item)
{
return $this->execute('item/'.$item, 'GET');
}
/**
* @return string
*
* Modify an item.
*/
public function updateItem($item)
{
return $this->execute('item/'.$item, 'PATCH');
}
/**
* @return string
*
* Delete an item and its feedbacks.
*/
public function deleteItem($item)
{
return $this->execute('item/'.$item, 'DELETE');
}
/**
* @return string
*
* Insert items. Overwrite if items exist.
*/
public function insetItems($parameters)
{
return $this->execute('items', 'POST', $parameters);
}
/**
* @return string
*
* Get items.
*/
public function getItems()
{
return $this->execute('items', 'GET');
}
/**
* @return string
*
* Append a category to an item.
*/
public function appendCategoryToItem($item, $category)
{
return $this->execute('item/'.$item.'/category/'.$category, 'PUT');
}
/**
* @return string
*
* Delete a category from an item.
*/
public function deleteCategoryFromItem($item, $category)
{
return $this->execute('item/'.$item.'/category/'.$category, 'DELETE');
}
// User APIs
/**
* @param $parameters
* @return string
*
* Insert a user. Overwrite if the user exists.
*/
public function insertUser($parameters)
{
return $this->execute('user', 'POST', $parameters);
}
/**
* @param $user
* @return string
*
* Get a user.
*/
public function getUser($user)
{
return $this->execute('user/'.$user, 'GET');
}
/**
* @param $user
* @param $parameters
* @return string
*
* Modify a user.
*/
public function modifyUser($user, $parameters)
{
return $this->execute('user/'.$user, 'PATCH', $parameters);
}
/**
* @param $user
* @return string
*
* Delete a user and his or her feedbacks.
*/
public function deleteUser($user)
{
return $this->execute('user/'.$user, 'DELETE');
}
/**
* @return string
*
* Get users.
*/
public function getUsers()
{
return $this->execute('users', 'GET');
}
// Feedback APIs
/**
* @param $parameters
* @return string
*
* Insert feedbacks. Ignore if exists.
*/
public function insertFeedback($parameters)
{
return $this->execute('feedback', 'POST', $parameters);
}
/**
* @param $parameters
* @return string
*
* Insert feedbacks. Overwrite if exists.
*/
public function updateFeedback($parameters)
{
return $this->execute('feedback', 'PUT', $parameters);
}
/**
* @return string
*
* Get feedbacks.
*/
public function getFeedbacks()
{
return $this->execute('feedback', 'GET');
}
/**
* @return string
*
* Get feedbacks with feedback type.
*/
public function getFeedbacksByType($type)
{
return $this->execute('feedback/'.$type, 'GET');
}
/**
* @return string
*
* Get feedbacks between a user and a item.
*/
public function getFeedbacksBetweenUserAndItem($user, $item)
{
return $this->execute('feedback/'.$user.'/'.$item, 'GET');
}
/**
* @return string
*
* Delete feedbacks between a user and a item.
*/
public function deleteFeedbacksBetweenUserAndItem($user, $item)
{
return $this->execute('feedback/'.$user.'/'.$item, 'DELETE');
}
/**
* @return string
*
* Get feedbacks between a user and a item with feedback type.
*/
public function getFeedbacksBetweenUserAndItemByFeedbackType($type, $user, $item)
{
return $this->execute('feedback/'.$type.'/'.$user.'/'.$item, 'GET');
}
/**
* @return string
*
* Delete feedbacks between a user and a item with feedback type.
*/
public function deleteFeedbacksBetweenUserAndItemByFeedbackType($type, $user, $item)
{
return $this->execute('feedback/'.$type.'/'.$user.'/'.$item, 'DELETE');
}
/**
* @return string
*
* Get feedback by user id.
*/
public function getFeedbacksByUserId($user)
{
return $this->execute('user/'.$user.'/feedback', 'GET');
}
/**
* @return string
*
* Get feedbacks by user id with feedback type.
*/
public function getFeedbacksByUserWithFeedbackType($user, $type)
{
return $this->execute('user/'.$user.'/feedback/'.$type, 'GET');
}
/**
* @return string
*
* Get feedback by item id.
*/
public function getFeedbackByItemId($item)
{
return $this->execute('item/'.$item.'/feedback', 'GET');
}
/**
* @return string
*
* Get feedbacks by item id with feedback type.
*/
public function getFeedbackByItemIdWithFeedbackType($item, $type)
{
return $this->execute('item/'.$item.'/feedback/'.$type, 'GET');
}
// Recommendation APIs
/**
* @return string
*
* Get latest items.
*/
public function getLatestItems()
{
return $this->execute('latest', 'GET');
}
/**
* @param $category
* @return string
*
* Get latest items in specified category.
*/
public function getLatestItemsByCategory($category): string
{
return $this->execute('latest/'. $category, 'GET');
}
/**
* @return string
*
* Get popular items.
*/
public function getPopularItems()
{
return $this->execute('popular', 'GET');
}
/**
* @param $category
* @return string
*
* Get popular items in specified category.
*/
public function getPopularItemsByCategory($category)
{
return $this->execute('popular/'. $category, 'GET');
}
/**
* @param $user
* @return string
*
* Get recommendation for user.
*/
public function getRecommendationForUser($user)
{
return $this->execute('recommend/'. $user, 'GET');
}
/**
* @param $user
* @param $category
* @return string
*
* Get recommendation for user in specified category.
*/
public function getRecommendationForUserByCategory($user, $category)
{
return $this->execute('recommend/'.$user.'/' . $category, 'GET');
}
/**
* @param $item
* @return string
*
* Get neighbors of a item.
*/
public function getNeighborsOfItem($item)
{
return $this->execute('item/'.$item.'/neighbors', 'GET');
}
/**
* @param $user
* @return string
*
* Get neighbors of a user.
*/
public function getNeighborsOfUser($user)
{
return $this->execute('user/'. $user.'/neighbors', 'GET');
}
/**
* @param $item
* @param $category
* @return string
*
* Get neighbors of a item in specified category.
*/
public function getNeighborsOfItemByCategory($item, $category): string
{
return $this->execute('item/'. $item.'/neighbors/'.$category, 'GET');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment