aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: bdbef74524c55be22a09a46c426f2fe56472ba37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Main
    ( main
    ) where

import RIO

import Network.HTTP.Req
    ( jsonResponse
    , req
    , (=:)
    , NoReqBody (..)
    , (/:)
    , GET (..)
    , https
    , responseBody
    , Req
    , defaultHttpConfig
    , runReq
    )

import Data.Tomato (Tomato)
import Tomato.Post (postTomato)

import qualified Data.Aeson.Types as Ae


data App = App
    { appLogFunc :: !LogFunc
    , appName :: !Utf8Builder
    , appToken :: !Text
    }

instance HasLogFunc App where
    logFuncL = lens appLogFunc (\x y -> x { appLogFunc = y })

runApp :: RIO App a -> IO a
runApp inner = do
  logOptions' <- logOptionsHandle stderr False
  -- let logOptions = setLogUseTime True $ setLogUseLoc True logOptions'
  let logOptions = logOptions'
  withLogFunc logOptions $ \logFunc -> do
    let app = App
          { appLogFunc = logFunc
          , appName = "Tomato Bot"
          , appToken =  "placeholder"
          }
    runRIO app inner

randomTomato :: Req Tomato
randomTomato = do
    let url = https "api.unsplash.com" /: "photos" /: "random"
    js <- req GET url NoReqBody jsonResponse $
        "query" =: ("tomato" :: Text) <>
        "client_id" =: ("FbzqI-oR7277JwL1ZGsyUw7yG1F5U0U3WhQ3kOW71Do" :: Text)
    tomato <- case Ae.fromJSON (responseBody js) of
        Ae.Success r -> pure r
        Ae.Error _s  -> error "deal with this later"
    return tomato

tomatoBot :: RIO App ()
tomatoBot = do
  name <- view $ to appName
  logInfo $ "Hello, " <> name
  logInfo $ "Fetching tomato"
  -- tomato <- runReq defaultHttpConfig randomTomato
  -- logInfo $ displayShow tomato
  -- url <- runReq defaultHttpConfig postTomato
  runReq defaultHttpConfig postTomato
  -- logInfo $ displayShow url
  logInfo $ "All done"

main :: IO ()
main = runApp tomatoBot