-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.Federation.API.Common where

import Data.Aeson
import Data.OpenApi (ToSchema)
import Imports
import Test.QuickCheck
import Wire.Arbitrary

-- | This is equivalent to '()', but JSONifies to an empty object instead of an
-- empty array. Returning an empty object gives us more flexibility, allowing
-- us to expand the response without breaking compatibility.
data EmptyResponse = EmptyResponse
  deriving stock (EmptyResponse -> EmptyResponse -> Bool
(EmptyResponse -> EmptyResponse -> Bool)
-> (EmptyResponse -> EmptyResponse -> Bool) -> Eq EmptyResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EmptyResponse -> EmptyResponse -> Bool
== :: EmptyResponse -> EmptyResponse -> Bool
$c/= :: EmptyResponse -> EmptyResponse -> Bool
/= :: EmptyResponse -> EmptyResponse -> Bool
Eq, Int -> EmptyResponse -> ShowS
[EmptyResponse] -> ShowS
EmptyResponse -> String
(Int -> EmptyResponse -> ShowS)
-> (EmptyResponse -> String)
-> ([EmptyResponse] -> ShowS)
-> Show EmptyResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EmptyResponse -> ShowS
showsPrec :: Int -> EmptyResponse -> ShowS
$cshow :: EmptyResponse -> String
show :: EmptyResponse -> String
$cshowList :: [EmptyResponse] -> ShowS
showList :: [EmptyResponse] -> ShowS
Show, (forall x. EmptyResponse -> Rep EmptyResponse x)
-> (forall x. Rep EmptyResponse x -> EmptyResponse)
-> Generic EmptyResponse
forall x. Rep EmptyResponse x -> EmptyResponse
forall x. EmptyResponse -> Rep EmptyResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EmptyResponse -> Rep EmptyResponse x
from :: forall x. EmptyResponse -> Rep EmptyResponse x
$cto :: forall x. Rep EmptyResponse x -> EmptyResponse
to :: forall x. Rep EmptyResponse x -> EmptyResponse
Generic)
  deriving (Gen EmptyResponse
Gen EmptyResponse
-> (EmptyResponse -> [EmptyResponse]) -> Arbitrary EmptyResponse
EmptyResponse -> [EmptyResponse]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen EmptyResponse
arbitrary :: Gen EmptyResponse
$cshrink :: EmptyResponse -> [EmptyResponse]
shrink :: EmptyResponse -> [EmptyResponse]
Arbitrary) via (GenericUniform EmptyResponse)

instance ToSchema EmptyResponse

instance FromJSON EmptyResponse where
  parseJSON :: Value -> Parser EmptyResponse
parseJSON = String
-> (Object -> Parser EmptyResponse)
-> Value
-> Parser EmptyResponse
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"EmptyResponse" ((Object -> Parser EmptyResponse) -> Value -> Parser EmptyResponse)
-> (Parser EmptyResponse -> Object -> Parser EmptyResponse)
-> Parser EmptyResponse
-> Value
-> Parser EmptyResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser EmptyResponse -> Object -> Parser EmptyResponse
forall a b. a -> b -> a
const (Parser EmptyResponse -> Value -> Parser EmptyResponse)
-> Parser EmptyResponse -> Value -> Parser EmptyResponse
forall a b. (a -> b) -> a -> b
$ EmptyResponse -> Parser EmptyResponse
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EmptyResponse
EmptyResponse

instance ToJSON EmptyResponse where
  toJSON :: EmptyResponse -> Value
toJSON EmptyResponse
EmptyResponse = [Pair] -> Value
object []