{-# LANGUAGE OverloadedStrings #-}
import Network.SOAP
import Network.SOAP.Transport.HTTP
import Text.XML.Stream.Parse
import qualified Data.Text as T
import qualified Text.XML as XML
import qualified Text.XML.Writer as W


main :: IO ()
main = do
    transport <- initTransport "http://www.webservicex.net/ConvertTemperature.asmx" traceRequest (iconv "utf-8")

    out <- convertTemperatureCToF transport 25
    print out
    return ()


convertTemperatureCToF :: Transport -> Int -> IO T.Text
convertTemperatureCToF t c = invokeWS t "http://www.webserviceX.NET/ConvertTemp" () (body c) parser


body :: Int -> W.XML
body c = W.elementA "ConvertTemp" [("xmlns","http://www.webserviceX.NET/")] $ do
           e "Temperature" (T.pack $ show c)
           e "FromUnit" "degreeCelsius"
           e "ToUnit" "degreeFahrenheit"
  where
    e :: XML.Name -> T.Text -> W.XML
    e n t = W.element n t


parser :: ResponseParser T.Text
parser = StreamParser $
    force "missing response" $
        tagName "ConvertTempResponse" ignoreAttrs $ \_ ->
            force "missing result" $
                tagNoAttr "ConvertTempResult" content
