Last active
December 16, 2015 05:59
-
-
Save fujimura/5388019 to your computer and use it in GitHub Desktop.
Hit an web API with http-streams and aeson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Applicative | |
import Control.Monad | |
import Data.Aeson (FromJSON, parseJSON, (.:)) | |
import qualified Data.Aeson as AE | |
import Data.ByteString.Char8 (ByteString) | |
import qualified Data.ByteString.Lazy as LBS | |
import Network.Http.Client (Response, concatHandler, get) | |
import OpenSSL (withOpenSSL) | |
import System.IO.Streams (InputStream, toList) | |
data User = User | |
{ userId :: String | |
, userName :: String | |
} deriving Show | |
instance FromJSON User where | |
parseJSON (AE.Object v) = User <$> | |
v .: "id" <*> | |
v .: "name" | |
parseJSON _ = mzero | |
jsonHandler :: FromJSON a => Response -> InputStream ByteString -> IO (Maybe a) | |
jsonHandler r i = (AE.decode . LBS.fromChunks) <$> toList i | |
main :: IO () | |
main = withOpenSSL $ do | |
user <- get "http://graph.facebook.com/dfujimura" jsonHandler :: IO (Maybe User) | |
print user |
Author
fujimura
commented
Apr 15, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment