Skip to content

Commit

Permalink
Add body param for delete request
Browse files Browse the repository at this point in the history
  • Loading branch information
SushilMallRC committed Jul 25, 2024
1 parent 556292f commit e9f601f
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 70 deletions.
126 changes: 69 additions & 57 deletions src/Platform/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;

}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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'
]);
}
Expand Down Expand Up @@ -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());

Expand All @@ -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
]);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
);
}
Expand All @@ -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);
Expand Down
78 changes: 65 additions & 13 deletions src/Platform/PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);

Expand All @@ -56,7 +56,7 @@ public function testAutomaticRefresh()
]);

$sdk->platform()->auth()->setData([
'expires_in' => 1,
'expires_in' => 1,
'expire_time' => 1
]);

Expand Down Expand Up @@ -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()
Expand All @@ -103,20 +105,20 @@ public function testApiUrl()
$sdk->platform()->createUrl('/account/~/extension/~', [
'addServer' => true,
'addMethod' => 'POST',
'addToken' => true
'addToken' => true
])
);

$this->assertEquals(
'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
])
);
Expand Down Expand Up @@ -147,7 +149,7 @@ public function testApiUrl()
$sdk->platform()->createUrl('https://foo/account/~/extension/~', [
'addServer' => true,
'addMethod' => 'POST',
'addToken' => true
'addToken' => true
])
);

Expand All @@ -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'));

Expand All @@ -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"]));
}


}

0 comments on commit e9f601f

Please sign in to comment.