Skip to content

Instantly share code, notes, and snippets.

@Tehnix
Last active August 29, 2015 13:55
Show Gist options
  • Save Tehnix/8757555 to your computer and use it in GitHub Desktop.
Save Tehnix/8757555 to your computer and use it in GitHub Desktop.
API handler
{-# LANGUAGE TupleSections, OverloadedStrings, DeriveGeneric #-}
module Handler.Admin.API where
import Import
import API.CloudFlare
import API.Disqus
data CloudFlareResponseJson = CloudFlareResponseJson Bool (Either Text CloudFlareTrafficBreakdown) deriving (Show)
instance ToJSON CloudFlareResponseJson where
toJSON (CloudFlareResponseJson status r) = object ["success" .= status, "result" .= r]
data DisqusResponseJson = DisqusResponseJson Bool (Either Text DisqusResponse) deriving (Show)
instance ToJSON DisqusResponseJson where
toJSON (DisqusResponseJson status r) = object ["success" .= status, "result" .= r]
getAdminCloudFlareStatsR :: Handler Value
getAdminCloudFlareStatsR = do
extra <- getExtra
stats <- case (extraCloudflareKey extra) of
Nothing -> return Nothing
Just cfKey ->
case (extraCloudflareMail extra) of
Nothing -> return Nothing
Just cfMail ->
case (extraCloudflareZone extra) of
Nothing -> return Nothing
Just cfZone -> return =<< getCloudFlareStats (CloudFlareAuth cfKey cfMail) $ CloudFlareAction cfZone "20"
return $ maybe errJson succJson stats
where
errJson = toJSON $ CloudFlareResponseJson False (Left "Error parsing CloudFlare JSON")
succJson traffic = toJSON $ CloudFlareResponseJson True (Right traffic)
getAdminDisqusStatsR :: Handler Value
getAdminDisqusStatsR = do
extra <- getExtra
stats <- case (extraDisqusSecretKey extra) of
Nothing -> return Nothing
Just disSecret ->
case (extraDisqusAccessToken extra) of
Nothing -> return Nothing
Just disToken -> return =<< getDisqusStats $ DisqusAuth disSecret disToken
return $ maybe errJson succJson stats
where
errJson = toJSON $ DisqusResponseJson False (Left "Error parsing Disqus JSON")
succJson s = toJSON $ DisqusResponseJson True (Right s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment