From 1791cf14704f7f9b869f8fa56eec40196ed36e2a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 12 Sep 2023 00:28:39 -0400 Subject: [PATCH] basic example for twirp-ezcurl --- examples/twirp_ezcurl/README.md | 8 ++++++ examples/twirp_ezcurl/calculator.proto | 29 ++++++++++++++++++++++ examples/twirp_ezcurl/calculator_client.ml | 20 +++++++++++++++ examples/twirp_ezcurl/dune | 11 ++++++++ 4 files changed, 68 insertions(+) create mode 100644 examples/twirp_ezcurl/README.md create mode 100644 examples/twirp_ezcurl/calculator.proto create mode 100644 examples/twirp_ezcurl/calculator_client.ml create mode 100644 examples/twirp_ezcurl/dune diff --git a/examples/twirp_ezcurl/README.md b/examples/twirp_ezcurl/README.md new file mode 100644 index 00000000..b2457e91 --- /dev/null +++ b/examples/twirp_ezcurl/README.md @@ -0,0 +1,8 @@ + +While running the server on port 8084: + +```sh +$ PORT=8084 dune exec examples/twirp_ezcurl/calculator_client.exe +query on http://localhost:8084/ +add call: returned 131 +``` diff --git a/examples/twirp_ezcurl/calculator.proto b/examples/twirp_ezcurl/calculator.proto new file mode 100644 index 00000000..3082757f --- /dev/null +++ b/examples/twirp_ezcurl/calculator.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +message DivByZero {} + +message I32 { + int32 value = 0; +} + +message AddReq { + int32 a = 1; + int32 b = 2; +} + +message AddAllReq { + repeated int32 ints = 1; +} + +message Empty {} + +service Calculator { + rpc add(AddReq) returns (I32); + + rpc add_all(AddAllReq) returns (I32); + + rpc ping(Empty) returns (Empty); + + rpc get_pings(Empty) returns (I32); +} + diff --git a/examples/twirp_ezcurl/calculator_client.ml b/examples/twirp_ezcurl/calculator_client.ml new file mode 100644 index 00000000..bfd45647 --- /dev/null +++ b/examples/twirp_ezcurl/calculator_client.ml @@ -0,0 +1,20 @@ +let spf = Printf.sprintf +let aspf = Format.asprintf + +let () = + let port = try int_of_string (Sys.getenv "PORT") with _ -> 8080 in + Printf.printf "query on http://localhost:%d/\n%!" port; + + let r = + match + Twirp_ezcurl.call ~use_tls:false ~host:"localhost" ~port + Calculator.Calculator.add + @@ Calculator.default_add_req ~a:31l ~b:100l () + with + | Ok x -> x.value |> Int32.to_int + | Error err -> + failwith (aspf "call to add failed: %a" Twirp_ezcurl.pp_error err) + in + + Printf.printf "add call: returned %d\n%!" r; + () diff --git a/examples/twirp_ezcurl/dune b/examples/twirp_ezcurl/dune new file mode 100644 index 00000000..c7872a84 --- /dev/null +++ b/examples/twirp_ezcurl/dune @@ -0,0 +1,11 @@ + +(executable + (name calculator_client) + (flags :standard -w -40) + (libraries ezcurl twirp_ezcurl)) + +(rule + (targets calculator.ml calculator.mli) + (deps calculator.proto) + (action + (run ocaml-protoc --binary --pp --yojson --services --ml_out ./ %{deps})))