#!/usr/bin/env stack
-- stack script --resolver lts-12.21
{-# LANGUAGE OverloadedStrings #-}
import           Data.Aeson            (Value)
import qualified Data.ByteString.Char8 as S8
import qualified Data.Yaml             as Yaml
import           Data.List
import           Network.HTTP.Simple
import           System.Environment

{------------------------------------------------------------------------------
ACKNOWLEDGEMENTS:
https://haskell.fpcomplete.com/library/http-client
------------------------------------------------------------------------------}

--main :: IO ()
main = do
  args <- getArgs
  let requests = processSet parseRequest_ args
  mapM_ makeRequest requests

makeRequestNoBody a = do
  response <- httpNoBody a
  putStrLn $ "The status code was: " ++ show (getResponseStatusCode response)
  print $ getResponseHeader "Content-Type" response

makeRequest a = do
  response <- httpJSON a
  -- putStrLn $ "The status code was: " ++ show (getResponseStatusCode response)
  -- print $ getResponseHeader "Content-Type" response
  S8.putStrLn $ Yaml.encode (getResponseBody response :: Value)

processSet func as
  | length as > 0 = (func (head as)) : (processSet func (tail as))
  | otherwise = []
