Frage

Mein Staging und Produktions-Server ist auf verschiedenen Servern und Domänen.

Welches ist die beste Art und Weise mit externen APIs umgehen würde, die einen Schlüssel haben, die auf Domain-Namen beruht? Ist das schlechte Praxis und beide auf demselben Server enthalten sein sollte?

War es hilfreich?

Lösung

Nun, meine eigene Lösung für dieses Problem wird mit verschiedenen Schlüsseln in einem Array für verschiedene Umgebungen.

In diesem Fall werde ich versuchen, es in PHP zu erklären

class API_Client
{
    const ENV_STAGING = 'staging';
    const ENV_PRODUCTION = 'production';

    protected static $apiKeys = array(
        self::ENV_STAGING    => 'thisisthekeyformystagingenv',
        self::ENV_PRODUCTION => 'thisisthekeyformyproductionenv',
    );

    protected static $environment = self::ENV_PRODUCTION;

    public static function getEnvironment()
    {
         return self::$environment;
    }

    public static function setEnvironment($environment)
    {
         self::$environment = $environment;
    }

    public static function apiCall($call)
    {
         $environment = self::getEnvironment();
         if(array_key_exists(self::$apiKeys, $environment))
             $apiKey = self::$apiKeys[$environment];
         else throw new Exception("No API key found for current environment '$environment'");

         return self::_apiCall($apiKey, $call);
    }

    protected static function _apiCall($apiKey, $call)
    {
         // Make the call to the API
    }
}

Ich hoffe, das hilft ...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top