/**
  * @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 testSignShouldReturnCorrectToken()
 {
     $expectedToken = "FjWuQVWOM9WikSetMMI8W3YFJ8yG8lJXSCbQbrA6O_o";
     $date = new \DateTime("2015-01-01 12:12:12");
     $token = Signature::sign("aaaa", "bbbb", "cccc", "dddd", "1.0", "v1", "/command", "{test:1}", "POST", $date);
     $this->assertEquals($expectedToken, $token);
 }
Beispiel #3
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);
 }