@@ -40,19 +40,20 @@ module Network.Google.Compute.Metadata
4040 , getMetadata
4141 ) where
4242
43+import Control.Exception (throwIO)
4344import Control.Monad.Catch
4445import Control.Monad.IO.Class (MonadIO (..))
4546import Data.Aeson (eitherDecode')
4647import Data.ByteString (ByteString)
4748import qualified Data.ByteString.Lazy as LBS
4849import Data.Char (toLower)
49−import Data.Default.Class (def)
5050import qualified Data.Text.Encoding as Text
5151import qualified Data.Text.Lazy as LText
5252import qualified Data.Text.Lazy.Encoding as LText
5353import Network.Google.Prelude (Text, (<>))
54−import Network.HTTP.Conduit (HttpException (..), Manager)
55−import qualified Network.HTTP.Conduit as Client
54+import Network.HTTP.Client (HttpException (..),
55+ HttpExceptionContent (..), Manager)
56+import qualified Network.HTTP.Client as Client
5657import Network.HTTP.Types (HeaderName)
5758import System.Environment (lookupEnv)
5859
@@ -91,7 +92,7 @@ isGCE m = liftIO $ do
9192 failure = const (pure False)
9293
9394 rq = metadataRequest
94− { Client.responseTimeout = Just 1000000
95+ { Client.responseTimeout = Client.responseTimeoutMicro 1000000
9596 }
9697
9798-- | A directory of custom metadata values that have been set for this project.
@@ -223,20 +224,24 @@ getMetadata :: MonadIO m
223224 -> [Int] -- ^ Acceptable status code responses.
224225 -> Manager
225226 -> m (Client.Response LBS.ByteString)
226−getMetadata path ss m =
227− liftIO . flip Client.httpLbs m $ metadataRequest
228− { Client.path = "/computeMetadata/v1/" <> path
229− , Client.checkStatus = \s hs cj ->
230− let c = fromEnum s
231− in if 200 <= c && c < 300 && notElem c ss
232− then Nothing
233− else Just . toException $ StatusCodeException s hs cj
234− }
227+getMetadata path statuses m =
228+ liftIO . flip Client.httpLbs m $
229+ metadataRequest
230+ { Client.path = "/computeMetadata/v1/" <> path
231+ , Client.checkResponse = \rq rs ->
232+ let c = fromEnum (Client.responseStatus rs)
233+ in if 200 <= c && c < 300 && notElem c statuses
234+ then return ()
235+ else do
236+ bs <- Client.brReadSome (Client.responseBody rs) 4096
237+ throwIO . HttpExceptionRequest rq $
238+ StatusCodeException (() <$ rs) (LBS.toStrict bs)
239+ }
235240
236241-- | A default @http-client@ 'Client.Request' with the host, port, and headers
237242-- set appropriately for @metadata.google.internal@ use.
238243metadataRequest :: Client.Request
239−metadataRequest = def
244+metadataRequest = Client.defaultRequest
240245 { Client.host = "metadata.google.internal"
241246 , Client.port = 80
242247 , Client.secure = False