Skip to content

Commit

Permalink
basic example for twirp-ezcurl
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Oct 20, 2023
1 parent 8fe33d7 commit 1791cf1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/twirp_ezcurl/README.md
Original file line number Diff line number Diff line change
@@ -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
```
29 changes: 29 additions & 0 deletions examples/twirp_ezcurl/calculator.proto
Original file line number Diff line number Diff line change
@@ -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);
}

20 changes: 20 additions & 0 deletions examples/twirp_ezcurl/calculator_client.ml
Original file line number Diff line number Diff line change
@@ -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;
()
11 changes: 11 additions & 0 deletions examples/twirp_ezcurl/dune
Original file line number Diff line number Diff line change
@@ -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})))

0 comments on commit 1791cf1

Please sign in to comment.