@@ -23,6 +23,7 @@
2323module Network.Google.Types where
2424
2525import Control.Lens hiding (coerce)
26+import Control.Monad
2627import Control.Monad.Catch
2728import Control.Monad.Trans.Resource
2829import Data.Aeson
@@ -200,26 +201,16 @@ data Client a = Client
200201 , _cliCheck :: Status -> Bool
201202 , _cliService :: Service
202203 , _cliRequest :: Request
203− , _cliResponse :: Stream -> ResourceT IO (Either SerializeError a)
204+ , _cliResponse :: Stream -> ResourceT IO (Either String a)
204205 }
205206
206−mime :: FromStream c a
207− => Proxy c
208− -> Method
209− -> [Int]
210− -> Request
211− -> Service
212− -> Client a
207+mime :: FromStream c a => Proxy c -> Method -> [Int] -> Request -> Service -> Client a
213208mime p = client (fromStream p) (Just (contentType p))
214209
215−discard :: Method
216− -> [Int]
217− -> Request
218− -> Service
219− -> Client ()
210+discard :: Method -> [Int] -> Request -> Service -> Client ()
220211discard = client (\b -> closeResumableSource b >> pure (Right ())) Nothing
221212
222−client :: (Stream -> ResourceT IO (Either SerializeError a))
213+client :: (Stream -> ResourceT IO (Either String a))
223214 -> Maybe MediaType
224215 -> Method
225216 -> [Int]
@@ -253,19 +244,13 @@ instance ToBody JSON Value where
253244 toBody Proxy = RequestBodyLBS . encode
254245
255246class Accept c => FromStream c a where
256− fromStream :: Proxy c
257− -> Stream
258− -> ResourceT IO (Either SerializeError a)
247+ fromStream :: Proxy c -> Stream -> ResourceT IO (Either String a)
259248
260249instance FromStream OctetStream Stream where
261250 fromStream Proxy = pure . Right
262251
263252instance FromJSON a => FromStream JSON a where
264− fromStream Proxy b = do
265− lbs <- sinkLBS b
266− case eitherDecode lbs of
267− Left e -> undefined
268− Right x -> pure (Right x)
253+ fromStream Proxy = sinkLBS >=> pure . eitherDecode
269254
270255class GoogleAuth a where
271256 _AuthKey :: Traversal' a AuthKey
@@ -496,48 +481,6 @@ instance {-# OVERLAPPING #-}
496481
497482 buildClient Proxy = discard methodDelete [204]
498483
499−-- perform :: Accept
500−-- -> (Stream -> Client (Either SerializeError a))
501−-- -> Method
502−-- -> [Int]
503−-- -> Request
504−-- -> Client a
505−-- perform a meth ns f rq = undefined -- do
506− -- m <- ask
507− -- x <- lift . try $ Client.http m (client meth a rq)
508− -- either (failure . TransportError) success x
509− -- where
510− -- failure = pure . Left
511−
512− -- success rs = do
513− -- let s = Client.responseStatus rs
514− -- b = Client.responseBody rs
515− -- hs = Client.responseHeaders rs
516− -- case content hs of
517− -- unless (matches respCT (acceptCT)) $
518− -- left $ UnsupportedContentType respCT respBody
519−
520− -- Nothing -> failure (SerializeError' (SerializeError hs))
521−
522− -- Just _ | statusInvalid s -> do
523− -- lbs <- sinkLBS b
524− -- failure (ServiceError' (ServiceError s c hs lbs))
525−
526− -- Just c | contentInvalid c -> do
527− -- lbs <- sinkLBS b
528− -- failure (SerializeError' (SerializeError s c hs lbs))
529−
530−
531− -- Just c -> first SerializeError' <$> f b
532−
533− -- content :: [Header] -> Maybe c
534− -- content = parseAccept
535− -- . fromMaybe "application/octet-stream"
536− -- . lookup hContentType
537−
538− -- statusCheck s = fromEnum s `elem` ns
539− -- contentCheck = matches a
540−
541484sinkLBS :: Stream -> ResourceT IO LBS.ByteString
542485sinkLBS = fmap LBS.fromChunks . ($$+- CL.consume)
543486