| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Network.Goggles
Contents
- Running WebApiM programs
- Lifting IO programs into
WebApiM
- Lifting IO programs into
- Types
- Private key
- Exceptions
Description
Dependencies
The examples require the following declarations (which in turn mean that the req and bytestring libraries are imported by the user's project). You will also need the OverloadedStrings language extension :
import qualified Data.ByteString.Lazy as LB import Network.HTTP.Req (responseBody) import Network.Goggles
Examples
This first example, listBucket, reads content from a cloud storage bucket:
- it loads the GCP credentials (username and RSA key),
- retrieves a token via OAuth2,
- performs a single call to the Cloud Storage API endpoint that lists the metadata related to the contents of a storage bucket, and
- returns the raw API data to the user as a lazy ByteString.
listBucket :: IO LB.ByteString
listBucket = do
let usr = "...iam.gserviceaccount.com"
bucket = "<my-gcs-bucket>"
key = "<rsa_key>"
pvtkey <- parseRSAPrivateKey key
let creds = GCPServiceAccount pvtkey usr Nothing ""
hdl <- createHandle creds scopesDefault
responseBody <$> evalWebApiIO hdl (listObjects bucket)- createHandle :: HasCredentials c => Credentials c -> Options c -> IO (Handle c)
- evalWebApiIO :: Handle c -> WebApiM c a -> IO a
- liftWebApiIO :: HasCredentials c => IO a -> WebApiM c a
- newtype WebApiM c a = WebApiM {
- runWebApiM :: ReaderT (Handle c) IO a
- class HasCredentials c where
- type Credentials c
- type Options c
- type TokenContent c
- data Token c = Token {
- tToken :: TokenContent c
- tTime :: UTCTime
- data Handle c = Handle {
- credentials :: Credentials c
- token :: TVar (Maybe (Token c))
- options :: Options c
- parseRSAPrivateKey :: MonadThrow m => Text -> m PrivateKey
- data KeyException
- data JWTError
- data TokenExchangeException
- data CloudException
Running WebApiM programs
createHandle :: HasCredentials c => Credentials c -> Options c -> IO (Handle c) Source #
Create a Handle with an empty token
Lifting IO programs into WebApiM
liftWebApiIO :: HasCredentials c => IO a -> WebApiM c a Source #
Lift an `IO a` action into the WebApiM monad, and catch synchronous exceptions, while rethrowing the asynchronous ones to IO
Types
The main type of the library. It can easily be re-used in libraries that interface with more than one cloud API provider because its type parameter c lets us be declare distinct behaviours for each.
Constructors
| WebApiM | |
Fields
| |
Instances
| Monad (WebApiM c) Source # | |
| Functor (WebApiM c) Source # | |
| Applicative (WebApiM c) Source # | |
| HasCredentials c => Alternative (WebApiM c) Source # | |
| HasCredentials c => MonadIO (WebApiM c) Source # | |
| HasCredentials c => MonadRandom (WebApiM c) Source # | the whole point of this parametrization is to have a distinct MonadHttp for each API provider/DSP instance HasCredentials c => MonadHttp (Boo c) where handleHttpException = throwM |
| HasCredentials c => MonadThrow (WebApiM c) Source # | |
| HasCredentials c => MonadCatch (WebApiM c) Source # | |
| HasCredentials c => MonadReader (Handle c) (WebApiM c) Source # | |
Authentication
class HasCredentials c where Source #
This class
Minimal complete definition
Methods
tokenFetch :: WebApiM c (Token c) Source #
An authentication Token with an expiry date
Constructors
| Token | |
Fields
| |
A Handle contains all information necessary to communicating with a cloud API provider:
- Authentication credentials (e.g. username/password)
- Authentication token (used to authenticate every API call)
- Options (e.g. GCP authentication scopes)
Constructors
| Handle | |
Fields
| |
Instances
| HasCredentials c => MonadReader (Handle c) (WebApiM c) Source # | |
Private key
parseRSAPrivateKey :: MonadThrow m => Text -> m PrivateKey Source #
Parse a chunk of text into an RSA private key. For Google Cloud Platform , this is the private key associated with the user's "service account" (for server-to-server API use)
https://console.cloud.google.com/apis/credentials
Note: do not supply the RSA header and footer or any newlines (they will be inserted by this function).
Exceptions
Errors associated with JWT-encoded token request
Constructors
| BadExpirationTime !String | |
| CryptoSignError !String |
data TokenExchangeException Source #
Token exchange exceptions
Constructors
| NotFound !String | Something went wrong with the request, token not found |
| APICredentialsNotFound !String |
data CloudException Source #
Cloud API exception
Constructors
| UnknownError !String | |
| IOError !String | |
| TimeoutError !String | |
| JsonDecodeError !String | |
| XMLDecodeError !String |
Instances