http-query-0.1.3: Simple http queries
Safe HaskellNone
LanguageHaskell2010

Network.HTTP.Query

Description

A small library for querying a Web API.


import Data.Text.IO as T
import Network.HTTP.Query

main = do
  let api = "http://www.example.com/api/1"
      endpoint = api +/+ "search"
  res <- webAPIQuery endpoint $ makeKey "q" "needle"
  T.putStrLn $
    case lookupKey "results" res of
      Nothing ->
        fromMaybe "search failed" $ lookupKey "error" res
      Just results ->
        lookupKey' "location" results
Synopsis

Documentation

withURLQuery :: String -> Query -> (Request -> a) -> a Source #

Sets up an API request for some action

webAPIQuery Source #

Arguments

:: (MonadIO m, FromJSON a) 
=> String

URL of endpoint

-> Query

query options

-> m a

returned JSON

Low-level web api query

apiQueryURI Source #

Arguments

:: String

url of endpoint

-> Query

query options

-> URI 

Get the URI for a web query

type Query = [QueryItem] #

type QueryItem = (ByteString, Maybe ByteString) #

maybeKey :: String -> Maybe String -> Query Source #

Maybe create a query key

makeKey :: String -> String -> Query Source #

Make a singleton key-value Query

makeItem :: String -> String -> QueryItem Source #

Make a key-value QueryItem

(+/+) :: String -> String -> String infixr 5 Source #

Combine two path segments with a slash

"abc" +/+ "def" == "abc/def"
"abc/" +/+ "def" == "abc/def"
"abc" +/+ "/def" == "abc/def"

lookupKey :: FromJSON a => Text -> Object -> Maybe a Source #

Look up key in object

lookupKeyEither :: FromJSON a => Text -> Object -> Either String a Source #

Like lookupKey but returns error message if not found

lookupKey' :: FromJSON a => Text -> Object -> a Source #

Like lookupKey but raises an error if no key found