123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace Qiniu\Rtc;
- use Qiniu\Http\Client;
- use Qiniu\Http\Error;
- use Qiniu\Config;
- use Qiniu\Auth;
- class AppClient
- {
- private $auth;
- private $baseURL;
- public function __construct(Auth $auth)
- {
- $this->auth = $auth;
- $this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION);
- }
-
- public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null)
- {
- $params['hub'] = $hub;
- $params['title'] = $title;
- if (!empty($maxUsers)) {
- $params['maxUsers'] = $maxUsers;
- }
- if ($noAutoKickUser !== null) {
- $params['noAutoKickUser'] = $noAutoKickUser;
- }
- $body = json_encode($params);
- $ret = $this->post($this->baseURL, $body);
- return $ret;
- }
-
- public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishRtmp = null, $noAutoKickUser = null)
- {
- $url = $this->baseURL . '/' . $appId;
- $params['hub'] = $hub;
- $params['title'] = $title;
- if (!empty($maxUsers)) {
- $params['maxUsers'] = $maxUsers;
- }
- if ($noAutoKickUser !== null) {
- $params['noAutoKickUser'] = $noAutoKickUser;
- }
- if (!empty($mergePublishRtmp)) {
- $params['mergePublishRtmp'] = $mergePublishRtmp;
- }
- $body = json_encode($params);
- $ret = $this->post($url, $body);
- return $ret;
- }
-
- public function getApp($appId)
- {
- $url = $this->baseURL . '/' . $appId;
- $ret = $this->get($url);
- return $ret;
- }
-
- public function deleteApp($appId)
- {
- $url = $this->baseURL . '/' . $appId;
- list(, $err) = $this->delete($url);
- return $err;
- }
-
- public function listUser($appId, $roomName)
- {
- $url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName);
- $ret = $this->get($url);
- return $ret;
- }
-
- public function kickUser($appId, $roomName, $userId)
- {
- $url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId);
- list(, $err) = $this->delete($url);
- return $err;
- }
-
- public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null)
- {
- if (isset($prefix)) {
- $query['prefix'] = $prefix;
- }
- if (isset($offset)) {
- $query['offset'] = $offset;
- }
- if (isset($limit)) {
- $query['limit'] = $limit;
- }
- if (isset($query) && !empty($query)) {
- $query = '?' . http_build_query($query);
- $url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query);
- } else {
- $url = sprintf("%s/%s/rooms", $this->baseURL, $appId);
- }
- $ret = $this->get($url);
- return $ret;
- }
-
- public function appToken($appId, $roomName, $userId, $expireAt, $permission)
- {
- $params['appId'] = $appId;
- $params['userId'] = $userId;
- $params['roomName'] = $roomName;
- $params['permission'] = $permission;
- $params['expireAt'] = $expireAt;
- $appAccessString = json_encode($params);
- return $this->auth->signWithData($appAccessString);
- }
- private function get($url, $cType = null)
- {
- $rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType);
- $rtcToken['Content-Type'] = $cType;
- $ret = Client::get($url, $rtcToken);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- return array($ret->json(), null);
- }
- private function delete($url, $contentType = 'application/json')
- {
- $rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
- $rtcToken['Content-Type'] = $contentType;
- $ret = Client::delete($url, $rtcToken);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- return array($ret->json(), null);
- }
- private function post($url, $body, $contentType = 'application/json')
- {
- $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType);
- $rtcToken['Content-Type'] = $contentType;
- $ret = Client::post($url, $body, $rtcToken);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = ($ret->body === null) ? array() : $ret->json();
- return array($r, null);
- }
- }
|