module Network.CircleCI.Cache (
clearCache
, CacheCleared (..)
, module Network.CircleCI.Common.Types
, module Network.CircleCI.Common.Run
) where
import Network.CircleCI.Common.URL
import Network.CircleCI.Common.Types
import Network.CircleCI.Common.HTTPS
import Network.CircleCI.Common.Run
import Control.Monad ( mzero )
import Control.Monad.Except ( runExceptT )
import Control.Monad.Reader ( ask )
import Control.Monad.IO.Class ( liftIO )
import Data.Aeson
import Data.Aeson.Types
import qualified Data.Proxy as P
import Data.Text ( Text )
import Network.HTTP.Client ( Manager )
import Servant.API
import Servant.Client
clearCache :: ProjectPoint
-> CircleCIResponse CacheCleared
clearCache project = do
AccountAPIToken token <- ask
liftIO . runExceptT $ do
manager <- httpsManager
servantClearCache (userName project)
(projectName project)
(Just token)
manager
apiBaseUrl
data CacheCleared = CacheSuccessfullyCleared
| UnableToClearCache ErrorMessage
deriving (Show)
instance FromJSON CacheCleared where
parseJSON (Object o) =
o .: "status" >>= toCacheCleared
parseJSON _ = mzero
toCacheCleared :: Text -> Parser CacheCleared
toCacheCleared rawStatus = return $
if | rawStatus `elem` okMessages -> CacheSuccessfullyCleared
| otherwise -> UnableToClearCache rawStatus
where
okMessages = [ "build dependency caches deleted"
, "build caches deleted"
]
type CacheAPI = ClearCacheCall
type ClearCacheCall =
"project"
:> Capture "username" UserName
:> Capture "project" ProjectName
:> "build-cache"
:> QueryParam "circle-token" Token
:> Delete '[JSON] CacheCleared
servantClearCache :: UserName
-> ProjectName
-> Maybe Token
-> Manager
-> BaseUrl
-> ClientM CacheCleared
servantClearCache = client cacheAPI
cacheAPI :: P.Proxy CacheAPI
cacheAPI = P.Proxy