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

module SpaceTraders.APIClient.Systems
  ( listSystems
  ) where

import Control.Exception
import qualified Data.Text as T
import qualified Database.SQLite.Simple as S
import Network.HTTP.Simple

import SpaceTraders.APIClient.Client
import SpaceTraders.APIClient.Pagination
import SpaceTraders.Database.Systems
import SpaceTraders.Model.System(System)

listSystems :: T.Text -> S.Connection -> IO (APIResponse [System])
listSystems t conn = do
  s <- getSystems conn
  listSystems' Pagination{limit=20, page=((length s) `div` 20) + 1, total=0}
  where
    listSystems' :: Pagination -> IO (APIResponse [System])
    listSystems' p = do
      resp <- sendPaginated $ setRequestPath "/v2/systems"
                            $ paginatedReq t (Just p)
      case resp of
        Left e -> throwIO e
        Right (APIMessage [] _) -> Right <$> getSystems conn
        Right (APIMessage r (Just p')) -> do
          addSystems conn r
          listSystems' (nextPage p')
        _ -> undefined