unsafeAwsRef
  :: (ResponseConsumer r a,
      Monoid (ResponseMetadata a),
      SignQuery r) =>
     Configuration -> ServiceConfiguration r NormalQuery -> HTTP.Manager -> IORef (ResponseMetadata a) -> r -> ResourceT IO a
unsafeAwsRef cfg info manager metadataRef request = do
  sd <- liftIO $ signatureData <$> timeInfo <*> credentials $ cfg
  let q = signQuery request info sd

  -- RPR
  liftIO $ putStrLn "String to sign: "
  liftIO $ putStrLn $ show (sqStringToSign q)
  -- liftIO $ putStrLn $ BS8.toString (sqStringToSign q)

  liftIO $ logger cfg Debug $ T.pack $ "String to sign: " ++ show (sqStringToSign q)
  httpRequest <- liftIO $ queryToHttpRequest q

  liftIO $ putStrLn $ show httpRequest

  liftIO $ logger cfg Debug $ T.pack $ "Host: " ++ show (HTTP.host httpRequest)
  resp <- do
      hresp <- HTTP.http httpRequest manager
      forM_ (HTTP.responseHeaders hresp) $ \(hname,hvalue) -> liftIO $ do
        logger cfg Debug $ T.decodeUtf8 $ "Response header '" `mappend` CI.original hname `mappend` "': '" `mappend` hvalue `mappend` "'"
      responseConsumer request metadataRef hresp
  return resp

-- | Run a URI-only AWS transaction. Returns a URI that can be sent anywhere. Does not work with all requests.
--
-- Usage:
-- @
--     uri <- awsUri cfg request
-- @
unsafeAwsRef
  :: (ResponseConsumer r a,
      Monoid (ResponseMetadata a),
      SignQuery r) =>
     Configuration -> ServiceConfiguration r NormalQuery -> HTTP.Manager -> IORef (ResponseMetadata a) -> r -> ResourceT IO a
unsafeAwsRef cfg info manager metadataRef request = do
  sd <- liftIO $ signatureData <$> timeInfo <*> credentials $ cfg
  let q = signQuery request info sd

  -- RPR
  liftIO $ putStrLn "String to sign: "
  liftIO $ putStrLn $ show (sqStringToSign q)
  -- liftIO $ putStrLn $ BS8.toString (sqStringToSign q)

  liftIO $ logger cfg Debug $ T.pack $ "String to sign: " ++ show (sqStringToSign q)
  httpRequest <- liftIO $ queryToHttpRequest q

  liftIO $ putStrLn $ show httpRequest

  liftIO $ logger cfg Debug $ T.pack $ "Host: " ++ show (HTTP.host httpRequest)
  resp <- do
      hresp <- HTTP.http httpRequest manager
      forM_ (HTTP.responseHeaders hresp) $ \(hname,hvalue) -> liftIO $ do
        logger cfg Debug $ T.decodeUtf8 $ "Response header '" `mappend` CI.original hname `mappend` "': '" `mappend` hvalue `mappend` "'"
      responseConsumer request metadataRef hresp
  return resp

-- | Run a URI-only AWS transaction. Returns a URI that can be sent anywhere. Does not work with all requests.
--
-- Usage:
-- @
--     uri <- awsUri cfg request
-- @
unsafeAwsRef cfg info manager metadataRef request = do
  sd <- liftIO $ signatureData <$> timeInfo <*> credentials $ cfg
  let q = signQuery request info sd

  -- RPR
  liftIO $ putStrLn "String to sign: "
  liftIO $ putStrLn $ show (sqStringToSign q)
  -- liftIO $ putStrLn $ BS8.toString (sqStringToSign q)

  liftIO $ logger cfg Debug $ T.pack $ "String to sign: " ++ show (sqStringToSign q)
  httpRequest <- liftIO $ queryToHttpRequest q

  liftIO $ putStrLn $ show httpRequest

  liftIO $ logger cfg Debug $ T.pack $ "Host: " ++ show (HTTP.host httpRequest)
  resp <- do
      hresp <- HTTP.http httpRequest manager
      forM_ (HTTP.responseHeaders hresp) $ \(hname,hvalue) -> liftIO $ do
        logger cfg Debug $ T.decodeUtf8 $ "Response header '" `mappend` CI.original hname `mappend` "': '" `mappend` hvalue `mappend` "'"
      responseConsumer request metadataRef hresp
  return resp

-- | Run a URI-only AWS transaction. Returns a URI that can be sent anywhere. Does not work with all requests.
--
-- Usage:
-- @
--     uri <- awsUri cfg request
-- @