Skip to content

Commit

Permalink
Enable request/response logging when OPENTOK_DEBUG is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
aiham committed Mar 5, 2018
1 parent 085539a commit 1536d66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
50 changes: 37 additions & 13 deletions src/OpenTok/Util/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function createSession($options)
$request = new Request('POST', '/session/create');
try {
$response = $this->client->send($request, [
'debug' => true,
'debug' => $this->isDebug(),
'form_params' => $this->postFieldsForOptions($options)
]);
$sessionXml = $this->getResponseXml($response);
Expand Down Expand Up @@ -143,6 +143,7 @@ public function startArchive($sessionId, $options)

try {
$response = $this->client->send($request, [
'debug' => $this->isDebug(),
'json' => array_merge(
array( 'sessionId' => $sessionId ),
$options
Expand All @@ -165,7 +166,9 @@ public function stopArchive($archiveId)
);

try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
$archiveJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
// TODO: what happens with JSON parse errors?
Expand All @@ -181,7 +184,9 @@ public function getArchive($archiveId)
'/v2/project/'.$this->apiKey.'/archive/'.$archiveId
);
try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
$archiveJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->handleException($e);
Expand All @@ -198,7 +203,9 @@ public function deleteArchive($archiveId)
['Content-Type' => 'application/json']
);
try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
if ($response->getStatusCode() != 204) {
json_decode($response->getBody(), true);
}
Expand All @@ -217,7 +224,9 @@ public function forceDisconnect($sessionId,$connectionId)
['Content-Type' => 'application/json']
);
try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
if ($response->getStatusCode() != 204) {
json_decode($response->getBody(), true);
}
Expand All @@ -240,7 +249,8 @@ public function listArchives($offset, $count)
}
try {
$response = $this->client->send($request, [
'query' => $queryParams
'debug' => $this->isDebug(),
'query' => $queryParams
]);
$archiveListJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
Expand All @@ -259,10 +269,11 @@ public function startBroadcast($sessionId, $options)

try {
$response = $this->client->send($request, [
'json' => [
'sessionId' => $sessionId,
'layout' => $options['layout']->jsonSerialize()
]
'debug' => $this->isDebug(),
'json' => [
'sessionId' => $sessionId,
'layout' => $options['layout']->jsonSerialize()
]
]);
$broadcastJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
Expand All @@ -280,7 +291,9 @@ public function stopBroadcast($broadcastId)
);

try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
$broadcastJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->handleBroadcastException($e);
Expand All @@ -295,7 +308,9 @@ public function getBroadcast($broadcastId)
'/v2/project/'.$this->apiKey.'/broadcast/'.$broadcastId
);
try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
$broadcastJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->handleBroadcastException($e);
Expand All @@ -310,7 +325,9 @@ public function getLayout($resourceId, $resourceType = 'broadcast')
'/v2/project/'.$this->apiKey.'/'.$resourceType.'/'.$resourceId.'/layout'
);
try {
$response = $this->client->send($request);
$response = $this->client->send($request, [
'debug' => $this->isDebug()
]);
$layoutJson = json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->handleException($e);
Expand All @@ -326,6 +343,7 @@ public function updateLayout($resourceId, $layout, $resourceType = 'broadcast')
);
try {
$response = $this->client->send($request, [
'debug' => $this->isDebug(),
'json' => $layout->jsonSerialize()
]);
$layoutJson = json_decode($response->getBody(), true);
Expand All @@ -343,6 +361,7 @@ public function updateStream($sessionId, $streamId, $properties)
);
try {
$response = $this->client->send($request, [
'debug' => $this->isDebug(),
'json' => $properties
]);
if ($response->getStatusCode() != 204) {
Expand Down Expand Up @@ -380,6 +399,7 @@ public function dial($sessionId, $token, $sipUri, $options)

try {
$response = $this->client->send($request, [
'debug' => $this->isDebug(),
'json' => $body
]);
$sipJson = json_decode($response->getBody(), true);
Expand Down Expand Up @@ -472,4 +492,8 @@ private function handleBroadcastException($e)
}
}

private function isDebug()
{
return defined('OPENTOK_DEBUG');
}
}
5 changes: 1 addition & 4 deletions tests/OpenTok/OpenTokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use OpenTok\TestHelpers;

// define('DEBUG', true);
define('OPENTOK_DEBUG', true);

class OpenTokTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -57,9 +57,6 @@ private function setupOTWithMocks($mocks)
'https://api.opentok.com',
$clientOptions
);
if (defined('DEBUG')) {
$this->client->addSubscriber(LogPlugin::getDebugPlugin());
}

// Push history onto handler stack *after* configuring client to
// ensure auth header is added before history handler is invoked
Expand Down

0 comments on commit 1536d66

Please sign in to comment.