client.eex 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. %%%-------------------------------------------------------------------
  2. %% @doc Client module for grpc service <%= unmodified_service_name %>.
  3. %% @end
  4. %%%-------------------------------------------------------------------
  5. %% this module was generated and should not be modified manually
  6. -module(<%= module_name %>_client).
  7. -compile(export_all).
  8. -compile(nowarn_export_all).
  9. -include_lib("grpc/include/grpc.hrl").
  10. -define(SERVICE, '<%= unmodified_service_name %>').
  11. -define(PROTO_MODULE, '<%= pb_module %>').
  12. -define(MARSHAL(T), fun(I) -> ?PROTO_MODULE:encode_msg(I, T) end).
  13. -define(UNMARSHAL(T), fun(I) -> ?PROTO_MODULE:decode_msg(I, T) end).
  14. -define(DEF(Path, Req, Resp, MessageType),
  15. #{path => Path,
  16. service =>?SERVICE,
  17. message_type => MessageType,
  18. marshal => ?MARSHAL(Req),
  19. unmarshal => ?UNMARSHAL(Resp)}).
  20. <%= for method <- methods do %>
  21. <%!-- IF1 --%>
  22. <%= if (not method.input_stream) and (not method.output_stream) do %>
  23. -spec <%= method.snake_case %>(<%= method.pb_module %>:<%= method.input %>())
  24. -> {ok, <%= method.pb_module %>:<%= method.output %>(), grpc:metadata()}
  25. | {error, term()}.
  26. <%= method.snake_case %>(Req) ->
  27. <%= method.snake_case %>(Req, #{}, #{}).
  28. -spec <%= method.snake_case %>(<%= method.pb_module %>:<%= method.input %>(), grpc:options())
  29. -> {ok, <%= method.pb_module %>:<%= method.output %>(), grpc:metadata()}
  30. | {error, term()}.
  31. <%= method.snake_case %>(Req, Options) ->
  32. <%= method.snake_case %>(Req, #{}, Options).
  33. -spec <%= method.snake_case %>(<%= method.pb_module %>:<%= method.input %>(), grpc:metadata(), grpc_client:options())
  34. -> {ok, <%= method.pb_module %>:<%= method.output %>(), grpc:metadata()}
  35. | {error, term()}.
  36. <%= method.snake_case %>(Req, Metadata, Options) ->
  37. grpc_client:unary(?DEF(<<"/<%= unmodified_service_name %>/<%= method.unmodified_method %>">>,
  38. <%= method.input %>, <%= method.output %>, <<"<%= method.message_type %>">>),
  39. Req, Metadata, Options).
  40. <%!-- END IF1 --%>
  41. <% end %>
  42. <%!-- IF2 --%>
  43. <%= if (not method.input_stream) and method.output_stream do %>
  44. -spec <%= method.snake_case %>(grpc_client:options())
  45. -> {ok, grpc_client:grpcstream()}
  46. | {error, term()}.
  47. <%= method.snake_case %>(Options) ->
  48. <%= method.snake_case %>(#{}, Options).
  49. -spec <%= method.snake_case %>(grpc:metadata(), grpc_client:options())
  50. -> {ok, grpc_client:grpcstream()}
  51. | {error, term()}.
  52. <%= method.snake_case %>(Metadata, Options) ->
  53. grpc_client:open(?DEF(<<"/<%= unmodified_service_name %>/<%= method.unmodified_method %>">>,
  54. <%= method.input %>, <%= method.output %>, <<"<%= method.message_type %>">>),
  55. Metadata, Options).
  56. <%!-- END IF2 --%>
  57. <% end %>
  58. <%!-- IF3 --%>
  59. <%= if method.input_stream do %>
  60. -spec <%= method.snake_case %>(grpc_client:options())
  61. -> {ok, grpc_client:grpcstream()}
  62. | {error, term()}.
  63. <%= method.snake_case %>(Options) ->
  64. <%= method.snake_case %>(#{}, Options).
  65. -spec <%= method.snake_case %>(grpc:metadata(), grpc_client:options())
  66. -> {ok, grpc_client:grpcstream()}
  67. | {error, term()}.
  68. <%= method.snake_case %>(Metadata, Options) ->
  69. grpc_client:open(?DEF(<<"/<%= unmodified_service_name %>/<%= method.unmodified_method %>">>,
  70. <%= method.input %>, <%= method.output %>, <<"<%= method.message_type %>">>),
  71. Metadata, Options).
  72. <% end %>
  73. <%!-- END IF3 --%>
  74. <% end %>
  75. <%!-- END for method <- methods --%>