Last active
August 29, 2015 13:55
-
-
Save Tehnix/8757555 to your computer and use it in GitHub Desktop.
API handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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