All Downloads are FREE. Search and download functionalities are using the official Maven repository.

haskell-servant.README.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
# Auto-Generated OpenAPI Bindings to `{{title}}`

The library in `lib` provides auto-generated-from-OpenAPI bindings to the {{title}} API.

## Installation

Installation follows the standard approach to installing Stack-based projects.

1. Install the [Haskell `stack` tool](http://docs.haskellstack.org/en/stable/README).
2. Run `stack install` to install this package.

Otherwise, if you already have a Stack project, you can include this package under the `packages` key in your `stack.yaml`:
```yaml
packages:
- location:
    git: https://github.com/yourGitOrg/yourGitRepo
    commit: somecommit
```

## Main Interface

The main interface to this library is in the `{{title}}.API` module, which exports the {{title}}Backend type. The {{title}}Backend
type can be used to create and define servers and clients for the API.

## Creating a Client

A client can be created via the `create{{title}}Client` function, which will generate a function for every endpoint of the API.
Then these functions can be invoked with `run{{title}}ClientWithManager` or more conveniently with `call{{title}}Client`
(depending if you want an `Either` back or you want to catch) to access the API endpoint they refer to, if the API is served
at the `url` you specified.

For example, if `localhost:8080` is serving the {{title}} API, you can write:

```haskell
{-# LANGUAGE RecordWildCards #-}

import {{title}}.API as API

import           Network.HTTP.Client     (newManager)
import           Network.HTTP.Client.TLS (tlsManagerSettings)
import           Servant.Client          (ClientEnv, mkClientEnv, parseBaseUrl)


main :: IO ()
main = do
  -- Configure the BaseUrl for the client
  url <- parseBaseUrl "http://localhost:8080/"

  -- You probably want to reuse the Manager across calls, for performance reasons
  manager <- newManager tlsManagerSettings

  -- Create the client (all endpoint functions will be available)
  {{title}}Backend{..} <- API.create{{title}}Client

  -- Any {{title}} API call can go here, e.g. here we call `getSomeEndpoint`
  API.call{{title}} (mkClientEnv manager url) getSomeEndpoint
```

## Creating a Server

In order to create a server, you must use the `run{{title}}Server` function. However, you unlike the client, in which case you *got* a `{{title}}Backend`
from the library, you must instead *provide* a `{{title}}Backend`. For example, if you have defined handler functions for all the
functions in `{{title}}.Handlers`, you can write:

```haskell
{-# LANGUAGE RecordWildCards #-}

import {{title}}.API

-- A module you wrote yourself, containing all handlers needed for the {{title}}Backend type.
import {{title}}.Handlers

-- Run a {{title}} server on localhost:8080
main :: IO ()
main = do
  let server = {{title}}Backend{..}
      config = Config "http://localhost:8080/"
  run{{title}}Server config server
```




© 2015 - 2024 Weber Informatics LLC | Privacy Policy