From b3271e90e6f21b359aa9a2a18792687c0ea00e65 Mon Sep 17 00:00:00 2001 From: Matthias Heinzel Date: Tue, 4 Aug 2020 17:51:13 +0200 Subject: [PATCH] create first endpoint in Federation API --- libs/wire-api-federation/package.yaml | 1 + .../src/Wire/API/Federation/API.hs | 30 +++++++++ .../Wire/API/Federation/API/Conversation.hs | 63 +++++++++++++++++++ .../API/Federation/APISpec.hs} | 10 ++- .../wire-api-federation.cabal | 7 ++- 5 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 libs/wire-api-federation/src/Wire/API/Federation/API.hs create mode 100644 libs/wire-api-federation/src/Wire/API/Federation/API/Conversation.hs rename libs/wire-api-federation/test/Test/{SerializationSpec.hs => Wire/API/Federation/APISpec.hs} (72%) diff --git a/libs/wire-api-federation/package.yaml b/libs/wire-api-federation/package.yaml index ded3759a671..38e1c253a9d 100644 --- a/libs/wire-api-federation/package.yaml +++ b/libs/wire-api-federation/package.yaml @@ -38,3 +38,4 @@ tests: - wire-api-federation - hspec - hspec-discover + - metrics-wai diff --git a/libs/wire-api-federation/src/Wire/API/Federation/API.hs b/libs/wire-api-federation/src/Wire/API/Federation/API.hs new file mode 100644 index 00000000000..7bb48ee3da2 --- /dev/null +++ b/libs/wire-api-federation/src/Wire/API/Federation/API.hs @@ -0,0 +1,30 @@ +-- This file is part of the Wire Server implementation. +-- +-- Copyright (C) 2020 Wire Swiss GmbH +-- +-- 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 . + +module Wire.API.Federation.API where + +import GHC.Generics (Generic) +import Servant.API.Generic (AsApi, ToServant, (:-)) +import qualified Wire.API.Federation.API.Conversation as Conversation (Api) + +type PlainApi = ToServant Api AsApi + +-- FUTUREWORK: Add Swagger docs +data Api routes = Api + { conversation :: routes :- ToServant Conversation.Api AsApi + } + deriving stock (Generic) diff --git a/libs/wire-api-federation/src/Wire/API/Federation/API/Conversation.hs b/libs/wire-api-federation/src/Wire/API/Federation/API/Conversation.hs new file mode 100644 index 00000000000..6640cff5917 --- /dev/null +++ b/libs/wire-api-federation/src/Wire/API/Federation/API/Conversation.hs @@ -0,0 +1,63 @@ +{-# LANGUAGE DerivingVia #-} + +-- This file is part of the Wire Server implementation. +-- +-- Copyright (C) 2020 Wire Swiss GmbH +-- +-- 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 . + +module Wire.API.Federation.API.Conversation where + +import Data.Aeson (FromJSON, ToJSON) +import Data.Id (ConvId, UserId) +import Data.Qualified (Qualified) +import Imports +import Servant.API (Capture, JSON, Post, ReqBody, (:>)) +import Servant.API.Generic ((:-)) +import Test.QuickCheck (Arbitrary (arbitrary)) +import qualified Test.QuickCheck as QC +import Wire.API.Federation.Event (ConversationEvent, MemberJoin) +import Wire.API.Federation.Util.Aeson (CustomEncoded (CustomEncoded)) + +data Api routes = Api + { joinConversationById :: + routes + :- "f" + :> "conversation" + :> Capture "cnv" (Qualified ConvId) + :> "join" + :> ReqBody '[JSON] JoinConversationByIdRequest + :> Post '[JSON] (ConversationUpdateResult MemberJoin) + } + deriving stock (Generic) + +data JoinConversationByIdRequest = JoinConversationByIdRequest + { joinUserId :: Qualified UserId + } + deriving stock (Eq, Show, Generic) + deriving (ToJSON, FromJSON) via (CustomEncoded JoinConversationByIdRequest) + +data ConversationUpdateResult a + = ConversationUpdated (ConversationEvent a) + | ConversationUnchanged + deriving stock (Eq, Show, Generic, Foldable, Functor, Traversable) + deriving (ToJSON, FromJSON) via (CustomEncoded (ConversationUpdateResult a)) + +-- Arbitrary + +instance Arbitrary JoinConversationByIdRequest where + arbitrary = JoinConversationByIdRequest <$> arbitrary + +instance Arbitrary a => Arbitrary (ConversationUpdateResult a) where + arbitrary = QC.oneof [pure ConversationUnchanged, ConversationUpdated <$> arbitrary] diff --git a/libs/wire-api-federation/test/Test/SerializationSpec.hs b/libs/wire-api-federation/test/Test/Wire/API/Federation/APISpec.hs similarity index 72% rename from libs/wire-api-federation/test/Test/SerializationSpec.hs rename to libs/wire-api-federation/test/Test/Wire/API/Federation/APISpec.hs index 3deccdf7b35..770447c8f11 100644 --- a/libs/wire-api-federation/test/Test/SerializationSpec.hs +++ b/libs/wire-api-federation/test/Test/Wire/API/Federation/APISpec.hs @@ -15,11 +15,15 @@ -- You should have received a copy of the GNU Affero General Public License along -- with this program. If not, see . -module Test.SerializationSpec where +module Test.Wire.API.Federation.APISpec where +import Data.Metrics.Servant (routesToPaths) +import Data.Metrics.Test (pathsConsistencyCheck) import Imports -import Test.Hspec +import Test.Hspec (Spec, it, shouldBe) +import Wire.API.Federation.API as API spec :: Spec spec = do - pure () + it "API consistency" $ do + pathsConsistencyCheck (routesToPaths @API.PlainApi) `shouldBe` mempty diff --git a/libs/wire-api-federation/wire-api-federation.cabal b/libs/wire-api-federation/wire-api-federation.cabal index 29c9aff6de3..7ad62767acf 100644 --- a/libs/wire-api-federation/wire-api-federation.cabal +++ b/libs/wire-api-federation/wire-api-federation.cabal @@ -4,7 +4,7 @@ cabal-version: 1.12 -- -- see: https://github.com/sol/hpack -- --- hash: 365781b146aeb5cff30955591ab1c8141b368aaa3b9a25b566331dfa677431b2 +-- hash: 942d9870717b6c174641f5f84d1344154f017118bd6870e939fba4eedc747d34 name: wire-api-federation version: 0.1.0 @@ -20,6 +20,8 @@ build-type: Simple library exposed-modules: + Wire.API.Federation.API + Wire.API.Federation.API.Conversation Wire.API.Federation.Event Wire.API.Federation.Util.Aeson other-modules: @@ -49,7 +51,7 @@ test-suite spec type: exitcode-stdio-1.0 main-is: Spec.hs other-modules: - Test.SerializationSpec + Test.Wire.API.Federation.APISpec Paths_wire_api_federation hs-source-dirs: test @@ -67,6 +69,7 @@ test-suite spec , hspec , hspec-discover , imports + , metrics-wai , servant >=0.16 , text >=0.11 , time >=1.8