From 32d43c3245ee3b686017ab79ba9bb65500481e91 Mon Sep 17 00:00:00 2001 From: Even Brenden Date: Wed, 25 Jan 2023 13:07:05 +0100 Subject: [PATCH] Add apiKey to Config --- example/Main.hs | 2 +- src/Unleash/Client.hs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/example/Main.hs b/example/Main.hs index 85d02de..14b50d6 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -27,7 +27,7 @@ featureToggle = "your-feature-toggle" main :: IO () main = do - config <- makeConfig "unleash-client-haskell-example" "localhost" unleashServer + config <- makeConfig "unleash-client-haskell-example" "localhost" unleashServer Nothing registerApplication config let threads = ($ config) <$> [statePoller, metricsPusher, application] runConcurrently $ traverse_ Concurrently threads diff --git a/src/Unleash/Client.hs b/src/Unleash/Client.hs index 58f63a6..73a57a5 100644 --- a/src/Unleash/Client.hs +++ b/src/Unleash/Client.hs @@ -24,8 +24,8 @@ import Unleash.HttpClient (getAllClientFeatures, register, sendMetrics) -- Smart constructor for Unleash client configuration -- Initializes the mutable variables properly -makeConfig :: MonadIO m => Text -> Text -> BaseUrl -> m Config -makeConfig applicationName instanceId serverUrl = do +makeConfig :: MonadIO m => Text -> Text -> BaseUrl -> Maybe Text -> m Config +makeConfig applicationName instanceId serverUrl apiKey = do state <- liftIO newEmptyMVar metrics <- liftIO $ newMVar mempty metricsBucketStart <- liftIO $ getCurrentTime >>= newMVar @@ -40,6 +40,7 @@ makeConfig applicationName instanceId serverUrl = do metrics = metrics, metricsBucketStart = metricsBucketStart, metricsPushIntervalInSeconds = 8, + apiKey = apiKey, httpClientEnvironment = clientEnv } @@ -53,6 +54,7 @@ data Config = Config metrics :: MVar [(Text, Bool)], metricsBucketStart :: MVar UTCTime, metricsPushIntervalInSeconds :: Int, + apiKey :: Maybe Text, httpClientEnvironment :: ClientEnv } @@ -69,14 +71,14 @@ registerClient config = do started = now, intervalSeconds = config.metricsPushIntervalInSeconds } - void <$> register config.httpClientEnvironment Nothing registrationPayload + void <$> register config.httpClientEnvironment config.apiKey registrationPayload -- Fetches the most recent feature toggle set from the Unleash server -- Meant to be run every statePollIntervalInSeconds -- Non-blocking pollState :: MonadIO m => Config -> m (Either ClientError ()) pollState config = do - eitherFeatures <- getAllClientFeatures config.httpClientEnvironment Nothing + eitherFeatures <- getAllClientFeatures config.httpClientEnvironment config.apiKey either (const $ pure ()) (void . updateState config.state) eitherFeatures pure . void $ eitherFeatures where @@ -100,7 +102,7 @@ pushMetrics config = do stop = now, toggles = bucket } - void <$> sendMetrics config.httpClientEnvironment Nothing metricsPayload + void <$> sendMetrics config.httpClientEnvironment config.apiKey metricsPayload -- Checks if a feature is enabled or not -- Blocks until first feature toggle set is received