Git Product home page Git Product logo

api-builder's Introduction

api-builder Build Status

Simple library for building API wrappers in Haskell – define a Builder, add some types with Receivable instances, an error type, and some routes, and you can easily use any API from Haskell code. Based on a EitherT StateT StateT monad transformer stack.

### Stack Overflow example

Define a type for a stack overflow question:

data Question = Question { title :: Text
                         , isAnswered :: Bool
                         , score :: Int
                         , tags :: [Text] }
  deriving (Show, Eq)

And a wrapper since SO wraps its JSON responses:

newtype Questions = Questions [Question]
  deriving (Show, Eq)

Add FromJSON instances for both the types:

instance FromJSON Question where
  parseJSON (Object o) =
    Question <$> o .: "title"
             <*> o .: "is_answered"
             <*> o .: "score"
             <*> o .: "tags"
  parseJSON _ = mempty

instance FromJSON Questions where
  parseJSON (Object o) = Questions <$> o .: "items"
  parseJSON _ = mempty

and a Receivable instance for Questions that just uses the FromJSON instance:

instance Receivable Questions where
  receive = useFromJSON

Define a Builder for the API endpoint:

stackOverflow :: Builder
stackOverflow = basicBuilder "StackOverflow API" "http://api.stackexchange.com"

And the Route to use to get the data:

answersRoute :: Route
answersRoute = Route { urlPieces = [ "2.2", "questions" ]
                     , urlParams = [ "order" =. Just "desc"
                                   , "sort" =. Just "activity"
                                   , "site" =. Just "stackoverflow" ]
                     , httpMethod = GET }

And a function to actually run the API:

getAnswers :: IO (Either (APIError ()) Questions)
getAnswers = execAPI stackOverflow () $ runRoute answersRoute

> getAnswers
Right (Questions [Question {title = "Using parse API with codeigniter", isAnswered = True, score = 2, tags = ["php","codeigniter","parse.com","codeigniter-2","php-5.6"]},Question {title = "Object...

api-builder's People

Contributors

intolerable avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.