Exemplo n.º 1
0
 /**
  * @param                    $method
  * @param                    $uri
  * @param array              $options
  * @param null|bool|\Closure $successCallback
  * @param null|bool|\Closure $failCallback
  *
  * @return bool|null|\StdClass
  * @throws Exception
  */
 public function request($method, $uri, $options = [], $successCallback = null, $failCallback = null)
 {
     if (!$successCallback && !$failCallback) {
         $json = null;
         // If there's no callbacks defined we assume this is a RESTful request
         Logger::getInstance()->debug($method . ' ' . $uri . ' ' . json_encode($options));
         $options = array_merge($this->defaultOptions, $options);
         $guzzle = new GuzzleHttp\Client(['base_uri' => self::BASE_URL_REST]);
         $res = $guzzle->request($method, $uri, $options);
         if ($res->getHeader('content-type')[0] == 'application/json') {
             $contents = $res->getBody()->getContents();
             Logger::getInstance()->debug($contents);
             $json = json_decode($contents);
         } else {
             throw new Exception("Server did not send JSON. Response was \"{$res->getBody()->getContents()}\"");
         }
         return $json;
     } else {
         // Use the STREAM API end point
         $Uri = new Uri(self::BASE_URL_STREAM);
         $Uri->setUserInfo($this->defaultOptions['auth'][0] . ':' . $this->defaultOptions['auth'][1]);
         $Uri->setPath($uri);
         if (isset($options['query'])) {
             $Uri->setQuery($options['query']);
         }
         $WSClient = new WebSocket\Client($Uri);
         Logger::getInstance()->debug($Uri);
         if (!$successCallback instanceof \Closure) {
             $successCallback = function ($response) {
                 $info = json_decode($response);
                 if (property_exists($info, 'output')) {
                     Logger::getInstance()->info(json_decode($response)->output);
                 } elseif ($info->type == 'error') {
                     // output error info
                     Logger::getInstance()->info($info->message);
                 }
             };
         }
         if (!$failCallback instanceof \Closure) {
             $failCallback = function (\Exception $exception) {
                 Logger::getInstance()->info($exception->getMessage());
             };
         }
         try {
             while ($response = $WSClient->receive()) {
                 $successCallback($response);
             }
         } catch (\Exception $e) {
             $failCallback($e);
         }
         return true;
     }
 }
Exemplo n.º 2
0
 public function testSetLogger()
 {
     Logger::getInstance()->setLogger(new \Monolog\Logger('UnitTest'));
     Logger::getInstance()->debug('Test from testSetLogger()');
     $this->assertInstanceOf(\Psr\Log\LoggerInterface::class, Logger::getInstance()->getLogger());
 }