99use GuzzleHttp \Psr7 \Request ;
1010use Psr \Http \Message \RequestInterface ;
1111use Firebase \JWT \JWT ;
12+ use GuzzleHttp \Exception \RequestException ;
1213use OpenTok \Exception \Exception ;
1314use OpenTok \Exception \DomainException ;
1415use OpenTok \Exception \UnexpectedValueException ;
2728use OpenTok \Exception \ForceDisconnectConnectionException ;
2829use OpenTok \Exception \ForceDisconnectUnexpectedValueException ;
2930use OpenTok \Exception \ForceDisconnectAuthenticationException ;
31+ use OpenTok \Exception \SignalNetworkConnectionException ;
3032use OpenTok \MediaMode ;
3133
3234use function GuzzleHttp \default_user_agent ;
@@ -45,38 +47,47 @@ class Client
4547 protected $ apiKey ;
4648 protected $ apiSecret ;
4749 protected $ configured = false ;
50+
51+ /**
52+ * @var \GuzzleHttp\Client
53+ */
4854 protected $ client ;
4955
5056 public function configure ($ apiKey , $ apiSecret , $ apiUrl , $ options = array ())
5157 {
5258 $ this ->apiKey = $ apiKey ;
5359 $ this ->apiSecret = $ apiSecret ;
5460
55- $ clientOptions = [
56- 'base_uri ' => $ apiUrl ,
57- 'headers ' => [
58- 'User-Agent ' => OPENTOK_SDK_USER_AGENT . ' ' . default_user_agent (),
59- ],
60- ];
61+ if (isset ($ options ['client ' ])) {
62+ $ this ->client = $ options ['client ' ];
63+ } else {
64+ $ clientOptions = [
65+ 'base_uri ' => $ apiUrl ,
66+ 'headers ' => [
67+ 'User-Agent ' => OPENTOK_SDK_USER_AGENT . ' ' . default_user_agent (),
68+ ],
69+ ];
70+
71+ if (!empty ($ options ['timeout ' ])) {
72+ $ clientOptions ['timeout ' ] = $ options ['timeout ' ];
73+ }
6174
62- if (!empty ($ options ['timeout ' ])) {
63- $ clientOptions ['timeout ' ] = $ options ['timeout ' ];
64- }
75+ if (empty ($ options ['handler ' ])) {
76+ $ handlerStack = HandlerStack::create ();
77+ } else {
78+ $ handlerStack = $ options ['handler ' ];
79+ }
80+ $ clientOptions ['handler ' ] = $ handlerStack ;
6581
66- if (empty ($ options ['handler ' ])) {
67- $ handlerStack = HandlerStack::create ();
68- } else {
69- $ handlerStack = $ options ['handler ' ];
70- }
71- $ clientOptions ['handler ' ] = $ handlerStack ;
82+ $ handler = Middleware::mapRequest (function (RequestInterface $ request ) {
83+ $ authHeader = $ this ->createAuthHeader ();
84+ return $ request ->withHeader ('X-OPENTOK-AUTH ' , $ authHeader );
85+ });
86+ $ handlerStack ->push ($ handler );
7287
73- $ handler = Middleware::mapRequest (function (RequestInterface $ request ) {
74- $ authHeader = $ this ->createAuthHeader ();
75- return $ request ->withHeader ('X-OPENTOK-AUTH ' , $ authHeader );
76- });
77- $ handlerStack ->push ($ handler );
88+ $ this ->client = new \GuzzleHttp \Client ($ clientOptions );
89+ }
7890
79- $ this ->client = new \GuzzleHttp \Client ($ clientOptions );
8091 $ this ->configured = true ;
8192 }
8293
@@ -504,27 +515,42 @@ public function dial($sessionId, $token, $sipUri, $options)
504515 return $ sipJson ;
505516 }
506517
507- public function signal ($ sessionId , $ options = array (), $ connectionId = null )
518+ /**
519+ * Signal either an entire session or a specific connection in a session
520+ *
521+ * @param string $sessionId ID of the session to send the signal to
522+ * @param array{type: string, data: mixed} $payload Signal payload to send
523+ * @param string $connectionId ID of the connection to send the signal to
524+ *
525+ * @todo Mark $payload as required, as you cannot send an empty signal request body
526+ *
527+ * @throws SignalNetworkConnectionException
528+ * @throws \Exception
529+ */
530+ public function signal ($ sessionId , $ payload = [], $ connectionId = null )
508531 {
509532 // set up the request
510-
511-
533+ $ requestRoot = '/v2/project/ ' . $ this ->apiKey . '/session/ ' . $ sessionId ;
512534 $ request = is_null ($ connectionId ) || empty ($ connectionId ) ?
513- new Request ('POST ' , ' /v2/project/ ' . $ this -> apiKey . ' /session/ ' . $ sessionId . '/signal ' )
514- : new Request ('POST ' , ' /v2/project/ ' . $ this -> apiKey . ' /session/ ' . $ sessionId . '/connection/ ' . $ connectionId . '/signal ' );
535+ new Request ('POST ' , $ requestRoot . '/signal ' )
536+ : new Request ('POST ' , $ requestRoot . '/connection/ ' . $ connectionId . '/signal ' );
515537
516538 try {
517539 $ response = $ this ->client ->send ($ request , [
518540 'debug ' => $ this ->isDebug (),
519541 'json ' => array_merge (
520- $ options
542+ $ payload
521543 )
522544 ]);
523545 if ($ response ->getStatusCode () != 204 ) {
524546 json_decode ($ response ->getBody (), true );
525547 }
526- } catch (\ Exception $ e ) {
548+ } catch (ClientException $ e ) {
527549 $ this ->handleSignalingException ($ e );
550+ } catch (RequestException $ e ) {
551+ throw new SignalNetworkConnectionException ('Unable to communicate with host ' , -1 , $ e );
552+ } catch (\Exception $ e ) {
553+ throw $ e ;
528554 }
529555 }
530556
@@ -611,7 +637,7 @@ private function handleBroadcastException($e)
611637 }
612638 }
613639
614- private function handleSignalingException ($ e )
640+ private function handleSignalingException (ClientException $ e )
615641 {
616642 $ responseCode = $ e ->getResponse ()->getStatusCode ();
617643 switch ($ responseCode ) {
@@ -624,7 +650,8 @@ private function handleSignalingException($e)
624650 $ message = 'The client specified by the connectionId property is not connected to the session. ' ;
625651 throw new SignalConnectionException ($ message , $ responseCode );
626652 case 413 :
627- $ message = 'The type string exceeds the maximum length (128 bytes), or the data string exceeds the maximum size (8 kB). ' ;
653+ $ message = 'The type string exceeds the maximum length (128 bytes), '
654+ . ' or the data string exceeds the maximum size (8 kB). ' ;
628655 throw new SignalUnexpectedValueException ($ message , $ responseCode );
629656 default :
630657 break ;
0 commit comments