Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting endpoints from environment variables #1013

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/amazonka-core/src/Amazonka/Endpoint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ module Amazonka.Endpoint
( -- * Endpoint
setEndpoint,
defaultEndpoint,
customEndpoints,
)
where

import Amazonka.Data.ByteString
import Amazonka.Data.Text (toText)
import Amazonka.Prelude
import Amazonka.Types
import qualified Amazonka.Types as Service (Service (abbrev))
import qualified Data.CaseInsensitive as CI
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Network.HTTP.Client as Client
import qualified System.Environment as Environment

-- | A convenience function for overriding the 'Service' 'Endpoint'.
--
Expand Down Expand Up @@ -95,3 +103,32 @@ defaultEndpoint Service {endpointPrefix = p} r = go (CI.mk p)
}

reg = toBS r

-- | Retrieve custom endpoints from environment variables:
--
-- * @AWS_ENDPOINT_URL@
-- * @AWS_ENDPOINT_URL_<SERVICE>@
--
-- The latter takes precedence over the former.
customEndpoints :: (MonadIO m) => m (Service -> Service)
customEndpoints = do
environment <- liftIO Environment.getEnvironment
let globalUrl = lookup "AWS_ENDPOINT_URL" environment >>= Client.parseUrlThrow
let serviceUrls = mapMaybe (\(k, v) -> (,v) . map Char.toLower <$> removePrefix "AWS_ENDPOINT_URL_" k) environment
let override s =
case lookup (Text.unpack . Text.toLower . toText $ Service.abbrev s) serviceUrls of
Just x -> setEndpointMaybe (Client.parseUrlThrow x) s
Nothing -> setEndpointMaybe globalUrl s
pure override
where
removePrefix :: String -> String -> Maybe String
removePrefix prefix s =
if prefix `List.isPrefixOf` s
then Just $ drop (length prefix) s
else Nothing

setEndpointMaybe :: Maybe Client.Request -> Service -> Service
setEndpointMaybe mreq s =
case mreq of
Just req -> setEndpoint (Client.secure req) (Client.host req) (Client.port req) s
Nothing -> s