From 251aab1384d5e90c9ce1e065ca7b544fbc1c90ad Mon Sep 17 00:00:00 2001 From: Tharanga Kothalawala Date: Sat, 16 May 2020 22:41:28 +0100 Subject: [PATCH] Added the v2 friend search API --- README.md | 8 +++++--- examples/config.php | 4 ++-- examples/event-service.php | 2 +- examples/friend-service.php | 23 +++++++++++++---------- examples/message-service.php | 2 +- examples/ultra-service.php | 2 +- examples/user-service.php | 2 +- src/Service/FriendService.php | 30 ++++++++++++++++++++++++++++-- 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0299e3a..8613513 100755 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ composer require hub/hubid-api-client ## Authentication +Refer to the [https://hubculture.com/developer/home](https://hubculture.com/developer/home) for obtaining the private and public keys. + ```php include '/vendor/autoload.php'; @@ -57,9 +59,9 @@ var_dump($user); Please run the following command to run a PHP server serving examples. ```bash -make demo +HUBID_PRIVATE_KEY=[your-private-key] HUBID_PUBLIC_KEY=[your-public-key] make demo ``` -Browse to [http://localhost:8085/message-service.php](http://localhost:8085/message-service.php). +Browse to [http://localhost:8085/friend-service.php](http://localhost:8085/friend-service.php). -You may look at examples under examples directory. +You may look at examples under `examples` directory. diff --git a/examples/config.php b/examples/config.php index 40e9515..097bed8 100644 --- a/examples/config.php +++ b/examples/config.php @@ -7,8 +7,8 @@ $config = array( 'base_path' => 'https://id.hubculture.com:466', 'verify' => false, - 'private_key' => 'private_5d265de1d9204f6235830ce2', - 'public_key' => 'public_153222247f4cbe2511208120a', + 'private_key' => getenv('HUBID_PRIVATE_KEY'), // __YOUR_KEY__ + 'public_key' => getenv('HUBID_PUBLIC_KEY'), // __YOUR_KEY__ 'client_id' => 10014, 'debug' => true, ); diff --git a/examples/event-service.php b/examples/event-service.php index 0178e23..76dd1f3 100755 --- a/examples/event-service.php +++ b/examples/event-service.php @@ -11,7 +11,7 @@ $redirectUrl = 'http://localhost:8085/event-service.php'; if (empty($_GET['access_token'])) { - $redirectLoginHelper->getAccessToken($redirectUrl); + $redirectLoginHelper->redirectToLoginUrl($redirectUrl); } else { $accessToken = $_GET['access_token']; $refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken); diff --git a/examples/friend-service.php b/examples/friend-service.php index 0d47876..c2ee458 100755 --- a/examples/friend-service.php +++ b/examples/friend-service.php @@ -25,16 +25,19 @@ // example event creation and retrieval $service = new FriendService($config); - $potentialFriendId = 21025; - try { - $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId); - var_dump($response); - } catch (HubIdApiException $ex) { - $service->removeFriend($potentialFriendId); - $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId); - } - var_dump($service->getPendingFriends()); - var_dump($service->removeFriend($potentialFriendId)); + var_dump($service->getFriends()); var_dump($service->getPendingFriends()); + var_dump($service->getFriendRequests()); + var_dump($service->searchFriends('user')); +// +// $potentialFriendId = 123456789; +// try { +// $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId); +// var_dump($response); +// } catch (HubIdApiException $ex) { +// $service->removeFriend($potentialFriendId); +// $response = $service->addFriend('A friend request via the API SDK', $potentialFriendId); +// } +// var_dump($service->removeFriend($potentialFriendId)); } diff --git a/examples/message-service.php b/examples/message-service.php index 3b76d7c..4875cd2 100755 --- a/examples/message-service.php +++ b/examples/message-service.php @@ -13,7 +13,7 @@ $redirectUrl = 'http://localhost:8085/message-service.php'; if (empty($_GET['access_token'])) { - $redirectLoginHelper->getAccessToken($redirectUrl); + $redirectLoginHelper->redirectToLoginUrl($redirectUrl); } else { $accessToken = $_GET['access_token']; $refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken); diff --git a/examples/ultra-service.php b/examples/ultra-service.php index b878d6d..0c3e451 100755 --- a/examples/ultra-service.php +++ b/examples/ultra-service.php @@ -11,7 +11,7 @@ $redirectUrl = 'http://localhost:8085/ultra-service.php'; if (empty($_GET['access_token'])) { - $redirectLoginHelper->getAccessToken($redirectUrl); + $redirectLoginHelper->redirectToLoginUrl($redirectUrl); } else { $accessToken = $_GET['access_token']; $refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken); diff --git a/examples/user-service.php b/examples/user-service.php index 45b50c9..2afee04 100755 --- a/examples/user-service.php +++ b/examples/user-service.php @@ -12,7 +12,7 @@ $redirectUrl = 'http://localhost:8085/user-service.php'; if (empty($_GET['access_token'])) { - $redirectLoginHelper->getAccessToken($redirectUrl); + $redirectLoginHelper->redirectToLoginUrl($redirectUrl); } else { $accessToken = $_GET['access_token']; $refreshedToken = $redirectLoginHelper->getRefreshToken($accessToken); diff --git a/src/Service/FriendService.php b/src/Service/FriendService.php index 608555b..ffeffc1 100755 --- a/src/Service/FriendService.php +++ b/src/Service/FriendService.php @@ -9,6 +9,7 @@ class FriendService extends TokenRefreshingService { const BASE = '/friends'; + const DEFAULT_PAGINATION_LIMIT = 10; /** * This returns all the friends of the current authenticated user. @@ -19,10 +20,10 @@ class FriendService extends TokenRefreshingService * @return array * @see UserService::getFriends() */ - public function getFriends($offset = 0, $limit = 10) + public function getFriends($offset = 0, $limit = self::DEFAULT_PAGINATION_LIMIT) { $offset = intval($offset) === 0 ? 0 : intval($offset); - $limit = intval($limit) === 0 ? 10 : intval($limit); + $limit = intval($limit) === 0 ? self::DEFAULT_PAGINATION_LIMIT : intval($limit); return $this->createResponse($this->get(self::BASE . "/?offset={$offset}&limit={$limit}")); } @@ -126,4 +127,29 @@ public function removeFriend($friendUserId) { return $this->createResponse($this->delete("/friend/{$friendUserId}")); } + + /** + * Use this to search for friends in the current authenticated user's (your) friend list. + * + * @param string $searchKeyword The search term to search for friends in your friend list. + * This can be part of a name or an email address. + * @param int $offset [optional] offset for pagination + * @param int $limit [optional] limit for pagination + * + * @return array + */ + public function searchFriends($searchKeyword, $offset = 0, $limit = self::DEFAULT_PAGINATION_LIMIT) + { + if (empty($searchKeyword)) { + return []; + } + + $offset = intval($offset) === 0 ? 0 : intval($offset); + $limit = intval($limit) === 0 ? self::DEFAULT_PAGINATION_LIMIT : intval($limit); + $searchKeyword = urlencode($searchKeyword); + + return $this->createResponse( + $this->get("/v2/friends/search?search={$searchKeyword}&offset={$offset}&limit={$limit}") + ); + } }