/**
  * @param Command $command
  * @param string $userSessionToken
  *
  * @return Model object
  * @throws WixHiveException
  */
 public function execute(Command $command, $userSessionToken = null)
 {
     $date = new \DateTime("now", new \DateTimeZone("UTC"));
     $getParams = ["version" => $this->getAPIVersionForCommand($command)];
     if ($userSessionToken !== null) {
         $getParams['userSessionToken'] = $userSessionToken;
     }
     // prepare the request based on the command
     $headers = ["x-wix-application-id" => $this->applicationId, "x-wix-instance-id" => $this->instanceId, "x-wix-timestamp" => $date->format(Signature::TIME_FORMAT), "x-wix-signature" => Signature::sign($this->applicationId, $this->secretKey, $this->instanceId, $userSessionToken, Command::WIXHIVE_VERSION, $this->getAPIVersionForCommand($command), $command->getCommand(), $command->getBody(), $command->getHttpMethod(), $date), "Content-Type" => "application/json", "Expect" => ""];
     try {
         // trigger the request to the WixHive API
         $request = new \GuzzleHttp\Psr7\Request($command->getHttpMethod(), $command->getEndpoint($getParams), $headers, $command->getBody());
         $response = (new \GuzzleHttp\Client(['base_uri' => $command->getBaseUri()]))->send($request);
         // checking response content type
         $contentType = $response->getHeader("Content-Type");
         $contentType = isset($contentType[0]) ? explode(";", $contentType[0]) : false;
         if (!isset($contentType[0]) || $contentType[0] !== "application/json") {
             throw new WixHiveException("Response content type is not supported", "415");
         }
     } catch (ClientException $e) {
         throw new WixHiveException($e->getResponse()->getReasonPhrase(), $e->getResponse()->getStatusCode());
     }
     // process received response from WixHive API
     return ResponseProcessor::process($command, $response);
 }
 public function testProcessShouldThrowException()
 {
     $id = rand(1, 100);
     $responseData = new stdClass();
     $responseData->errorCode = 403;
     $responseData->message = "Permission denied";
     $this->setExpectedException('Exception', "Permission denied", 403);
     ResponseProcessor::process(new GetActivityById($id), new Response($responseData));
 }
 public function testProcessShouldThrowException()
 {
     $id = rand(1, 100);
     $responseData = new stdClass();
     $responseData->errorCode = 403;
     $responseData->message = "Permission denied";
     $response = \Phake::mock("GuzzleHttp\\Psr7\\Response");
     \Phake::when($response)->getBody()->thenReturn(json_encode($responseData));
     $this->setExpectedException('Exception', "Permission denied", 403);
     ResponseProcessor::process(new GetActivityById($id), $response);
 }
Example #4
0
 /**
  * @param Command $command
  * @param string $userSessionToken
  *
  * @return Model object
  * @throws WixHiveException
  */
 public function execute(Command $command, $userSessionToken = null)
 {
     $date = new \DateTime("now", new \DateTimeZone("UTC"));
     $getParams = ["version" => self::API_VERSION];
     if ($userSessionToken !== null) {
         $getParams['userSessionToken'] = $userSessionToken;
     }
     // prepare the request based on the command
     $headers = ["x-wix-application-id" => $this->applicationId, "x-wix-instance-id" => $this->instanceId, "x-wix-timestamp" => $date->format(Signature::TIME_FORMAT), "x-wix-signature" => Signature::sign($this->applicationId, $this->secretKey, $this->instanceId, $userSessionToken, Command::WIXHIVE_VERSION, self::API_VERSION, $command->getCommand(), $command->getBody(), $command->getHttpMethod(), $date), "Content-Type" => "application/json", "Expect" => ""];
     $wixHiveRequest = new Request($command->getEndpointUrl($getParams), $command->getHttpMethod(), $headers, $command->getBody());
     // trigger the request to the WixHive API
     $response = (new Connector())->execute($wixHiveRequest);
     // process received response from WixHive API
     return ResponseProcessor::process($command, $response);
 }