Created
June 19, 2014 20:07
-
-
Save mfenniak/d27e3e7f1b406527b676 to your computer and use it in GitHub Desktop.
My first ugly working Haskell program, making an HTTP request w/ hard-coded JSON to retrieve data from Replicon's old RepliConnect API.
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
import Network.HTTP | |
import Network.URI | |
import Data.Either | |
data InstanceInfo = | |
InstanceInfo { | |
apiURL :: URI, | |
companyKey :: String, | |
loginName :: String, | |
password :: String | |
} | |
makeRequest :: InstanceInfo -> String -> IO (Either String String) | |
makeRequest info body = | |
do resp <- simpleHTTP request | |
case resp of | |
Left connError -> error "ERROR" --connError | |
Right realResp -> do | |
case rspCode realResp of | |
code@(1, _, _) -> error $ "1xx: " ++ (show code) | |
code@(2, _, _) -> error $ "2xx: " ++ (show code) | |
code@(3, _, _) -> error $ "3xx: " ++ (show code) | |
code@(4, _, _) -> error $ "4xx: " ++ (show code) | |
code@(5, _, _) -> error $ "5xx: " ++ (show code) | |
otherwise -> error "?" | |
where request = Request { rqURI = apiURL info, | |
rqMethod = POST, | |
rqHeaders = [ | |
mkHeader HdrContentType "application/json", | |
mkHeader HdrContentLength (show $ length body) | |
], | |
rqBody = body } | |
main = | |
do | |
putStrLn "Hello, world!" | |
case parseAbsoluteURI("https://na1.replicon.com/a/RemoteApi/RemoteApi.ashx/8.25.7/") of | |
Nothing -> error "Unable to parse hard-coded API URL" | |
Just uri -> do | |
let info = InstanceInfo { apiURL = uri, | |
companyKey = "somecompanykey", | |
loginName = "someloginname", | |
Main.password = "somepassword" } | |
val <- makeRequest info "{ \"Action\": \"GetSystemPreferences\" }" | |
case val of | |
Left resp -> putStrLn $ "Got response!: " ++ resp | |
Right err -> error err |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment