Safe Haskell | None |
---|---|
Language | Haskell2010 |
Actions for working with any of Google's APIs
Note: this module may become a standalone package at some point.
- type Api = ReaderT (String, Manager) (ExceptT ApiError IO)
- data ApiError
- runApi :: String -> Api a -> IO (Either ApiError a)
- runApi_ :: String -> Api a -> IO ()
- throwApiError :: String -> Api a
- type URL = String
- type Path = String
- type Params = [(ByteString, Maybe ByteString)]
- type DownloadSink a = ResumableSource (ResourceT IO) ByteString -> ResourceT IO a
- getJSON :: FromJSON a => URL -> Params -> Api a
- getSource :: URL -> Params -> DownloadSink a -> Api a
- postJSON :: (ToJSON a, FromJSON b) => URL -> Params -> a -> Api b
- putJSON :: (ToJSON a, FromJSON b) => URL -> Params -> a -> Api b
- requestJSON :: FromJSON a => URL -> (Request -> Request) -> Api a
- requestLbs :: URL -> (Request -> Request) -> Api (Response ByteString)
- authorize :: URL -> Api Request
- decodeBody :: FromJSON a => Response ByteString -> Api a
- addHeader :: Header -> Request -> Request
- allowStatus :: Status -> Request -> Request
- setBody :: ByteString -> Request -> Request
- setBodySource :: Int64 -> Source (ResourceT IO) ByteString -> Request -> Request
- setMethod :: Method -> Request -> Request
- liftIO :: MonadIO m => forall a. IO a -> m a
- throwError :: MonadError e m => forall a. e -> m a
- catchError :: MonadError e m => forall a. m a -> (e -> m a) -> m a
- sinkFile :: MonadResource m => FilePath -> Consumer ByteString m ()
The Api monad
type Api = ReaderT (String, Manager) (ExceptT ApiError IO) Source
A transformer stack for providing the access token and rescuing errors
HttpError HttpException | Exceptions raised by http-conduit |
InvalidJSON String | Failure to parse a response as JSON |
GenericError String | All other errors |
Run an Api
computation with the given Access token
runApi_ :: String -> Api a -> IO () Source
Like runApi
but discards the result and raises ApiError
s as exceptions
throwApiError :: String -> Api a Source
Abort an Api
computation with the given message
HTTP-related types
type Params = [(ByteString, Maybe ByteString)] Source
High-level requests
type DownloadSink a = ResumableSource (ResourceT IO) ByteString -> ResourceT IO a Source
Downloads use sinks for space efficiency and so that callers can implement
things like throttling or progress output themselves. If you just want to
download to a file, use the re-exported sinkFile
getSource :: URL -> Params -> DownloadSink a -> Api a Source
Make an authorized GET request, sending the response to the given sink
postJSON :: (ToJSON a, FromJSON b) => URL -> Params -> a -> Api b Source
Make an authorized POST request for JSON
putJSON :: (ToJSON a, FromJSON b) => URL -> Params -> a -> Api b Source
Make an authorized PUT request for JSON
Lower-level requests
requestJSON :: FromJSON a => URL -> (Request -> Request) -> Api a Source
Make an authorized request for JSON, first modifying it via the passed function
requestLbs :: URL -> (Request -> Request) -> Api (Response ByteString) Source
Make an authorized request, first modifying it via the passed function, and returning the raw response content
Api helpers
decodeBody :: FromJSON a => Response ByteString -> Api a Source
Decode a JSON body, capturing failure as an ApiError
Request helpers
allowStatus :: Status -> Request -> Request Source
Modify the Request's status check to not treat the given status as an error
setBody :: ByteString -> Request -> Request Source
setBodySource :: Int64 -> Source (ResourceT IO) ByteString -> Request -> Request Source
Re-exports
throwError :: MonadError e m => forall a. e -> m a
Is used within a monadic computation to begin exception processing.
catchError :: MonadError e m => forall a. m a -> (e -> m a) -> m a
A handler function to handle previous errors and return to normal execution. A common idiom is:
do { action1; action2; action3 } `catchError` handler
where the action
functions can call throwError
.
Note that handler
and the do-block must have the same return type.
sinkFile :: MonadResource m => FilePath -> Consumer ByteString m ()
Stream all incoming data to the given file.
Since 0.3.0