/**
  * Sends the re-crawl API request to Nosto.
  *
  * @param NostoAccountInterface $account the account to re-crawl the product(s) for.
  * @param array $payload the request payload as an array that will be json encoded.
  * @return bool true on success.
  * @throws NostoException if the request fails or cannot be made.
  */
 protected static function sendRequest(NostoAccountInterface $account, array $payload)
 {
     $token = $account->getApiToken('products');
     if ($token === null) {
         throw new NostoException('Failed to send product re-crawl to Nosto. No `products` API token found for account.');
     }
     $request = new NostoApiRequest();
     $request->setPath(NostoApiRequest::PATH_PRODUCT_RE_CRAWL);
     $request->setContentType('application/json');
     $request->setAuthBasic('', $token->getValue());
     $response = $request->post(json_encode($payload));
     if ($response->getCode() !== 200) {
         Nosto::throwHttpException('Failed to send product re-crawl to Nosto.', $request, $response);
     }
     return true;
 }
예제 #2
0
 /**
  * Encrypts and returns the data.
  *
  * @param NostoAccountInterface $account the account to export the data for.
  * @param NostoExportCollectionInterface $collection the data collection to export.
  * @return string the encrypted data.
  */
 public static function export(NostoAccountInterface $account, NostoExportCollectionInterface $collection)
 {
     $data = '';
     // Use the first 16 chars of the SSO token as secret for encryption.
     $token = $account->getApiToken('sso');
     if (!empty($token)) {
         $tokenValue = $token->getValue();
         $secret = substr($tokenValue, 0, 16);
         if (!empty($secret)) {
             $iv = NostoCryptRandom::getRandomString(16);
             $cipher = new NostoCipher();
             $cipher->setSecret($secret);
             $cipher->setIV($iv);
             $cipherText = $cipher->encrypt($collection->getJson());
             // Prepend the IV to the cipher string so that nosto can parse and use it.
             // There is no security concern with sending the IV as plain text.
             $data = $iv . $cipherText;
         }
     }
     return $data;
 }