Copyright | (c) 2014 Patrick Bahr |
---|---|
License | BSD3 |
Maintainer | Patrick Bahr <paba@di.ku.dk> |
Stability | experimental |
Portability | non-portable (GHC Extensions) |
Safe Haskell | None |
Language | Haskell98 |
This module provides utility functions to query web APIs. These queries can then be used to feed Alfred with suggestions.
- jsonQuery :: FromJSON a => Text -> Query a
- jsonQuery' :: FromJSON a => (ByteString -> ByteString) -> Text -> Query a
- xmlQuery :: (GenericXMLString a, GenericXMLString b) => Text -> Query (Node a b)
- xmlQueryLazy :: (GenericXMLString a, GenericXMLString b) => Text -> Query (Node a b)
- escapeString :: String -> String
- escapeText :: Text -> Text
- type Query a = Query' Text a
- transformQuery :: (a -> b) -> Query' q a -> Query' q b
- type Query' q a = q -> IO (Either Text a)
Documentation
jsonQuery :: FromJSON a => Text -> Query a Source
This function performs a query by performing an HTTP GET request at the url obtained by concatenating the first argument with the second one (after escaping it). The returned query takes a string as an argument and appends it to the base url to obtain the url that is used for the query. The result is then parsed as a JSON object. For example, for a Google search:
runQuery :: Query (Text,[Text]) runQuery = jsonQuery suggestURL suggestURL = "http://google.com/complete/search?client=firefox&q="
jsonQuery' :: FromJSON a => (ByteString -> ByteString) -> Text -> Query a Source
This function is a variant of jsonQuery
that takes a function
as an additional argument that is used to transform the raw
ByteString
that is returned by the query. This can be helpful if
the source does not provide valid UTF-8 formatted JSON. For
example, for a Google search:
runQuery :: Query (Text,[Text]) runQuery = jsonQuery' (encodeUtf8 . decodeLatin1) suggestURL suggestURL = "http://google.com/complete/search?client=firefox&q="
xmlQuery :: (GenericXMLString a, GenericXMLString b) => Text -> Query (Node a b) Source
This function performs a query by performing an HTTP GET request at the url obtained by concatenating the first argument with the second one (after escaping it). The returned query takes a string as an argument and appends it to the base url to obtain the url that is used for the query. The result is then parsed as an XML document. For example, for a DBLP search:
runQuery :: Query (Node Text Text) runQuery query = xmlQuery suggestURL query suggestURL = "http://dblp.uni-trier.de/search/author?xauthor="
xmlQueryLazy :: (GenericXMLString a, GenericXMLString b) => Text -> Query (Node a b) Source
Lazy variant of xmlQueryLazy
. This function may be useful if
results tend to be lengthy and only a small prefix of the result is
used.
escapeString :: String -> String Source
Escapes the string for use in a URL.
escapeText :: Text -> Text Source
Escapes the text for use in a URL.
transformQuery :: (a -> b) -> Query' q a -> Query' q b Source
Functorial map for Query'
.