openapi-generator/samples/server/petstore/haskell-servant
2018-05-08 16:44:51 +08:00
..
.openapi-generator Rename "swagger" to "openapi" (#191) 2018-04-22 21:28:17 +02:00
lib Regenerate samples (#367) 2018-05-08 16:44:51 +08:00
.openapi-generator-ignore Regenerate samples (#367) 2018-05-08 16:44:51 +08:00
openapi-petstore.cabal Regenerate samples (#367) 2018-05-08 16:44:51 +08:00
README.md Regenerate samples (#367) 2018-05-08 16:44:51 +08:00
Setup.hs Rename 2016-02-01 08:56:15 +09:00
stack.yaml [Haskell] Fix broken client/server compilation errors (#5097) 2017-03-17 16:55:11 +08:00
swagger-petstore.cabal refactor special mapping to defualt codegen 2016-05-11 15:44:06 +08:00

Auto-Generated OpenAPI Bindings to OpenAPIPetstore

The library in lib provides auto-generated-from-OpenAPI bindings to the OpenAPIPetstore API.

Installation

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

  1. Install the Haskell stack tool.
  2. Run stack install to install this package.

Main Interface

The main interface to this library is in the OpenAPIPetstore.API module, which exports the OpenAPIPetstoreBackend type. The OpenAPIPetstoreBackend type can be used to create and define servers and clients for the API.

Creating a Client

A client can be created via the createOpenAPIPetstoreClient function, which, if provided with a hostname and a port, will generate a client that can be used to access the API if it is being served at that hostname / port combination. For example, if localhost:8080 is serving the OpenAPIPetstore API, you can write:

{-# LANGUAGE RecordWildCards #-}

import OpenAPIPetstore.API

main :: IO ()
main = do
  OpenAPIPetstoreBackend{..} <- createOpenAPIPetstoreClient (ServerConfig "localhost" 8080)
  -- Any OpenAPIPetstore API call can go here.
  return ()

Creating a Server

In order to create a server, you must use the runOpenAPIPetstoreServer function. However, you unlike the client, in which case you got a OpenAPIPetstoreBackend from the library, you must instead provide a OpenAPIPetstoreBackend. For example, if you have defined handler functions for all the functions in OpenAPIPetstore.Handlers, you can write:

{-# LANGUAGE RecordWildCards #-}

import OpenAPIPetstore.API

-- A module you wrote yourself, containing all handlers needed for the OpenAPIPetstoreBackend type.
import OpenAPIPetstore.Handlers

-- Run a OpenAPIPetstore server on localhost:8080
main :: IO ()
main = do
  let server = OpenAPIPetstoreBackend{..}
  runOpenAPIPetstoreServer (ServerConfig "localhost" 8080) server

You could use optparse-applicative or a similar library to read the host and port from command-line arguments:

{-# LANGUAGE RecordWildCards #-}

module Main (main) where

import OpenAPIPetstore.API (runOpenAPIPetstoreServer, OpenAPIPetstoreBackend(..), ServerConfig(..))

import Control.Applicative ((<$>), (<*>))
import Options.Applicative (execParser, option, str, auto, long, metavar, help)

main :: IO ()
main = do
  config <- parseArguments
  runOpenAPIPetstoreServer config OpenAPIPetstoreBackend{}

-- | Parse host and port from the command line arguments.
parseArguments :: IO ServerConfig
parseArguments =
  execParser $
    ServerConfig
      <$> option str  (long "host" <> metavar "HOST" <> help "Host to serve on")
      <*> option auto (long "port" <> metavar "PORT" <> help "Port to serve on")