@@ -22,6 +22,7 @@
23module Network.Google.Types where
23module Network.Google.Types where
25+import Control.Applicative
25import Control.Exception.Lens (exception)
26import Control.Exception.Lens (exception)
26import Control.Lens hiding (coerce)
27import Control.Lens hiding (coerce)
27import Control.Monad
28import Control.Monad
@@ -38,13 +39,9 @@ import qualified Data.Conduit.List as CL
38import Data.Data
39import Data.Data
39import Data.DList (DList)
40import Data.DList (DList)
40import qualified Data.DList as DList
41import qualified Data.DList as DList
41−import Data.Foldable (foldMap, foldl')
42+import Data.Foldable (foldl')
42−import Data.List (intersperse)
44import Data.Monoid
43import Data.Monoid
46import Data.Text (Text)
44import Data.Text (Text)
47−import qualified Data.Text as Text
48import qualified Data.Text.Encoding as Text
45import qualified Data.Text.Encoding as Text
49import Data.Text.Lazy.Builder (Builder)
46import Data.Text.Lazy.Builder (Builder)
50import qualified Data.Text.Lazy.Builder as Build
47import qualified Data.Text.Lazy.Builder as Build
@@ -55,38 +52,41 @@ import Network.HTTP.Media hiding (Accept)
55import Network.HTTP.Types hiding (Header)
52import Network.HTTP.Types hiding (Header)
56import qualified Network.HTTP.Types as HTTP
53import qualified Network.HTTP.Types as HTTP
57import Servant.API
54import Servant.API
58−import Servant.Utils.Links
60data AltJSON = AltJSON
56data AltJSON = AltJSON
61− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
57+ deriving (Eq, Ord, Show, Read, Generic, Typeable)
63instance ToText AltJSON where
59instance ToText AltJSON where
64 toText = const "json"
60 toText = const "json"
66data AltMedia = AltMedia
62data AltMedia = AltMedia
67− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
63+ deriving (Eq, Ord, Show, Read, Generic, Typeable)
69instance ToText AltMedia where
65instance ToText AltMedia where
70 toText = const "media"
66 toText = const "media"
72newtype AuthKey = AuthKey Text
68newtype AuthKey = AuthKey Text
73− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToText)
69+ deriving (Eq, Ord, Show, Read, Generic, Typeable, ToText)
75newtype OAuthToken = OAuthToken Text
71newtype OAuthToken = OAuthToken Text
76− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
72+ deriving (Eq, Ord, Show, Read, Generic, Typeable)
78instance ToText OAuthToken where
74instance ToText OAuthToken where
79− toText (OAuthToken t) = "Bearer " <> t
75+ toText (OAuthToken t) = t
77+newtype OAuthScope = OAuthScope Text
78+ deriving (Eq, Ord, Show, Read, Generic, Typeable, ToText)
80+newtype Bearer a = Bearer a
82+instance ToText a => ToText (Bearer a) where
83+ toText = mappend "Bearer " . toText
81newtype MediaDownload a = MediaDownload a
85newtype MediaDownload a = MediaDownload a
83_MediaDownload :: Iso' (MediaDownload a) a
87_MediaDownload :: Iso' (MediaDownload a) a
84_MediaDownload = iso (\(MediaDownload x) -> x) MediaDownload
88_MediaDownload = iso (\(MediaDownload x) -> x) MediaDownload
86−instance GoogleAuth a => GoogleAuth (MediaDownload a) where
87− _AuthKey = _MediaDownload . _AuthKey
88− _AuthToken = _MediaDownload . _AuthToken
90_Coerce :: (Coercible a b, Coercible b a) => Iso' a b
90_Coerce :: (Coercible a b, Coercible b a) => Iso' a b
91_Coerce = iso coerce coerce
91_Coerce = iso coerce coerce
@@ -101,7 +101,7 @@ _Default = iso f Just
101type Stream = ResumableSource (ResourceT IO) ByteString
101type Stream = ResumableSource (ResourceT IO) ByteString
103newtype ServiceId = ServiceId Text
103newtype ServiceId = ServiceId Text
104− deriving (Eq, Show, Data, Typeable)
104+ deriving (Eq, Show, Typeable)
106data Error
106data Error
107 = TransportError HttpException
107 = TransportError HttpException
@@ -168,6 +168,18 @@ data Service = Service
168 , _svcPort :: !Int
168 , _svcPort :: !Int
169 , _svcSecure :: !Bool
169 , _svcSecure :: !Bool
170 , _svcTimeout :: !(Maybe Seconds)
170 , _svcTimeout :: !(Maybe Seconds)
171+ , _svcScopes :: ![OAuthScope]
174+defaultService :: ServiceId -> ByteString -> Builder -> Service
175+defaultService i h p = Service
181+ , _svcTimeout = Just 70
173-- | An intermediary request builder.
185-- | An intermediary request builder.
@@ -178,6 +190,14 @@ data Request = Request
178 , _rqBody :: Maybe (MediaType, RequestBody)
190 , _rqBody :: Maybe (MediaType, RequestBody)
193+instance Monoid Request where
194+ mempty = Request mempty mempty mempty Nothing
195+ mappend a b = Request
196+ (_rqPath a <> "/" <> _rqPath b)
197+ (_rqQuery a <> _rqQuery b)
198+ (_rqHeaders a <> _rqHeaders b)
199+ (_rqBody b <|> _rqBody a)
181appendPath :: Request -> Builder -> Request
201appendPath :: Request -> Builder -> Request
182appendPath rq x = rq { _rqPath = _rqPath rq <> "/" <> x }
202appendPath rq x = rq { _rqPath = _rqPath rq <> "/" <> x }
@@ -256,11 +276,7 @@ instance FromStream OctetStream Stream where
256instance FromJSON a => FromStream JSON a where
276instance FromJSON a => FromStream JSON a where
257 fromStream Proxy = sinkLBS >=> pure . eitherDecode
277 fromStream Proxy = sinkLBS >=> pure . eitherDecode
259−class GoogleAuth a where
260− _AuthKey :: Traversal' a AuthKey
261− _AuthToken :: Traversal' a OAuthToken
263−class GoogleAuth a => GoogleRequest a where
279+class GoogleRequest a where
264 type Rs a :: *
280 type Rs a :: *
266 requestClient :: a -> Client (Rs a)
282 requestClient :: a -> Client (Rs a)