| Safe Haskell | None |
|---|
APIBuilder.API
- type API s e a = EitherT (APIError e) (StateT Builder (StateT s IO)) a
- runAPI :: Builder -> s -> API s e a -> IO (Either (APIError e) a)
- runRoute :: (FromJSON a, FromJSON e) => Route -> API s e a
- routeRequest :: Builder -> Route -> Maybe Request
- liftEither :: EitherT (APIError e) (StateT Builder (StateT s IO)) a -> API s e a
- liftBuilder :: StateT Builder (StateT s IO) a -> API s e a
- liftState :: StateT s IO a -> API s e a
- name :: Text -> API s e ()
- baseURL :: Text -> API s e ()
- customizeRoute :: (Route -> Route) -> API s e ()
- customizeRequest :: (Request -> Request) -> API s e ()
API
type API s e a = EitherT (APIError e) (StateT Builder (StateT s IO)) aSource
Main API type. s is the API's internal state, e is the API's custom error type,
and a is the result when the API runs.
Running the API
Arguments
| :: Builder | initial |
| -> s | initial state |
| -> API s e a | the actual |
| -> IO (Either (APIError e) a) | IO action that returns either an error or the result |
Runs an API by executing its transformer stack and dumping it all into IO.
runRoute :: (FromJSON a, FromJSON e) => Route -> API s e aSource
Runs a Route. Infers the type to convert to from the JSON with the a in API,
and infers the error type from e.
routeRequest :: Builder -> Route -> Maybe RequestSource
Try to construct a Request from a Route (with the help of the Builder). Returns Nothing if
the URL is invalid or there is another error with the Route.
Lifting
liftEither :: EitherT (APIError e) (StateT Builder (StateT s IO)) a -> API s e aSource
Lifts an action that works on an API to an action that works on an API.
This function is provided solely for future-proofing in the case that more transformers
need to be stacked on top - it's implemented simply as id for the moment.
liftBuilder :: StateT Builder (StateT s IO) a -> API s e aSource
Lifts an action that operates on a Builder to one that works on an API. Useful
mainly for gaining access to a Builder from inside an API.
liftState :: StateT s IO a -> API s e aSource
Lifts an action on an API's state type s to one that works on the API. Good
for messing with the state from inside the API.
Changing the Builder within the API
name :: Text -> API s e ()Source
Modify the name of the Builder from inside an API. Using this is probably not the best idea,
it's nice if the Builder's name is stable at least.
baseURL :: Text -> API s e ()Source
Modify the baseURL of the Builder from inside an API.
Can be useful for changing the API's endpoints for certain requests.
customizeRoute :: (Route -> Route) -> API s e ()Source
Modify every Route before it runs. Useful for adding extra params to every query,
for example.
customizeRequest :: (Request -> Request) -> API s e ()Source
Modify every Request before the API fetches it. Useful for adding headers to every request,
for example.