diff --git a/src/Services/InfoProviderSystem/Providers/CanopyProvider.php b/src/Services/InfoProviderSystem/Providers/CanopyProvider.php index f7683084..131db15f 100644 --- a/src/Services/InfoProviderSystem/Providers/CanopyProvider.php +++ b/src/Services/InfoProviderSystem/Providers/CanopyProvider.php @@ -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}"; } /** diff --git a/src/Settings/InfoProviderSystem/CanopySettings.php b/src/Settings/InfoProviderSystem/CanopySettings.php index f6a0494b..88e1fcb7 100644 --- a/src/Settings/InfoProviderSystem/CanopySettings.php +++ b/src/Settings/InfoProviderSystem/CanopySettings.php @@ -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; + } } diff --git a/src/Settings/InfoProviderSystem/InfoProviderSettings.php b/src/Settings/InfoProviderSystem/InfoProviderSettings.php index 3e78233f..248fcedc 100644 --- a/src/Settings/InfoProviderSystem/InfoProviderSettings.php +++ b/src/Settings/InfoProviderSystem/InfoProviderSettings.php @@ -72,4 +72,7 @@ class InfoProviderSettings #[EmbeddedSettings] public ?ConradSettings $conrad = null; + + #[EmbeddedSettings] + public ?CanopySettings $canopy = null; } diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index c8c41b92..f422beb3 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -12569,5 +12569,23 @@ Buerklin-API Authentication server: Amazon barcode + + + settings.ips.canopy + Canopy + + + + + settings.ips.canopy.alwaysGetDetails + Always fetch details + + + + + settings.ips.canopy.alwaysGetDetails.help + When selected, more details will be fetched from canopy when creating a part. This causes an additional API request, but gives product bullet points and category info. + +