summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/APIClient/Agent.hs
blob: cdd04e694f02ad6f26214725f31f61ce8d684ad7 (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
{-# LANGUAGE DeriveAnyClass    #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module SpaceTraders.APIClient.Agent
  ( RegisterMessage(..)
  , myAgent
  , register
  ) where

import           Control.Monad.Reader
import           Data.Aeson
import qualified Data.Text                     as T
import           GHC.Generics
import           Network.HTTP.Simple

import           SpaceTraders
import           SpaceTraders.APIClient.Client
import           SpaceTraders.Database.Agents
import           SpaceTraders.Model.Agent      (Agent)
import           SpaceTraders.Model.Contract
import           SpaceTraders.Model.Ship       (Ship)

myAgent :: SpaceTradersT (APIResponse Agent)
myAgent = do
  a@(Right ag) <- send $ setRequestPath "/v2/my/agent"
  setAgent ag
  return a


data RegisterRequest = RegisterRequest { faction :: T.Text
                                       , symbol  :: T.Text
                                       } deriving (ToJSON, Generic, Show)
data RegisterMessage = RegisterMessage { agent    :: Agent
                                       , contract :: Contract
                                       , ship     :: Ship
                                       , token    :: T.Text
                                       } deriving (FromJSON, Generic, Show)

register :: (HasRequest env, MonadIO m, MonadReader env m) => T.Text -> T.Text -> m (APIResponse RegisterMessage)
register s f = send $ setRequestPath "/v2/register"
                    . setRequestMethod "POST"
                    . setRequestBodyJSON RegisterRequest{symbol = s, faction = f}