Skip to content

Commit

Permalink
Merge pull request #194 from well-typed/edsko/cleanup-api
Browse files Browse the repository at this point in the history
Cleanup API modules
  • Loading branch information
edsko authored Jul 18, 2024
2 parents b3108cb + 7029f12 commit 249c6d0
Show file tree
Hide file tree
Showing 48 changed files with 223 additions and 157 deletions.
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/Core/Greeter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import Network.GRPC.Common.Protobuf

import Demo.Client.Util.DelayOr (DelayOr)
import Demo.Client.Util.DelayOr qualified as DelayOr
import Demo.Common.API
import Demo.Common.Logging
import Demo.Client.Util.Logging

import Proto.API.Helloworld

{-------------------------------------------------------------------------------
helloworld.Greeter
Expand Down
2 changes: 1 addition & 1 deletion demo-client/Demo/Client/API/Core/NoFinal/Greeter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf

import Demo.Common.API
import Proto.API.Helloworld

{-------------------------------------------------------------------------------
helloworld.Greeter
Expand Down
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/Core/RouteGuide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf

import Demo.Common.API
import Demo.Common.Logging
import Demo.Client.Util.Logging

import Proto.API.RouteGuide

{-------------------------------------------------------------------------------
RouteGuide
Expand Down
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/StreamType/Conduit/RouteGuide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import Network.GRPC.Common.Protobuf

import Demo.Client.Util.DelayOr (DelayOr)
import Demo.Client.Util.DelayOr qualified as DelayOr
import Demo.Common.API
import Demo.Common.Logging
import Demo.Client.Util.Logging

import Proto.API.RouteGuide

{-------------------------------------------------------------------------------
routeguide.RouteGuide
Expand Down
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/StreamType/IO/Greeter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import Network.GRPC.Client.StreamType.IO
import Network.GRPC.Common.NextElem qualified as NextElem
import Network.GRPC.Common.Protobuf

import Demo.Common.API
import Demo.Common.Logging
import Demo.Client.Util.Logging

import Proto.API.Helloworld

{-------------------------------------------------------------------------------
helloworld.Greeter
Expand Down
9 changes: 4 additions & 5 deletions demo-client/Demo/Client/API/StreamType/IO/Ping.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module Demo.Client.API.StreamType.IO.Ping (ping) where

import Data.ByteString.Lazy.Char8 qualified as Lazy

import Network.GRPC.Client
import Network.GRPC.Client.StreamType.IO
import Network.GRPC.Common.Protobuf

import Demo.Common.API
import Proto.API.Ping

ping :: Connection -> Lazy.ByteString -> IO ()
ping :: Connection -> Proto PingMessage -> IO ()
ping conn msg = do
pong <- nonStreaming conn (rpc @Ping) msg
Lazy.putStrLn pong
print pong
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/StreamType/IO/RouteGuide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import Network.GRPC.Common.Protobuf

import Demo.Client.Util.DelayOr (DelayOr)
import Demo.Client.Util.DelayOr qualified as DelayOr
import Demo.Common.API
import Demo.Common.Logging
import Demo.Client.Util.Logging

import Proto.API.RouteGuide

{-------------------------------------------------------------------------------
routeguide.RouteGuide
Expand Down
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/API/StreamType/MonadStack/Greeter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import Network.GRPC.Client.StreamType.CanCallRPC
import Network.GRPC.Common.NextElem qualified as NextElem
import Network.GRPC.Common.Protobuf

import Demo.Common.API
import Demo.Common.Logging qualified as Logging
import Demo.Client.Util.Logging qualified as Logging

import Proto.API.Helloworld

{-------------------------------------------------------------------------------
Example custom monad
Expand Down
20 changes: 16 additions & 4 deletions demo-client/Demo/Client/Cmdline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Demo.Client.Cmdline (

import Prelude

import Data.ByteString.Lazy qualified as Lazy (ByteString)
import Data.Foldable (asum)
import Data.Int
import Data.Kind
Expand All @@ -35,7 +34,11 @@ import Network.GRPC.Common.Protobuf
import Paths_grapesy

import Demo.Client.Util.DelayOr (DelayOr(..))
import Demo.Common.API

import Proto.API.Helloworld
import Proto.API.Ping
import Proto.API.RouteGuide
import Text.Read (readMaybe)

{-------------------------------------------------------------------------------
Definition
Expand Down Expand Up @@ -75,7 +78,7 @@ data SMethod :: Type -> Type where
SRecordRoute :: [DelayOr (Proto Point)] -> SMethod RecordRoute
SRouteChat :: [DelayOr (Proto RouteNote)] -> SMethod RouteChat

SPing :: Lazy.ByteString -> SMethod Ping
SPing :: Proto PingMessage -> SMethod Ping

deriving stock instance Show (SMethod rpc)

Expand Down Expand Up @@ -249,7 +252,7 @@ parseSomeMethod = Opt.subparser $ mconcat [
Opt.many (parseDelayOr $ parseRouteNote)
, sub "ping" "Ping.ping" $
SomeMethod . SPing <$>
Opt.argument Opt.str (Opt.metavar "MSG")
Opt.argument parsePingMessage (Opt.metavar "NUM")
]

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -324,6 +327,15 @@ parseRouteNote =
& #location .~ location
& #message .~ message

parsePingMessage :: Opt.ReadM (Proto PingMessage)
parsePingMessage = Opt.str >>= aux
where
aux :: String -> Opt.ReadM (Proto PingMessage)
aux str =
case readMaybe str of
Nothing -> fail $ "Could not parse ping ID " ++ show str
Just pid -> return $ defMessage & #id .~ pid

{-------------------------------------------------------------------------------
Internal auxiliary
-------------------------------------------------------------------------------}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Demo.Common.Logging (
module Demo.Client.Util.Logging (
threadSafeTracer
, logMsg
) where
Expand Down
2 changes: 1 addition & 1 deletion demo-server/Demo/Server/Service/Greeter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Network.GRPC.Server
import Network.GRPC.Server.Protobuf
import Network.GRPC.Server.StreamType

import Demo.Common.API
import Proto.API.Helloworld

{-------------------------------------------------------------------------------
Top-level
Expand Down
11 changes: 6 additions & 5 deletions demo-server/Demo/Server/Service/Ping.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module Demo.Server.Service.Ping (handlers) where
{-# LANGUAGE OverloadedLabels #-}

import Data.ByteString.Lazy qualified as Lazy (ByteString)
module Demo.Server.Service.Ping (handlers) where

import Network.GRPC.Common.Protobuf
import Network.GRPC.Server.StreamType

import Demo.Common.API
import Proto.API.Ping

handlers :: Methods IO '[Ping]
handlers =
Method (mkNonStreaming handlePing)
$ NoMoreMethods

handlePing :: Lazy.ByteString -> IO Lazy.ByteString
handlePing = return
handlePing :: Proto PingMessage -> IO (Proto PongMessage)
handlePing ping = return $ defMessage & #id .~ (ping ^. #id)

5 changes: 3 additions & 2 deletions demo-server/Demo/Server/Service/RouteGuide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Demo.Server.Service.RouteGuide (
) where

import Control.Monad.IO.Class
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State (StateT, evalStateT)
import Control.Monad.Trans.State qualified as State
import Data.Map qualified as Map
Expand All @@ -21,10 +22,10 @@ import Network.GRPC.Server
import Network.GRPC.Server.Protobuf
import Network.GRPC.Server.StreamType

import Demo.Common.API
import Demo.Server.Aux.RouteGuide
import Demo.Server.Cmdline
import Control.Monad.Trans.Reader

import Proto.API.RouteGuide

{-------------------------------------------------------------------------------
Custom handler monad
Expand Down
8 changes: 5 additions & 3 deletions demo-server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import Network.GRPC.Server.Protobuf
import Network.GRPC.Server.Run
import Network.GRPC.Server.StreamType

import Paths_grapesy

import Demo.Server.Cmdline
import Demo.Server.Service.Greeter qualified as Greeter
import Demo.Server.Service.Ping qualified as Ping
import Demo.Server.Service.RouteGuide qualified as RouteGuide

import Demo.Common.API

import Paths_grapesy
import Proto.API.Helloworld
import Proto.API.Ping
import Proto.API.RouteGuide

{-------------------------------------------------------------------------------
All services
Expand Down
31 changes: 15 additions & 16 deletions grapesy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ library
Paths_grapesy

Proto.OrcaLoadReport
Proto.OrcaLoadReport_Fields

Control.Monad.XIO
autogen-modules:
Expand Down Expand Up @@ -316,8 +315,11 @@ test-suite test-grapesy

Paths_grapesy

Proto.API.Interop
Proto.API.Ping
Proto.Empty
Proto.Messages
Proto.Ping
Proto.Test
autogen-modules:
Paths_grapesy
Expand Down Expand Up @@ -359,7 +361,6 @@ executable demo-client
, common-executable-flags
hs-source-dirs:
demo-client
demo-common
proto
main-is:
Main.hs
Expand All @@ -374,16 +375,16 @@ executable demo-client
Demo.Client.API.StreamType.MonadStack.Greeter
Demo.Client.Cmdline
Demo.Client.Util.DelayOr

Demo.Common.API
Demo.Common.Logging
Demo.Client.Util.Logging

Paths_grapesy

Proto.API.Helloworld
Proto.API.Ping
Proto.API.RouteGuide
Proto.Helloworld
Proto.Helloworld_Fields
Proto.Ping
Proto.RouteGuide
Proto.RouteGuide_Fields
autogen-modules:
Paths_grapesy
build-depends:
Expand Down Expand Up @@ -412,7 +413,6 @@ executable demo-server
, common-executable-flags
hs-source-dirs:
demo-server
demo-common
proto
main-is:
Main.hs
Expand All @@ -423,13 +423,12 @@ executable demo-server
Demo.Server.Service.Ping
Demo.Server.Service.RouteGuide

Demo.Common.API
Demo.Common.Logging

Proto.API.Helloworld
Proto.API.Ping
Proto.API.RouteGuide
Proto.Helloworld
Proto.Helloworld_Fields
Proto.Ping
Proto.RouteGuide
Proto.RouteGuide_Fields

Paths_grapesy
autogen-modules:
Expand All @@ -442,7 +441,6 @@ executable demo-server
, aeson >= 1.5 && < 2.3
, bytestring >= 0.10 && < 0.13
, containers >= 0.6 && < 0.8
, contra-tracer >= 0.2 && < 0.3
, exceptions >= 0.10 && < 0.11
, network >= 3.1 && < 3.3
, optparse-applicative >= 0.16 && < 0.19
Expand Down Expand Up @@ -493,7 +491,6 @@ test-suite grapesy-interop
main-is:
Main.hs
other-modules:
Interop.API
Interop.Client
Interop.Client.Common
Interop.Client.Connect
Expand Down Expand Up @@ -532,9 +529,11 @@ test-suite grapesy-interop

Paths_grapesy

Proto.Ping
Proto.API.Interop
Proto.API.Ping
Proto.Empty
Proto.Messages
Proto.Ping
Proto.Test
autogen-modules:
Paths_grapesy
Expand Down
4 changes: 3 additions & 1 deletion interop/Interop/Client/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import Data.ByteString qualified as BS.Strict
import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Compression qualified as Compr
import Network.GRPC.Common.Protobuf
import Network.GRPC.Spec

import Interop.API
import Interop.Util.Exceptions
import Interop.Util.Messages

import Proto.API.Interop

{-------------------------------------------------------------------------------
Config
-------------------------------------------------------------------------------}
Expand Down
5 changes: 2 additions & 3 deletions interop/Interop/Client/Ping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Network.GRPC.Client.StreamType.IO
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf

import Proto.Ping

import Interop.API
import Interop.Client.Connect
import Interop.Cmdline

import Proto.API.Ping

ping :: Cmdline -> IO ()
ping cmdline =
withConnection def (testServer cmdline) $ \conn ->
Expand Down
3 changes: 2 additions & 1 deletion interop/Interop/Client/TestCase/CancelAfterBegin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module Interop.Client.TestCase.CancelAfterBegin (runTest) where
import Network.GRPC.Client
import Network.GRPC.Common

import Interop.API
import Interop.Client.Connect
import Interop.Cmdline
import Interop.Util.Exceptions

import Proto.API.Interop

-- | <https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md#cancel_after_begin>
--
-- This is not really testing anything about the server, but rather about how
Expand Down
4 changes: 3 additions & 1 deletion interop/Interop/Client/TestCase/CancelAfterFirstResponse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module Interop.Client.TestCase.CancelAfterFirstResponse (runTest) where

import Network.GRPC.Client
import Network.GRPC.Common
import Network.GRPC.Common.Protobuf

import Interop.API
import Interop.Client.Connect
import Interop.Cmdline
import Interop.Util.Exceptions
import Interop.Client.Common (mkStreamingOutputCallRequest)

import Proto.API.Interop

-- | <https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md#cancel_after_first_response>
runTest :: Cmdline -> IO ()
runTest cmdline =
Expand Down
Loading

0 comments on commit 249c6d0

Please sign in to comment.