예제 #1
0
 /**
  * @inheritdoc
  */
 public static function create(NostoAccountMetaDataInterface $meta)
 {
     $params = array('title' => $meta->getTitle(), 'name' => $meta->getName(), 'platform' => $meta->getPlatform(), 'front_page_url' => $meta->getFrontPageUrl(), 'currency_code' => strtoupper($meta->getCurrencyCode()), 'language_code' => strtolower($meta->getOwnerLanguageCode()), 'owner' => array('first_name' => $meta->getOwner()->getFirstName(), 'last_name' => $meta->getOwner()->getLastName(), 'email' => $meta->getOwner()->getEmail()), 'api_tokens' => array());
     // Add optional billing details if the required data is set.
     $billingDetails = array('country' => strtoupper($meta->getBillingDetails()->getCountry()));
     if (!empty($billingDetails['country'])) {
         $params['billing_details'] = $billingDetails;
     }
     // Add optional partner code if one is set.
     $partnerCode = $meta->getPartnerCode();
     if (!empty($partnerCode)) {
         $params['partner_code'] = $partnerCode;
     }
     // Request all available API tokens for the account.
     foreach (NostoApiToken::$tokenNames as $name) {
         $params['api_tokens'][] = 'api_' . $name;
     }
     $request = new NostoApiRequest();
     $request->setPath(NostoApiRequest::PATH_SIGN_UP);
     $request->setReplaceParams(array('{lang}' => $meta->getLanguageCode()));
     $request->setContentType('application/json');
     $request->setAuthBasic('', $meta->getSignUpApiToken());
     $response = $request->post(json_encode($params));
     if ($response->getCode() !== 200) {
         Nosto::throwHttpException('Nosto account could not be created.', $request, $response);
     }
     $account = new self($meta->getPlatform() . '-' . $meta->getName());
     $account->tokens = NostoApiToken::parseTokens($response->getJsonResult(true), '', '_token');
     return $account;
 }
예제 #2
0
 /**
  * Builds the API request and returns it.
  *
  * @return NostoApiRequest the request object.
  * @throws NostoException if the request object cannot be built.
  */
 protected function initApiRequest()
 {
     $token = $this->account->getApiToken(NostoApiToken::API_EXCHANGE_RATES);
     if (is_null($token)) {
         Nosto::throwException(sprintf('No `%s` API token found for account "%s".', NostoApiToken::API_EXCHANGE_RATES, $this->account->getName()));
     }
     $request = new NostoApiRequest();
     $request->setContentType('application/json');
     $request->setAuthBasic('', $token->getValue());
     $request->setPath(NostoApiRequest::PATH_CURRENCY_EXCHANGE_RATE);
     return $request;
 }
예제 #3
0
 /**
  * Create and returns a new API request object initialized with:
  * - content type
  * - auth token
  *
  * @return NostoApiRequest the newly created request object.
  * @throws NostoException if the account does not have the `products` token set.
  */
 protected function initApiRequest()
 {
     $token = $this->account->getApiToken('products');
     if (is_null($token)) {
         throw new NostoException('No `products` API token found for account.');
     }
     $request = new NostoApiRequest();
     $request->setContentType('application/json');
     $request->setAuthBasic('', $token->getValue());
     return $request;
 }
예제 #4
0
 /**
  * Builds the API request and returns it.
  *
  * @param string $path the request API path.
  * @param string $auth the basic auth token.
  * @return NostoApiRequest the request object.
  */
 protected function initApiRequest($path, $auth)
 {
     $request = new NostoApiRequest();
     $request->setContentType('application/json');
     $request->setAuthBasic('', $auth);
     $request->setPath($path);
     return $request;
 }
예제 #5
0
 /**
  * Create and returns a new API request object initialized with:
  * - path
  * - content type
  * - auth token
  *
  * @return NostoApiRequest the newly created request object.
  * @throws NostoException if the account does not have the `products` token set.
  */
 protected function initApiRequest()
 {
     $token = $this->account->getApiToken(NostoApiToken::API_PRODUCTS);
     if (is_null($token)) {
         throw new NostoException(sprintf('No `%s` API token found for account "%s".', NostoApiToken::API_PRODUCTS, $this->account->getName()));
     }
     $request = new NostoApiRequest();
     $request->setPath(NostoApiRequest::PATH_PRODUCT_RE_CRAWL);
     $request->setContentType('application/json');
     $request->setAuthBasic('', $token->getValue());
     return $request;
 }
 /**
  * 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;
 }