refactor: create getters module

group helper funcs for creating directory names and fetching network resources into a single module
This commit is contained in:
q9i 2024-07-22 15:39:35 -07:00
parent 9fbc5d0f53
commit 76973ba364
3 changed files with 25 additions and 20 deletions

17
app/Getters.hs Normal file
View file

@ -0,0 +1,17 @@
module Getters where
import Const (prog)
import System.FilePath ((</>))
import System.Directory (XdgDirectory(XdgCache, XdgConfig), getXdgDirectory)
import Control.Exception (SomeException, try)
import Network.HTTP.Request (Response, get)
pathToCache :: String -> IO String
pathToCache str = (</> str) <$> getXdgDirectory XdgCache prog
pathToConfig :: String -> IO String
pathToConfig str = (</> str) <$> getXdgDirectory XdgConfig prog
fetch :: String -> IO (Either SomeException Response)
fetch = try . get

View file

@ -7,16 +7,17 @@ module Main where
import Pure (buildCmd, readLines) import Pure (buildCmd, readLines)
import Time (activateOnSunrise, sunriseNow, sunsetNow) import Time (activateOnSunrise, sunriseNow, sunsetNow)
import Const (darkModeScript, lightModeScript, prog, query) import Const (darkModeScript, lightModeScript, query)
import Sugar (continue, crash, destruct, exec, killall) import Sugar (continue, crash, destruct, exec, killall)
import Types (ResponseMsg(..), ResponseCode(..), genericErr, toResponseMsg) import Types (ResponseCode(..), ResponseMsg(..), genericErr, toResponseMsg)
import Getters (fetch, pathToCache, pathToConfig)
import Data.List.Extra ((!?)) import Data.List.Extra ((!?))
import Data.ByteString.Char8 (unpack) import Data.ByteString.Char8 (unpack)
import System.FilePath ((</>), takeDirectory) import System.FilePath (takeDirectory)
import System.Directory (XdgDirectory(XdgCache, XdgConfig), createDirectoryIfMissing, doesFileExist, getXdgDirectory) import System.Directory (createDirectoryIfMissing, doesFileExist)
import Control.Exception (SomeException, catch, try) import Control.Exception (SomeException, catch)
import Network.HTTP.Request (Response(responseBody, responseStatus), get) import Network.HTTP.Request (Response(responseBody, responseStatus))
-- error handling -- error handling
-- refactor into modules -- refactor into modules
@ -25,19 +26,6 @@ import Network.HTTP.Request (Response(responseBody, responseStatus), get)
-- introduce liquid types and checking -- introduce liquid types and checking
-- whitepaper! -- whitepaper!
pathToCache :: String -> IO String
pathToCache str = do
dir <- getXdgDirectory XdgCache prog
return (dir </> str)
pathToConfig :: String -> IO String
pathToConfig str = do
dir <- getXdgDirectory XdgConfig prog
return (dir </> str)
fetch :: String -> IO (Either SomeException Response)
fetch = try . get
ping :: (Response -> IO ()) -> IO () -> IO () ping :: (Response -> IO ()) -> IO () -> IO ()
ping run err = do ping run err = do
res <- fetch query res <- fetch query

View file

@ -62,7 +62,7 @@ executable suntheme
main-is: Main.hs main-is: Main.hs
-- Modules included in this executable, other than Main. -- Modules included in this executable, other than Main.
other-modules: Types, Const, Pure, Time, Sugar other-modules: Types, Const, Pure, Time, Sugar, Getters
-- LANGUAGE extensions used by modules in this package. -- LANGUAGE extensions used by modules in this package.
-- other-extensions: -- other-extensions: