@dryv/ocaml-client-sdkv0.1.0
Typed OCaml Cohttp/Lwt client with Yojson wire models and generated operations.
dryv pack add @dryv/ocaml-client-sdk{%- macro module_name(symbol) -%}{{ symbol[0] | upper }}{{ symbol[1:] }}{%- endmacro -%}
{%- macro dep_module(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.symbol and not dependency.is_self -%}{{ module_name(dependency.symbol) }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro base_type(value) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal or value.is.record -%}Yojson.Safe.t
{%- elif value.is.schema -%}{{ dep_module(value.schema) or 'Yojson.Safe.t' }}.t
{%- elif value.is.enum -%}{{ dep_module(value.property.enum) or 'Yojson.Safe.t' }}.t
{%- elif value.is.boolean -%}bool
{%- elif value.is.integer -%}int
{%- elif value.is.number -%}float
{%- elif value.is.decimal or value.is.money -%}string
{%- elif value.is.string or value.is.temporal or value.is.uuid -%}string
{%- else -%}Yojson.Safe.t
{%- endif -%}
{%- endmacro -%}
{%- macro encode(value, expression) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal or value.is.record -%}{{ expression }}
{%- elif value.is.schema -%}{{ dep_module(value.schema) }}.to_yojson {{ expression }}
{%- elif value.is.enum -%}{{ dep_module(value.property.enum) }}.to_yojson {{ expression }}
{%- elif value.is.boolean -%}`Bool {{ expression }}
{%- elif value.is.integer -%}`Int {{ expression }}
{%- elif value.is.number -%}`Float {{ expression }}
{%- elif value.is.decimal or value.is.money -%}`String {{ expression }}
{%- elif value.is.string or value.is.temporal or value.is.uuid -%}`String {{ expression }}
{%- else -%}{{ expression }}
{%- endif -%}
{%- endmacro -%}
{%- macro decode(value, expression) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal or value.is.record -%}Ok {{ expression }}
{%- elif value.is.schema -%}{{ dep_module(value.schema) }}.of_yojson {{ expression }}
{%- elif value.is.enum -%}{{ dep_module(value.property.enum) }}.of_yojson {{ expression }}
{%- elif value.is.boolean -%}(match {{ expression }} with `Bool value -> Ok value | _ -> Error "expected boolean")
{%- elif value.is.integer -%}(match {{ expression }} with `Int value -> Ok value | _ -> Error "expected integer")
{%- elif value.is.number -%}(match {{ expression }} with `Float value -> Ok value | `Int value -> Ok (float_of_int value) | _ -> Error "expected number")
{%- elif value.is.decimal or value.is.money -%}(match {{ expression }} with `String value -> Ok value | _ -> Error "expected decimal string")
{%- elif value.is.string or value.is.temporal or value.is.uuid -%}(match {{ expression }} with `String value -> Ok value | _ -> Error "expected string")
{%- else -%}Ok {{ expression }}
{%- endif -%}
{%- endmacro -%}
{%- macro field_type(field) -%}
{{- base_type(field.value) -}}{%- if field.optional -%} option{%- endif -%}
{%- endmacro -%}
{%- macro bound(binding) -%}
{%- if binding.port.field is none -%}input{%- else -%}input.{{ binding.port.field.snake }}{%- endif -%}
{%- endmacro -%}
{%- macro binding_optional(operation, binding) -%}
{%- if binding.port.field is not none -%}
{%- for field in operation.input.fields -%}
{%- if field.name.raw == binding.port.field.raw and field.optional -%}x{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- macro append_pair(target, binding, name) -%}
{%- if binding_optional(operation, binding) %}
let {{ target }} =
match {{ bound(binding) }} with
| None -> {{ target }}
| Some value ->
Dryv_client.append_wire {{ target }} {{ name | tojson }} ({{ encode(binding.port.value, 'value') }})
in
{%- else %}
let {{ target }} =
Dryv_client.append_wire {{ target }} {{ name | tojson }} ({{ encode(binding.port.value, bound(binding)) }})
in
{%- endif -%}
{%- endmacro -%}
{%- set http = operation.facets.http -%}
{%- if operation.input and operation.input.kind != 'schema' %}
type input =
{
{%- for field in operation.input.fields %}
{{ field.name.snake }} : {{ field_type(field) }}{% if not loop.last %};{% endif %}
{%- endfor %}
}
let input_to_yojson input =
`Assoc
[
{%- for field in operation.input.fields %}
{%- if field.optional %}
( {{ field.name.raw | tojson }}
, match input.{{ field.name.snake }} with
| None -> `Null
| Some value -> {{ encode(field.value, 'value') }} ){% if not loop.last %};{% endif %}
{%- else %}
({{ field.name.raw | tojson }}, {{ encode(field.value, 'input.' ~ field.name.snake) }}){% if not loop.last %};{% endif %}
{%- endif %}
{%- endfor %}
]
{%- endif %}
let execute
client
{%- if operation.input %}
(input : {% if operation.input.kind == 'schema' %}{{ base_type(operation.input.value) }}{% else %}input{% endif %})
{%- endif %}
=
{%- if http.input_groups.multipart %}
Lwt.fail
(Dryv_client.Api_error
{
status = 501;
message = "multipart bindings require an explicit OCaml semantic mapping";
payload = None;
})
{%- else %}
let path = {{ http.path.local | tojson }} in
{%- for binding in http.input_groups.path %}
{%- if binding_optional(operation, binding) %}
let path_value =
match {{ bound(binding) }} with
| Some value -> value
| None ->
raise
(Dryv_client.Api_error
{
status = 400;
message = "missing path parameter {{ binding.name or binding.port.name.raw }}";
payload = None;
})
in
let path =
Dryv_client.replace_path
path
{{ ('{' ~ (binding.name or binding.port.name.raw) ~ '}') | tojson }}
(Dryv_client.path_encode ({{ encode(binding.port.value, 'path_value') }}))
in
{%- else %}
let path =
Dryv_client.replace_path
path
{{ ('{' ~ (binding.name or binding.port.name.raw) ~ '}') | tojson }}
(Dryv_client.path_encode ({{ encode(binding.port.value, bound(binding)) }}))
in
{%- endif %}
{%- endfor %}
let query = [] in
{%- for binding in http.input_groups.query %}
{{ append_pair('query', binding, binding.name or binding.port.name.raw) }}
{%- endfor %}
{%- for binding in http.input_groups.query_object %}
let query =
Dryv_client.append_object
query
({% if binding_optional(operation, binding) %}match {{ bound(binding) }} with None -> `Null | Some value -> {{ encode(binding.port.value, 'value') }}{% else %}{{ encode(binding.port.value, bound(binding)) }}{% endif %})
in
{%- endfor %}
let headers = [] in
{%- for binding in http.input_groups.header %}
{{ append_pair('headers', binding, binding.name or binding.port.name.raw) }}
{%- endfor %}
let cookies = [] in
{%- for binding in http.input_groups.cookie %}
{{ append_pair('cookies', binding, binding.name or binding.port.name.raw) }}
{%- endfor %}
{%- if http.input_groups.body %}
{%- set binding = http.input_groups.body[0] %}
let json_body =
{% if binding_optional(operation, binding) %}(match {{ bound(binding) }} with None -> None | Some value -> Some ({{ encode(binding.port.value, 'value') }})){% else %}Some ({{ encode(binding.port.value, bound(binding)) }}){% endif %}
in
{%- else %}
let json_body = None in
{%- endif %}
{%- if http.input_groups.form %}
let form = [] in
{%- for binding in http.input_groups.form %}
{%- if binding.port.field is none %}
let form =
Dryv_client.append_object
form
({% if binding_optional(operation, binding) %}match {{ bound(binding) }} with None -> `Null | Some value -> {{ encode(binding.port.value, 'value') }}{% else %}{% if operation.input and operation.input.kind != 'schema' %}input_to_yojson input{% else %}{{ encode(binding.port.value, bound(binding)) }}{% endif %}{% endif %})
in
{%- else %}
{{ append_pair('form', binding, binding.name or binding.port.name.raw) }}
{%- endif %}
{%- endfor %}
let form = Some form in
{%- else %}
let form = None in
{%- endif %}
let meth = Cohttp.Code.method_of_string {{ http.method | tojson }} in
{%- if operation.output %}
Dryv_client.send_json
client
~meth
~path
~query
~headers
~cookies
~json_body
~form
~decode:(fun value -> {{ decode(operation.output.value, 'value') }})
{%- else %}
Dryv_client.send_empty
client
~meth
~path
~query
~headers
~cookies
~json_body
~form
{%- endif %}
{%- endif %}