Files
simplex-chat/definitions/src/Simplex/Messaging/ServerAPI.hs
Evgeny Poberezkin cc55bf3e6b Different approach to commands (#34)
* different approach to command types (WIP)

* PartyProtocol class and other commands

* pretty-print scenarion

* remove old files

* remove unused prf/predicate templates

* remove NoImplicitePrelude from doctest (although there are no doctests atm)
2020-05-31 21:51:15 +01:00

156 lines
4.2 KiB
Haskell

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Simplex.Messaging.ServerAPI
( ServerAPI,
serverApiIntro,
serverApiExtra,
)
where
import Control.Lens
import Data.Function ()
import Servant
import Servant.Docs
import Simplex.Messaging.Types
type ServerAPI =
CreateConnection
:<|> SecureConnection
:<|> DeleteConnection
:<|> GetMessages
:<|> DeleteMessage
:<|> SendMessage
type CreateConnection =
"connection" :> ReqBody '[JSON] CreateConnRequest
:> PostCreated '[JSON] CreateConnResponse
type SecureConnection =
"connection" :> Capture "connectionId" Base64EncodedString
:> ReqBody '[JSON] SecureConnRequest
:> Put '[JSON] NoContent
type DeleteConnection =
"connection" :> Capture "connectionId" Base64EncodedString
:> Delete '[JSON] NoContent
type GetMessages =
"connection" :> Capture "connectionId" Base64EncodedString
:> "messages"
:> QueryParam "fromMessageId" (Maybe Base64EncodedString)
:> Get '[JSON] MessagesResponse
type DeleteMessage =
"connection" :> Capture "connectionId" Base64EncodedString
:> "messages"
:> Capture "messageId" Base64EncodedString
:> Delete '[JSON] NoContent
type SendMessage =
"connection" :> Capture "senderConnectionId" Base64EncodedString
:> "messages"
:> ReqBody '[JSON] SendMessageRequest
:> PostCreated '[JSON] NoContent
-- API docs
serverApiIntro :: DocIntro
serverApiIntro =
DocIntro
"Simplex messaging protocol REST API"
[ "This document lists all required REST endpoints of simplex messaging API.",
"Also see [Simplex messaging protocol implementation](simplex-messaging-implementation.md) for more details."
]
serverApiExtra :: ExtraInfo ServerAPI
serverApiExtra =
info
(Proxy :: Proxy CreateConnection)
"Create connection"
[]
<> info
(Proxy :: Proxy SecureConnection)
"Secure connection"
[]
<> info
(Proxy :: Proxy DeleteConnection)
"Delete connection"
[]
<> info
(Proxy :: Proxy GetMessages)
"Get messages"
[]
<> info
(Proxy :: Proxy DeleteMessage)
"Delete message"
[]
<> info
(Proxy :: Proxy SendMessage)
"Send message"
[]
where
info p title comments =
extraInfo p $ defAction & notes <>~ [DocNote title comments]
instance ToCapture (Capture "connectionId" String) where
toCapture _ =
DocCapture
"connectionId"
"Recipient connection ID - unique connection ID to be used by connection recipient"
instance ToCapture (Capture "senderConnectionId" String) where
toCapture _ =
DocCapture
"senderConnectionId"
"Sender connection ID - unique connection ID to be used by connection sender"
instance ToCapture (Capture "messageId" String) where
toCapture _ =
DocCapture
"messageId"
"Message ID - unique message ID to be used by connection recipient"
instance ToParam (QueryParam "fromMessageId" (Maybe Base64EncodedString)) where
toParam _ =
DocQueryParam
"fromMessageId"
["message ID, e.g., `p8PCiGPZ`"]
"if set, the server will respond with the messages received starting from the message with server message ID (unique per server) passed in this parameter."
Normal
instance ToSample CreateConnRequest where
toSamples _ = singleSample $ CreateConnRequest "BODbZxmtKUUF1l8pj4nVjQ"
instance ToSample CreateConnResponse where
toSamples _ = singleSample $ CreateConnResponse "Qxz93A" "N9pA3g"
instance ToSample SecureConnRequest where
toSamples _ = singleSample $ SecureConnRequest "XPaVEVNunkYKqqK0dnAT5Q"
dummyMessage :: Message
dummyMessage =
Message
{ msgId = "p8PCiGPZ",
ts = "2020-03-15T19:58:33.695Z",
msg = "OQLMXoEA4iv-aR46puPJuY1Rdoc1KY0gfq8oElJwtAs"
}
instance ToSample MessagesResponse where
toSamples _ =
singleSample $
MessagesResponse
{ messages = [dummyMessage],
nextMessageId = Nothing
}
instance ToSample SendMessageRequest where
toSamples _ =
singleSample $
SendMessageRequest
{ msg = "OQLMXoEA4iv-aR46puPJuY1Rdoc1KY0gfq8oElJwtAs"
}