From e9f601f7de8d0679e629c18a1e2d946d255e5175 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Fri, 26 Jul 2024 01:20:18 +0530 Subject: [PATCH] Add body param for delete request --- src/Platform/Platform.php | 126 +++++++++++++++++++--------------- src/Platform/PlatformTest.php | 78 +++++++++++++++++---- 2 files changed, 134 insertions(+), 70 deletions(-) diff --git a/src/Platform/Platform.php b/src/Platform/Platform.php index 88285f1b..e88686b7 100644 --- a/src/Platform/Platform.php +++ b/src/Platform/Platform.php @@ -20,14 +20,15 @@ class Platform const API_VERSION = 'v1.0'; const URL_PREFIX = '/restapi'; const KNOWN_PREFIXES = array( - self::URL_PREFIX, - '/rcvideo', '/video', - '/webinar', - '/team-messaging', - '/analytics', - '/ai', - '/scim', - '/cx' + self::URL_PREFIX, + '/rcvideo', + '/video', + '/webinar', + '/team-messaging', + '/analytics', + '/ai', + '/scim', + '/cx' ); /** @var string */ @@ -77,9 +78,9 @@ public function __construct(Client $client, $clientId, $clientSecret, $server, $ $this->_client = $client; $this->_userAgent = (!empty($this->_appName) ? ($this->_appName . (!empty($this->_appVersion) ? '/' . $this->_appVersion : '') . ' ') : '') . - php_uname('s') . '/' . php_uname('r') . ' ' . - 'PHP/' . phpversion() . ' ' . - 'RCPHPSDK/' . SDK::VERSION; + php_uname('s') . '/' . php_uname('r') . ' ' . + 'PHP/' . phpversion() . ' ' . + 'RCPHPSDK/' . SDK::VERSION; } @@ -106,11 +107,19 @@ public function createUrl($path = '', $options = []) $builtUrl .= $this->_server; } - if (!array_reduce(self::KNOWN_PREFIXES, - function ($result, $key) use($path) { - if ($result) { return $result; } else { return str_starts_with( $path, $key ); } - }, - FALSE) && !$hasHttp) { + if ( + !array_reduce( + self::KNOWN_PREFIXES, + function ($result, $key) use ($path) { + if ($result) { + return $result; + } else { + return str_starts_with($path, $key); + } + }, + FALSE + ) && !$hasHttp + ) { $builtUrl .= self::URL_PREFIX . '/' . self::API_VERSION; } @@ -165,17 +174,18 @@ public function loggedIn() public function authUrl($options) { return $this->createUrl(self::AUTHORIZE_ENDPOINT . '?' . http_build_query( - [ - 'response_type' => 'code', - 'redirect_uri' => $options['redirectUri'] ? $options['redirectUri'] : null, - 'client_id' => $this->_clientId, - 'state' => array_key_exists('state',$options) ? $options['state'] : null, - 'brand_id' => array_key_exists('brandId',$options) ? $options['brandId'] : null, - 'display' => array_key_exists('display',$options) ? $options['display'] : null, - 'prompt' => array_key_exists('prompt',$options) ? $options['prompt'] : null, - 'code_challenge' => array_key_exists('code_challenge',$options) ? $options['code_challenge'] : null, - 'code_challenge_method' => array_key_exists('code_challenge_method',$options) ? $options['code_challenge_method'] : null - ]), [ + [ + 'response_type' => 'code', + 'redirect_uri' => $options['redirectUri'] ? $options['redirectUri'] : null, + 'client_id' => $this->_clientId, + 'state' => array_key_exists('state', $options) ? $options['state'] : null, + 'brand_id' => array_key_exists('brandId', $options) ? $options['brandId'] : null, + 'display' => array_key_exists('display', $options) ? $options['display'] : null, + 'prompt' => array_key_exists('prompt', $options) ? $options['prompt'] : null, + 'code_challenge' => array_key_exists('code_challenge', $options) ? $options['code_challenge'] : null, + 'code_challenge_method' => array_key_exists('code_challenge_method', $options) ? $options['code_challenge_method'] : null + ] + ), [ 'addServer' => 'true' ]); } @@ -205,39 +215,41 @@ public function login($options) { if (is_string($options)) { $options = [ - 'username' => func_get_arg(0), + 'username' => func_get_arg(0), 'extension' => func_get_arg(1) ? func_get_arg(1) : null, - 'password' => func_get_arg(2) + 'password' => func_get_arg(2) ]; } - $response = !empty($options['code']) ? $this->requestToken(self::TOKEN_ENDPOINT, [ + $response = !empty($options['code']) ? $this->requestToken( + self::TOKEN_ENDPOINT, + [ - 'grant_type' => 'authorization_code', - 'code' => $options['code'], - 'redirect_uri' => $options['redirectUri'], - 'access_token_ttl' => self::ACCESS_TOKEN_TTL, - 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL + 'grant_type' => 'authorization_code', + 'code' => $options['code'], + 'redirect_uri' => $options['redirectUri'], + 'access_token_ttl' => self::ACCESS_TOKEN_TTL, + 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL - ] + (!empty($options['codeVerifier']) ? [ 'code_verifier' => $options['codeVerifier'] ] : []) + ] + (!empty($options['codeVerifier']) ? ['code_verifier' => $options['codeVerifier']] : []) - ) : (!empty($options['jwt']) ? $this->requestToken(self::TOKEN_ENDPOINT, [ + ) : (!empty($options['jwt']) ? $this->requestToken(self::TOKEN_ENDPOINT, [ - 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', - 'assertion' => $options['jwt'], - 'access_token_ttl' => self::ACCESS_TOKEN_TTL, - 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL + 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', + 'assertion' => $options['jwt'], + 'access_token_ttl' => self::ACCESS_TOKEN_TTL, + 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL - ]) : $this->requestToken(self::TOKEN_ENDPOINT, [ + ]) : $this->requestToken(self::TOKEN_ENDPOINT, [ - 'grant_type' => 'password', - 'username' => $options['username'], - 'extension' => $options['extension'] ? $options['extension'] : null, - 'password' => $options["password"], - 'access_token_ttl' => self::ACCESS_TOKEN_TTL, - 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL + 'grant_type' => 'password', + 'username' => $options['username'], + 'extension' => $options['extension'] ? $options['extension'] : null, + 'password' => $options["password"], + 'access_token_ttl' => self::ACCESS_TOKEN_TTL, + 'refresh_token_ttl' => self::REFRESH_TOKEN_TTL - ])); + ])); $this->_auth->setData($response->jsonArray()); @@ -260,9 +272,9 @@ public function refresh() // Synchronous $response = $this->requestToken(self::TOKEN_ENDPOINT, [ - "grant_type" => "refresh_token", - "refresh_token" => $this->_auth->refreshToken(), - "access_token_ttl" => self::ACCESS_TOKEN_TTL, + "grant_type" => "refresh_token", + "refresh_token" => $this->_auth->refreshToken(), + "access_token_ttl" => self::ACCESS_TOKEN_TTL, "refresh_token_ttl" => self::REFRESH_TOKEN_TTL ]); @@ -314,9 +326,9 @@ public function inflateRequest(RequestInterface $request, $options = []) /** @var RequestInterface $request */ $request = $request->withAddedHeader('User-Agent', $this->_userAgent) - ->withAddedHeader('RC-User-Agent', $this->_userAgent); + ->withAddedHeader('RC-User-Agent', $this->_userAgent); - $uri = new Uri($this->createUrl((string)$request->getUri(), ['addServer' => true])); + $uri = new Uri($this->createUrl((string) $request->getUri(), ['addServer' => true])); return $request->withUri($uri); @@ -432,10 +444,10 @@ public function put( * @throws Exception If an error occurs parsing the response from the request. * @throws ApiException If an error occurs making the request. */ - public function delete($url = '', $queryParameters = [], array $headers = [], $options = []) + public function delete($url = '', $body = null, $queryParameters = [], array $headers = [], $options = []) { return $this->sendRequest( - $this->_client->createRequest('DELETE', $url, $queryParameters, null, $headers), + $this->_client->createRequest('DELETE', $url, $queryParameters, $body, $headers), $options ); } @@ -457,7 +469,7 @@ protected function requestToken($path = '', $body = []) } $headers = [ 'Authorization' => 'Basic ' . $this->apiKey(), - 'Content-Type' => 'application/x-www-form-urlencoded' + 'Content-Type' => 'application/x-www-form-urlencoded' ]; $request = $this->_client->createRequest('POST', $path, null, $body, $headers); diff --git a/src/Platform/PlatformTest.php b/src/Platform/PlatformTest.php index 14aa1ea9..dc2d7df6 100644 --- a/src/Platform/PlatformTest.php +++ b/src/Platform/PlatformTest.php @@ -39,7 +39,7 @@ public function testRefreshWithOutdatedToken() $sdk = $this->getSDK(); $sdk->platform()->auth()->setData([ - 'refresh_token_expires_in' => 1, + 'refresh_token_expires_in' => 1, 'refresh_token_expire_time' => 1 ]); @@ -56,7 +56,7 @@ public function testAutomaticRefresh() ]); $sdk->platform()->auth()->setData([ - 'expires_in' => 1, + 'expires_in' => 1, 'expire_time' => 1 ]); @@ -86,12 +86,14 @@ public function testLogout() public function testAuthUrl() { $sdk = $this->getSDK(); - $url = $sdk->platform()->authUrl(array( - 'redirectUri' => 'foo', - 'state' => 'bar', - 'client_id' => 'baz' - )); - $this->assertEquals( $url, "https://whatever/restapi/oauth/authorize?response_type=code&redirect_uri=foo&client_id=whatever&state=bar" ); + $url = $sdk->platform()->authUrl( + array( + 'redirectUri' => 'foo', + 'state' => 'bar', + 'client_id' => 'baz' + ) + ); + $this->assertEquals($url, "https://whatever/restapi/oauth/authorize?response_type=code&redirect_uri=foo&client_id=whatever&state=bar"); } public function testApiUrl() @@ -103,7 +105,7 @@ public function testApiUrl() $sdk->platform()->createUrl('/account/~/extension/~', [ 'addServer' => true, 'addMethod' => 'POST', - 'addToken' => true + 'addToken' => true ]) ); @@ -111,12 +113,12 @@ public function testApiUrl() 'https://whatever/restapi/v1.0/account/~/extension/~', $sdk->platform()->createUrl('/account/~/extension/~', [ 'addServer' => true - ]) + ]) ); $this->assertEquals( 'https://whatever/rcvideo/v2/account/~/extension/~/bridges', - $sdk->platform()->createUrl('/rcvideo/v2/account/~/extension/~/bridges', [ + $sdk->platform()->createUrl('/rcvideo/v2/account/~/extension/~/bridges', [ 'addServer' => true ]) ); @@ -147,7 +149,7 @@ public function testApiUrl() $sdk->platform()->createUrl('https://foo/account/~/extension/~', [ 'addServer' => true, 'addMethod' => 'POST', - 'addToken' => true + 'addToken' => true ]) ); @@ -162,7 +164,7 @@ public function testProcessRequest() $request = $sdk->platform()->inflateRequest(new Request('GET', '/foo')); - $this->assertEquals('https://whatever/restapi/v1.0/foo', (string)$request->getUri()); + $this->assertEquals('https://whatever/restapi/v1.0/foo', (string) $request->getUri()); $this->assertEquals($request->getHeaderLine('User-Agent'), $request->getHeaderLine('RC-User-Agent')); @@ -172,4 +174,54 @@ public function testProcessRequest() } + public function testDeleteWithBody() + { + $body = ["keepAssetsInInventory" => true, "records" => [["id" => "123"]]]; + $sdk = $this->getSDK([ + $this->createResponse('DELETE', '/restapi/v2/accounts/~/extensions', ["keepAssetsInInventory" => true, "records" => [["id" => "123"]]]) + ]); + $response = $sdk->platform()->delete("/restapi/v2/accounts/~/extensions", $body); + $this->assertEquals($response->body(), json_encode($body)); + } + public function testDeleteWithoutBody() + { + $sdk = $this->getSDK([ + $this->createResponse('DELETE', '/restapi/v2/accounts/~/extensions', ["Success"]) + ]); + $response = $sdk->platform()->delete("/restapi/v2/accounts/~/extensions"); + $this->assertEquals($response->body(), json_encode(["Success"])); + } + + public function testDeleteWithParamsBody() + { + $body = [ + 'param1' => 'value1', + 'param2' => 'value2' + ]; + $sdk = $this->getSDK([ + $this->createResponse('DELETE', '/restapi/v2/accounts/~/extensions', [ + 'param1' => 'value1', + 'param2' => 'value2' + ]) + ]); + $response = $sdk->platform()->delete("/restapi/v2/accounts/~/extensions", [ + 'param1' => 'value1', + 'param2' => 'value2' + ]); + $this->assertEquals($response->body(), json_encode($body)); + } + public function testDeleteWithBodyWithParams() + { + $body = ["keepAssetsInInventory" => true, "records" => [["id" => "123"]]]; + $sdk = $this->getSDK([ + $this->createResponse('DELETE', '/restapi/v2/accounts/~/extensions', ["Success"]) + ]); + $response = $sdk->platform()->delete("/restapi/v2/accounts/~/extensions", $body, [ + 'param1' => 'value1', + 'param2' => 'value2' + ]); + $this->assertEquals($response->body(), json_encode(["Success"])); + } + + } \ No newline at end of file