Skip to content

Commit

Permalink
Added the v2 friend search API
Browse files Browse the repository at this point in the history
  • Loading branch information
tharangakothalawala committed May 16, 2020
1 parent 557c1a4 commit 251aab1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
4 changes: 2 additions & 2 deletions examples/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion examples/event-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 13 additions & 10 deletions examples/friend-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion examples/message-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/ultra-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/user-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 28 additions & 2 deletions src/Service/FriendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}"));
}
Expand Down Expand Up @@ -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}")
);
}
}

0 comments on commit 251aab1

Please sign in to comment.