nti / community-client
轻松连接到社区管理API
0.0.1
2023-08-18 21:48 UTC
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- guzzlehttp/guzzle-services: ^1.0
README
简介
这是一个无需烦恼即可连接到社区内部管理REST API的PHP客户端。
功能
- 易于使用
- 无需获取或生成令牌 - 客户端已处理
- 无需指定除基本URI以外的任何URL
- 无需对JSON进行编码/解码,只需以预期的方式提供数据即可
如何使用
1. 创建新客户端
$client = NTI\CommunityClient\Admin\CommunityClient::factory([ 'realm' => 'master', 'username' => 'admin', 'password' => '1234', 'client_id' => 'admin-cli', 'client_secret' => 'sfdasd', 'baseUri' => 'http://127.0.0.1:8180', ]);
2. 使用它
$client->getUsers(); //Result // Array of users /* Array ( [0] => Array ( 'userid' => 1 'usergroupid' => 0 'membergroupids' => 'displaygroupid' => 0 'username' => Anthony 'email' => no-reply+anthony@insided.com 'posts' => 5 'deleted_posts' => 0 'options' => 1024 'autosubscribe' => 1 'customoptions' => 7 'topics' => 7 'solved' => 1 'ipaddress' => 'usertitle' => Pioneer 'customtitle' => 1 'pmunread' => 1 'subscriptions' => 22 'pmtotal' => 1 'following' => 0 'followers' => 0 'avatar' => https://uploads-us-west-2.insided.com/alliant-en-sandbox/icon/200x200/4032f252-3c4a-46d7-83a0-5733ca2c3418.png 'signature' => 'reputation' => 2 'lastvisit' => 1614769533 'lastactivity' => 1606228559 'insided_sso_customeruid' => 'reviewcount' => 0 'ratingcount' => 0 'lastpostid' => 127 'lastpost' => 1614771269 'joindate' => 1403085355 'likes' => 2 'likes_given' => 3 'blogposts' => 0 'researches' => 0 'rank_id' => 5 'rank_name' => Inspiring 'rank_display_name' => Inspiring 'rank_avatar_icon' => 'rank_avatar_icon_thumb' => 'is_moderator' => 0 ) ) $client->getUser([ 'id' => '39839a9b-de08-4d2c-b91a-a6ce2595b1f3' ]); //Result Array( 'userid' => 3 'usergroupid' => 0 'membergroupids' => 'displaygroupid' => 0 'username' => Eliza 'email' => no-reply+eliza@insided.com 'posts' => 3 'deleted_posts' => 0 'options' => 1024 'autosubscribe' => 1 'customoptions' => 7 'topics' => 11 'solved' => 1 'ipaddress' => 54.182.244.102 'usertitle' => 'customtitle' => 0 'pmunread' => 0 'subscriptions' => 22 'pmtotal' => 1 'following' => 0 'followers' => 0 'avatar' => https://uploads-us-west-2.insided.com/alliant-en-sandbox/icon/200x200/e481abd7-8942-4617-974a-0fa6c9d3a403.png 'signature' => 'reputation' => 2 'lastvisit' => 1662977253 'lastactivity' => 1662982021 'insided_sso_customeruid' => 'reviewcount' => 0 'ratingcount' => 0 'lastpostid' => 142 'lastpost' => 1662977570 'joindate' => 1563816419 'likes' => 2 'likes_given' => 1 'blogposts' => 0 'researches' => 0 'rank_id' => 8 'rank_name' => New Participant 'rank_display_name' => New Participant 'rank_avatar_icon' => 'rank_avatar_icon_thumb' => 'is_moderator' => 0 ) $client->createUser(['data' => [ 'email' => 'test@test.com', 'username' => 'test', 'password' => true, 'user_role' => [ 'roles.registered', 'Option A', 'Option B', 'Other' ], 'profile_field' => [ '12' => 'Smith', '34' => 'John' ], 'sso' => [ 'facebook' => '333ABBDDD', 'oauth2' => '4545ag63274'], ]]); //Result Array( 'user' => Array( 'username' => 'test' 'email' => 'test@test.com', 'joindate' => '1692394871' 'userid' => 25 'sso' => Array() ) ) $client->deleteUser([ 'id' => 1 ]); */
自定义
支持的凭据
可以通过更改社区内部客户端的配置来更改用于身份验证的凭据类型。
目前支持以下凭据:
- 密码凭据,默认使用
- 用于与用户账户进行身份验证
$client = NTI\CommunityClient\Admin\CommunityClient::factory([ ... 'grant_type' => 'password', 'username' => 'admin', 'password' => '1234', ]);
- 客户端凭据
- 用于与客户端服务账户进行身份验证
$client = NTI\CommunityClient\Admin\CommunityClient::factory([ ... 'grant_type' => 'client_credentials', 'client_id' => 'admin-cli', 'client_secret' => '84ab3d98-a0c3-44c7-b532-306f222ce1ff', ]);
注入中间件
可以通过使用 middlewares
关键字在社区内部客户端配置中注入 Guzzle客户端中间件。
例如
use GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; $client = NTI\CommunityClient\Admin\CommunityClient::factory([ ... 'middlewares' => [ // throws exceptions when request fails Middleware::httpErrors(), // other custom middlewares Middleware::mapRequest(function (RequestInterface $request) { return $request; }), ], ]);
更改令牌的保存和存储方式
默认情况下,令牌在运行时保存。这意味着创建新客户端时不会使用先前令牌。
您可以通过实现自己的 TokenStorage
接口来自定义客户端配置中的令牌存储方式,该接口描述了如何存储和检索令牌。
class CustomTokenStorage implements TokenStorage { public function getToken() { // TODO } public function saveToken(array $token) { // TODO } } $client = NTI\CommunityClient\Admin\CommunityClient::factory([ ... 'token_storage' => new CustomTokenStorage(), ]);
自定义社区内部端点
可以通过使用 custom_operations
关键字在社区内部配置中注入 Guzzle服务操作。这样,您可以扩展内置支持的端点以包含自定义端点。
$client = CommunityClient::factory([ ... 'custom_operations' => [ 'getUsersByAttribute' => [ 'uri' => '/auth/realms/{realm}/userapi-rest/users/search-by-attr', 'description' => 'Get users by attribute Returns a list of users, filtered according to query parameters', 'httpMethod' => 'GET', 'parameters' => [ 'realm' => [ 'location' => 'uri', 'description' => 'The Realm name', 'type' => 'string', 'required' => true, ], 'attr' => [ 'location' => 'query', 'type' => 'string', 'required' => true, ], 'value' => [ 'location' => 'query', 'type' => 'string', 'required' => true, ], ], ], ] ]);