mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-25 10:59:35 +00:00
Make Canopy provider configurable via UI
This commit is contained in:
parent
0b9b2cbf58
commit
300382f6e3
4 changed files with 70 additions and 4 deletions
|
|
@ -75,7 +75,7 @@ class CanopyProvider implements InfoProviderInterface
|
|||
|
||||
private function productPageFromASIN(string $asin): string
|
||||
{
|
||||
return "https://www.amazon.{$this->settings->domain}/dp/{$asin}";
|
||||
return "https://www.{$this->settings->getRealDomain()}/dp/{$asin}";
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -29,23 +29,68 @@ use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
|
|||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CountryType;
|
||||
use Symfony\Component\Translation\TranslatableMessage as TM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[Settings(label: new TM("settings.ips.canopy"), description: new TM("settings.ips.canopy.help"))]
|
||||
#[Settings(label: new TM("settings.ips.canopy"))]
|
||||
#[SettingsIcon("fa-plug")]
|
||||
class CanopySettings
|
||||
{
|
||||
public const ALLOWED_DOMAINS = [
|
||||
"amazon.de" => "DE",
|
||||
"amazon.com" => "US",
|
||||
"amazon.co.uk" => "UK",
|
||||
"amazon.fr" => "FR",
|
||||
"amazon.it" => "IT",
|
||||
"amazon.es" => "ES",
|
||||
"amazon.ca" => "CA",
|
||||
"amazon.com.au" => "AU",
|
||||
"amazon.com.br" => "BR",
|
||||
"amazon.com.mx" => "MX",
|
||||
"amazon.in" => "IN",
|
||||
"amazon.co.jp" => "JP",
|
||||
"amazon.nl" => "NL",
|
||||
"amazon.pl" => "PL",
|
||||
"amazon.sa" => "SA",
|
||||
"amazon.sg" => "SG",
|
||||
"amazon.se" => "SE",
|
||||
"amazon.com.tr" => "TR",
|
||||
"amazon.ae" => "AE",
|
||||
"amazon.com.be" => "BE",
|
||||
"amazon.com.cn" => "CN",
|
||||
];
|
||||
|
||||
use SettingsTrait;
|
||||
|
||||
#[SettingsParameter(label: new TM("settings.ips.canopy.apiKey"),
|
||||
#[SettingsParameter(label: new TM("settings.ips.mouser.apiKey"),
|
||||
formType: APIKeyType::class,
|
||||
formOptions: ["help_html" => true], envVar: "PROVIDER_CANOPY_API_KEY", envVarMode: EnvVarMode::OVERWRITE)]
|
||||
public ?string $apiKey = null;
|
||||
|
||||
public string $domain = "de";
|
||||
/**
|
||||
* @var string The domain used internally for the API requests. This is not necessarily the same as the domain shown to the user, which is determined by the keys of the ALLOWED_DOMAINS constant
|
||||
*/
|
||||
#[SettingsParameter(label: new TM("settings.ips.tme.country"), formType: ChoiceType::class, formOptions: ["choices" => self::ALLOWED_DOMAINS])]
|
||||
public string $domain = "DE";
|
||||
|
||||
/**
|
||||
* @var bool If true, the provider will always retrieve details for a part, resulting in an additional API request
|
||||
*/
|
||||
#[SettingsParameter(label: new TM("settings.ips.canopy.alwaysGetDetails"), description: new TM("settings.ips.canopy.alwaysGetDetails.help"))]
|
||||
public bool $alwaysGetDetails = false;
|
||||
|
||||
/**
|
||||
* Returns the real domain (e.g. amazon.de) based on the selected domain (e.g. DE)
|
||||
* @return string
|
||||
*/
|
||||
public function getRealDomain(): string
|
||||
{
|
||||
$domain = array_search($this->domain, self::ALLOWED_DOMAINS);
|
||||
if ($domain === false) {
|
||||
throw new \InvalidArgumentException("Invalid domain selected");
|
||||
}
|
||||
return $domain;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,4 +72,7 @@ class InfoProviderSettings
|
|||
|
||||
#[EmbeddedSettings]
|
||||
public ?ConradSettings $conrad = null;
|
||||
|
||||
#[EmbeddedSettings]
|
||||
public ?CanopySettings $canopy = null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue