{-# LANGUAGE TemplateHaskell #-}
module Calamity.HTTP.Internal.Types
( RestError(..)
, RateLimitState(..)
, DiscordResponseType(..)
, Bucket(..)
, BucketState(..)
, GatewayResponse
, BotGatewayResponse ) where
import Calamity.HTTP.Internal.Route
import Control.Concurrent.Event ( Event )
import Control.Concurrent.STM.TVar ( TVar )
import Data.Time
import Data.Aeson
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString as B
import Data.Text as T
import qualified StmContainers.Map as SC
import qualified Data.Aeson as Aeson
import Optics.TH
data RestError
= HTTPError
{ RestError -> Int
status :: Int
, RestError -> Maybe Value
response :: Maybe Value
}
| InternalClientError T.Text
deriving ( Int -> RestError -> ShowS
[RestError] -> ShowS
RestError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RestError] -> ShowS
$cshowList :: [RestError] -> ShowS
show :: RestError -> String
$cshow :: RestError -> String
showsPrec :: Int -> RestError -> ShowS
$cshowsPrec :: Int -> RestError -> ShowS
Show)
data BucketState = BucketState
{ BucketState -> Maybe UTCTime
resetTime :: Maybe UTCTime
, BucketState -> Int
resetKey :: Int
, BucketState -> Int
remaining :: Int
, BucketState -> Int
limit :: Int
, BucketState -> Int
ongoing :: Int
}
deriving ( Int -> BucketState -> ShowS
[BucketState] -> ShowS
BucketState -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BucketState] -> ShowS
$cshowList :: [BucketState] -> ShowS
show :: BucketState -> String
$cshow :: BucketState -> String
showsPrec :: Int -> BucketState -> ShowS
$cshowsPrec :: Int -> BucketState -> ShowS
Show )
newtype Bucket = Bucket
{ Bucket -> TVar BucketState
state :: TVar BucketState
}
data RateLimitState = RateLimitState
{ RateLimitState -> Map RouteKey ByteString
bucketKeys :: SC.Map RouteKey B.ByteString
, RateLimitState -> Map ByteString Bucket
buckets :: SC.Map B.ByteString Bucket
, RateLimitState -> Event
globalLock :: Event
}
data DiscordResponseType
= Good
LB.ByteString
(Maybe (BucketState, B.ByteString))
| Ratelimited
UTCTime
Bool
(Maybe (BucketState, B.ByteString))
| ServerError Int
| ClientError Int LB.ByteString
| InternalResponseError T.Text
newtype GatewayResponse = GatewayResponse
{ GatewayResponse -> Text
url :: T.Text
}
deriving stock ( Int -> GatewayResponse -> ShowS
[GatewayResponse] -> ShowS
GatewayResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GatewayResponse] -> ShowS
$cshowList :: [GatewayResponse] -> ShowS
show :: GatewayResponse -> String
$cshow :: GatewayResponse -> String
showsPrec :: Int -> GatewayResponse -> ShowS
$cshowsPrec :: Int -> GatewayResponse -> ShowS
Show )
instance Aeson.FromJSON GatewayResponse where
parseJSON :: Value -> Parser GatewayResponse
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"GatewayResponse" forall a b. (a -> b) -> a -> b
$ \Object
v ->
Text -> GatewayResponse
GatewayResponse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"url"
data BotGatewayResponse = BotGatewayResponse
{ BotGatewayResponse -> Text
url :: T.Text
, BotGatewayResponse -> Int
shards :: Int
}
deriving ( Int -> BotGatewayResponse -> ShowS
[BotGatewayResponse] -> ShowS
BotGatewayResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BotGatewayResponse] -> ShowS
$cshowList :: [BotGatewayResponse] -> ShowS
show :: BotGatewayResponse -> String
$cshow :: BotGatewayResponse -> String
showsPrec :: Int -> BotGatewayResponse -> ShowS
$cshowsPrec :: Int -> BotGatewayResponse -> ShowS
Show )
instance Aeson.FromJSON BotGatewayResponse where
parseJSON :: Value -> Parser BotGatewayResponse
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"BotGatewayResponse" forall a b. (a -> b) -> a -> b
$ \Object
v ->
Text -> Int -> BotGatewayResponse
BotGatewayResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"url"
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"shards"
$(makeFieldLabelsNoPrefix ''Bucket)
$(makeFieldLabelsNoPrefix ''BucketState)
$(makeFieldLabelsNoPrefix ''RateLimitState)
$(makeFieldLabelsNoPrefix ''GatewayResponse)
$(makeFieldLabelsNoPrefix ''BotGatewayResponse)