{-# LANGUAGE OverloadedStrings #-}

module Yahoo.Query (
    request
    ) where

import Control.Lens
import Data.Aeson (Value)
import qualified Data.ByteString.Lazy as BS
import Network.Wreq
import Text.Printf

import qualified Data.Char as Char
import qualified Data.Map as Map
import qualified Data.Text as Text

-- | Encodes a character for representation in an URL
encode :: Char -> String
encode c
  | c == ' ' = "+"
  | Char.isAlphaNum c || (Text.singleton c) `Text.isInfixOf` "-._~" = [c]
  | otherwise = printf "%%%02X" c

urlEncode :: String -> String
urlEncode = concatMap encode

request :: String -> IO (Response BS.ByteString)
request query = get ("https://query.yahooapis.com/v1/public/yql?q=" ++ (urlEncode query) ++ "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=")
