-- | Perhaps this module should be a separate package and published to hackage.
module Network.RabbitMqAdmin where

import Data.Aeson
import Imports
import Servant
import Servant.Client
import Servant.Client.Generic

type RabbitMqBasicAuth = BasicAuth "RabbitMq Management" BasicAuthData

type VHost = Text

type QueueName = Text

-- | Upstream Docs:
-- https://rawcdn.githack.com/rabbitmq/rabbitmq-server/v3.12.0/deps/rabbitmq_management/priv/www/api/index.html
data AdminAPI route = AdminAPI
  { -- | NOTE: This endpoint can be made paginated, but that complicates
    -- consumer code a little. This might be needed for performance tuning
    -- later, but perhaps not.
    forall route.
AdminAPI route
-> route
   :- ("api"
       :> ("queues"
           :> (Capture "vhost" Text
               :> (QueryParam "name" Text
                   :> (QueryParam "use_regex" Bool :> Get '[JSON] [Queue])))))
listQueuesByVHost ::
      route
        :- "api"
          :> "queues"
          :> Capture "vhost" VHost
          :> QueryParam "name" Text
          :> QueryParam "use_regex" Bool
          :> Get '[JSON] [Queue],
    forall route.
AdminAPI route
-> route
   :- ("api"
       :> ("queues"
           :> (Capture "vhost" Text
               :> (Capture "queue" Text :> DeleteNoContent))))
deleteQueue ::
      route
        :- "api"
          :> "queues"
          :> Capture "vhost" VHost
          :> Capture "queue" QueueName
          :> DeleteNoContent
  }
  deriving ((forall x. AdminAPI route -> Rep (AdminAPI route) x)
-> (forall x. Rep (AdminAPI route) x -> AdminAPI route)
-> Generic (AdminAPI route)
forall x. Rep (AdminAPI route) x -> AdminAPI route
forall x. AdminAPI route -> Rep (AdminAPI route) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall route x. Rep (AdminAPI route) x -> AdminAPI route
forall route x. AdminAPI route -> Rep (AdminAPI route) x
$cfrom :: forall route x. AdminAPI route -> Rep (AdminAPI route) x
from :: forall x. AdminAPI route -> Rep (AdminAPI route) x
$cto :: forall route x. Rep (AdminAPI route) x -> AdminAPI route
to :: forall x. Rep (AdminAPI route) x -> AdminAPI route
Generic)

data AuthenticatedAPI route = AuthenticatedAPI
  { forall route.
AuthenticatedAPI route
-> route :- (RabbitMqBasicAuth :> ToServant AdminAPI AsApi)
api ::
      route
        :- RabbitMqBasicAuth
          :> ToServant AdminAPI AsApi
  }
  deriving ((forall x.
 AuthenticatedAPI route -> Rep (AuthenticatedAPI route) x)
-> (forall x.
    Rep (AuthenticatedAPI route) x -> AuthenticatedAPI route)
-> Generic (AuthenticatedAPI route)
forall x. Rep (AuthenticatedAPI route) x -> AuthenticatedAPI route
forall x. AuthenticatedAPI route -> Rep (AuthenticatedAPI route) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall route x.
Rep (AuthenticatedAPI route) x -> AuthenticatedAPI route
forall route x.
AuthenticatedAPI route -> Rep (AuthenticatedAPI route) x
$cfrom :: forall route x.
AuthenticatedAPI route -> Rep (AuthenticatedAPI route) x
from :: forall x. AuthenticatedAPI route -> Rep (AuthenticatedAPI route) x
$cto :: forall route x.
Rep (AuthenticatedAPI route) x -> AuthenticatedAPI route
to :: forall x. Rep (AuthenticatedAPI route) x -> AuthenticatedAPI route
Generic)

data Queue = Queue {Queue -> Text
name :: Text, Queue -> Text
vhost :: Text}
  deriving (Int -> Queue -> ShowS
[Queue] -> ShowS
Queue -> String
(Int -> Queue -> ShowS)
-> (Queue -> String) -> ([Queue] -> ShowS) -> Show Queue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Queue -> ShowS
showsPrec :: Int -> Queue -> ShowS
$cshow :: Queue -> String
show :: Queue -> String
$cshowList :: [Queue] -> ShowS
showList :: [Queue] -> ShowS
Show, Queue -> Queue -> Bool
(Queue -> Queue -> Bool) -> (Queue -> Queue -> Bool) -> Eq Queue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Queue -> Queue -> Bool
== :: Queue -> Queue -> Bool
$c/= :: Queue -> Queue -> Bool
/= :: Queue -> Queue -> Bool
Eq, (forall x. Queue -> Rep Queue x)
-> (forall x. Rep Queue x -> Queue) -> Generic Queue
forall x. Rep Queue x -> Queue
forall x. Queue -> Rep Queue x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Queue -> Rep Queue x
from :: forall x. Queue -> Rep Queue x
$cto :: forall x. Rep Queue x -> Queue
to :: forall x. Rep Queue x -> Queue
Generic)

instance FromJSON Queue

instance ToJSON Queue

adminClient :: BasicAuthData -> AdminAPI (AsClientT ClientM)
adminClient :: BasicAuthData -> AdminAPI (AsClientT ClientM)
adminClient BasicAuthData
ba = ToServant AdminAPI (AsClientT ClientM)
-> AdminAPI (AsClientT ClientM)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant (ToServant AdminAPI (AsClientT ClientM)
 -> AdminAPI (AsClientT ClientM))
-> ToServant AdminAPI (AsClientT ClientM)
-> AdminAPI (AsClientT ClientM)
forall a b. (a -> b) -> a -> b
$ AuthenticatedAPI (AsClientT ClientM)
clientWithAuth.api BasicAuthData
ba
  where
    clientWithAuth :: AuthenticatedAPI (AsClientT ClientM)
    clientWithAuth :: AuthenticatedAPI (AsClientT ClientM)
clientWithAuth = AuthenticatedAPI (AsClientT ClientM)
forall (routes :: * -> *) (m :: * -> *).
(HasClient m (ToServantApi routes),
 GenericServant routes (AsClientT m),
 Client m (ToServantApi routes) ~ ToServant routes (AsClientT m)) =>
routes (AsClientT m)
genericClient