{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleContexts #-} module Util where import Servant hiding (addHeader) import Servant.Client.Core.Request import Servant.Client.Core.Request (addHeader) import qualified Data.Text as T import Data.Text.Lazy.Encoding import Data.ByteString.Builder import Data.List (intercalate) import Data.List.Split (chunksOf) import Data.String (fromString) import Web.Cookie import Text.Printf (printf) setCookie :: SetCookie -> Request -> Request setCookie c = addHeader "Set-Cookie" (decodeUtf8 . toLazyByteString $ renderSetCookie c) formatMoney :: Int -> T.Text formatMoney amount = pre <> t <> "," <> c where pre = if amount < 0 then "-" else "" t = fromString $ reverse (intercalate "." $ chunksOf 3 $ reverse $ fst sp) c = fromString $ snd sp sp = tail <$> break (== '.') (printf "%.2f" (fromIntegral $ abs amount :: Float)) redirect303 :: Link -> ServerError redirect303 link = err303 { errHeaders = [ ("Location", "/" <> (fromString $ show $ linkURI link)) ] } parseCookieText :: T.Text -> [(T.Text, T.Text)] parseCookieText = map (\x -> let (one, two) = T.breakOn "=" x in (one, T.tail two)) . T.splitOn "; "