Change Endpoints Used In API Calls
Loading Endpoints from Account Data
Services which interact with Klevu's APIs use a common class to provide endpoint URLs for requests. In the majority of cases, the domains provided will the ones needed to send or receive data. The primary exception is when sending search requests (including for Smart Category Merchandising) where the search URL varies by account. Note: at time of writing, no SDK servies interact with search URLs
The BaseUrlsProvider class allows updating base URLs from an Account object (see Examples > How To > Account Management > Retrieve Klevu Account Details) either via the constructor or after instantiation.
<?php
declare(strict_types=1);
use Klevu\PhpSDK\Model\Account;
use Klevu\PhpSDK\Model\AccountFactory;
use Klevu\PhpSDK\Provider\BaseUrlsProvider;
$accountFactory = new AccountFactory();
$account = $accountFactory->create(
data: [
Account::FIELD_SEARCH_URL => 'eucs27v2.ksearchnet.com',
],
);
$baseUrlsProvider = new BaseUrlsProvider(
account: $account,
);
$searchUrl = $baseUrlsProvider->getSearchUrl(); // eucs27v2.ksearchnet.com
$account->setSearchUrl('eucs30v2.ksearchnet.com');
$baseUrlsProvider->updateFromAccount(
account: $account,
);
$searchUrl = $baseUrlsProvider->getSearchUrl(); // eucs30v2.ksearchnet.com
An example use case would be to load and cache Klevu account information locally, which can be used to create Account objects (via the AccountFactory) for subsequent requests, rather than sending API requests on each page load.
Injecting Endpoints Into Services
The BaseUrlsProvider instance can then be used by any service that interacts with Klevu, all of which accept a BaseLoggerInterface instance via their constructor.
Note: if you are integrating into a modern framework, you should implement this via your dependency injection system.
<?php
declare(strict_types=1);
use Klevu\PhpSDK\Model\Account;
use Klevu\PhpSDK\Model\AccountFactory;
use Klevu\PhpSDK\Provider\BaseUrlsProvider;
use Klevu\PhpSDK\Service\Account\AccountFeaturesService;
use Klevu\PhpSDK\Service\Account\AccountLookupService;
use Klevu\PhpSDK\Service\Account\UpdateStoreFeedUrlService;
use Klevu\PhpSDK\Service\Analytics\CollectService as AnalyticsCollectService;
use Klevu\PhpSDK\Service\Indexing\AttributesService;
use Klevu\PhpSDK\Service\Indexing\Batch\DeleteService as BatchDeleteService;
use Klevu\PhpSDK\Service\Indexing\BatchService;
$accountFactory = new AccountFactory();
$account = $accountFactory->create(
data: [
Account::FIELD_SEARCH_URL => 'eucs27v2.ksearchnet.com',
],
);
$baseUrlsProvider = new BaseUrlsProvider(
account: $account,
);
// Account
$accountFeatureService = new AccountFeaturesService(
baseUrlsProvider: $baseUrlsProvider,
);
$accountLookupService = new AccountLookupService(
baseUrlsProvider: $baseUrlsProvider,
);
$updateStoreFeedUrlService = new UpdateStoreFeedUrlService(
baseUrlsProvider: $baseUrlsProvider,
);
// Analytics
$collectService = new AnalyticsCollectService(
baseUrlsProvider: $baseUrlsProvider,
);
// Indexing
$attributesService = new AttributesService(
baseUrlsProvider: $baseUrlsProvider,
);
$batchService = new BatchService(
baseUrlsProvider: $baseUrlsProvider,
);
$batchDeleteService = new BatchDeleteService(
baseUrlsProvider: $baseUrlsProvider,
);