public function required(HttpResponse $response)
 {
     return HttpConstants::STATUS_AUTHORIZATION_REQUIRED == $response->getStatusCode();
 }
Beispiel #2
0
 /**
  * @covers \http\HttpResponse::setStatusCode
  */
 public function testSetStatusCode()
 {
     $this->object->setStatusCode(301);
     $code = $this->object->getStatusCode();
     $this->assertEquals(301, $code, 'Status code should be 301');
 }
 /** Test Wookie server connection
  *  @return boolean true if success, otherwise false
  */
 public function Test()
 {
     $ctx = @stream_context_create(array('http' => array('timeout' => 15)));
     $url = $this->getURL();
     if (!empty($url)) {
         $response = new HttpResponse(@file_get_contents($url . 'advertise?all=true', false, $ctx), $http_response_header);
         if ($response->getStatusCode() == 200) {
             $xmlDoc = @simplexml_load_string($response->getResponseText());
             if (is_object($xmlDoc) && $xmlDoc->getName() == 'widgets') {
                 return true;
             }
         }
     }
     return false;
 }
Beispiel #4
0
 /**
  * Handle a non 200 series response from the API.
  *
  * This method is here specifically for sub-classes to override. When an API call is made and a non-200 series
  * status code is returned then this method is called with that response. This lets API client authors to extract
  * the appropriate error message out of the response and decide whether or not to throw a PHP exception.
  *
  * It is recommended that you obey the caller's wishes on whether or not to throw an exception by using the
  * following `if` statement:
  *
  * ```php
  * if ($this->val('throw', $options, $this->throwExceptions)) {
  * ...
  * }
  * ```
  *
  * @param HttpResponse $response The response sent from the API.
  * @param array $options The options that were sent with the request.
  * @throws \Exception Throws an exception if the settings or options say to throw an exception.
  */
 public function handleErrorResponse(HttpResponse $response, $options = [])
 {
     if ($this->val('throw', $options, $this->throwExceptions)) {
         $body = $response->getBody();
         if (is_array($body)) {
             $message = $this->val('message', $body, $response->getReasonPhrase());
         } else {
             $message = $response->getReasonPhrase();
         }
         throw new \Exception($message, $response->getStatusCode());
     }
 }
 /**
  * Get property for Widget instance
  * @param WidgetInstance instance of WidgetInstance
  * @param Propety instance of WidgetProperty
  * @return WidgetProperty if request fails, return false;
  * @throws WookieConnectorException, WookieWidgetInstanceException
  */
 public function getProperty($widgetInstance, $propertyInstance)
 {
     $Url = $this->getConnection()->getURL() . 'properties';
     try {
         if (!$widgetInstance instanceof WidgetInstance) {
             throw new Exception\WookieWidgetInstanceException('No Widget instance');
         }
         if (!$propertyInstance instanceof WidgetProperty) {
             throw new Exception\WookieConnectorException('No properties instance');
         }
         $data = array('api_key' => $this->getConnection()->getApiKey(), 'shareddatakey' => $this->getConnection()->getSharedDataKey(), 'userid' => $this->getUser()->getLoginName(), 'widgetid' => $widgetInstance->getIdentifier(), 'propertyname' => $propertyInstance->getName());
         $request = @http_build_query($data);
         if (!$this->checkURL($Url)) {
             throw new Exception\WookieConnectorException("Properties rest URL is incorrect: " . $Url);
         }
         $response = new HttpResponse(@file_get_contents($Url . '?' . $request, false, $this->getHttpStreamContext()), $http_response_header);
         $statusCode = $response->getStatusCode();
         if ($statusCode != 200) {
             throw new Exception\WookieConnectorException($response->headerToString() . '<br />' . $response->getResponseText());
         }
         return new WidgetProperty($propertyInstance->getName(), $response->getResponseText());
     } catch (WookieConnectorException $e) {
         $this->getLogger()->write($e->toString());
     } catch (WookieWidgetInstanceException $e) {
         $this->getLogger()->write($e->toString());
     }
     return false;
 }