Over a million developers have joined DZone.

Writing a REST client in Haskell

DZone's Guide to

Writing a REST client in Haskell

· Integration Zone
Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

A few days ago I decided to buy some bitcoin. Then I noticed it fluctuates a lot despite a general upwards trend. Hmmm … if I just bought at the right moment and sold at a different right moment, I could make money fall out of the void!

I have since lost $5 by playing and gained $30 by leaving it alone. Obviously I suck at this …

I know! Let’s make a bot that does this! A low frequency trading bot, that sounds like fun. And let’s write it in Haskell, just to keep things interesting. Marrying the strictest of languages with the messiest of resources – the internet … what could possibly go wrong?

First order of business – a REST client.

Curator added this image for fun.

REST client

Before my bot can do any trading and intricate algorithmic trading, it needs to talk to the marketplace of choice. I picked Bitstamp because they’re the only ones that let me do this without a US bank account.

Writing a REST client in most languages is simple. Reading Bitstamp’s ticker in Python looks like this:

import requests, json
r = requests.get("https://www.bitstamp.net/api/ticker")
print json.loads(r.content)
# prints: {u'volume': u'17179.28558844', u'last': u'159.49', u'bid': u'159.49', u'high': u'161.00', u'low': u'139.00', u'ask': u'159.64'}

That’s it. Everything you need for a set of values nicely accessible as a dictionary.

In Haskell, well in Haskell figuring out how to do that took me all night, then a bit of the morning and finally a helpful tweet from a stranger to tell me just how I was misusing monads.

First of all, we are going to need a bunch of imports. The ones we really care about are the http-conduit library and the Aeson parser of JSON strings. Everything else is there because … well I’m not sure actually, but it seems to be necessary, otherwise things don’t work.

{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit
import Control.Monad.IO.Class
import Data.ByteString.Lazy
import Data.Aeson
import Data.Attoparsec.Number
import Control.Applicative
import Control.Monad.Trans

I am not perfectly certain what the OverloadedStrings bit at the top does. It’s some sort of compiler directive and most haskell libraries I find in the wild tell me I will be a much happier man if I turn it on. Shrug.

All it takes now is making an HTTP request and parsing the response as JSON. Simple, right?

Well, Haskell is strict and you can’t just parse things all willy nilly. We need to tell the parser what we expect, what we want to do with the result and what type it’s going to be. Can’t just have a bag of stuff! Nope, needs to be a well defined bag of things.

data Ticker = Ticker
              { high :: Number,
                last :: Number,
                bid :: Number,
                volume :: Number,
                low :: Number,
                ask :: Number
              } deriving Show

Great. We have a Ticker type that has a bunch of numbers and some names. That Show part seems to say that we’ll be able to print this out to the console. Smashing!

That’s not enough though, it is time for some strange hieroglyphics that tell Aeson how exactly parsing works.

instance FromJSON Ticker where
  parseJSON (Object v) = Ticker 
                         v .: "high" 
                         v .: "last" 
                         v .: "bid" 
                         v .: "volume" 
                         v .: "low" 
                         v .: "ask"

If I understand this correctly, those strange symbols are applicatives. The .: does … something … and the <$> and <*> do something else. The whole bit is about defining how to convert a key in the JSON string into a value in the Ticker type. I think.

Right, let’s make a function that will talk to the server and return a Ticker object. Maybe. If all goes well.

ticker::(MonadIO m) > m (Maybe Ticker)
ticker = get "ticker" >>= return . decode

Fairly simple stuff. Take something from the internet carefully wrapped in MonadIO, unwrap it for a bit, feed it into decode, which magically uses all the stuff we defined earlier, and wrap it back into both a MonadIO and a Maybe. Parsing can fail you know.

get::(MonadIO m) => String > m ByteString
get url = simpleHttp $ "https://www.bitstamp.net/api/"++url

This is the generalized get function that talks to Bitstamp using the simpleHttp function from http-conduit. It looks simple, but I’m sure a lot of hairy stuff is going on behind the scenes.

To make sure everything works, we run it.

main = do
  ticker >>= print


Yup, the output we get is Nothing. It is at this point you realize someone isn’t using JSON correctly and all those numbers are actually strings. Strings. Now how the hell do you tell Haskell to automagically transform those into Numbers before putting them in the Ticker object?

Messy messy internet.

But hey! Got Haskell to talk to a REST API. How cool is that!?

Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.


Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}