@dryv/csharp-client-sdkv0.1.0
Typed .NET 10 HttpClient SDK using System.Text.Json.
dryv pack add @dryv/csharp-client-sdk{%- macro cs_ident(name) -%}
{%- if name in ['abstract','as','base','bool','break','byte','case','catch','char','checked','class','const','continue','decimal','default','delegate','do','double','else','enum','event','explicit','extern','false','finally','fixed','float','for','foreach','goto','if','implicit','in','int','interface','internal','is','lock','long','namespace','new','null','object','operator','out','override','params','private','protected','public','readonly','ref','return','sbyte','sealed','short','sizeof','stackalloc','static','string','struct','switch','this','throw','true','try','typeof','uint','ulong','unchecked','unsafe','ushort','using','virtual','void','volatile','while','record','required','file','scoped'] -%}@{{ name }}{%- else -%}{{ name }}{%- endif -%}
{%- endmacro -%}
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro cs_type(value) -%}
{%- if value is none -%}JsonElement
{%- elif value.is.schema -%}{{ symbol_for(value.schema) or 'JsonElement' }}
{%- elif value.is.enum -%}{{ symbol_for(value.property.enum) or 'JsonElement' }}
{%- elif value.is.boolean -%}bool
{%- elif value.is.integer -%}long
{%- elif value.is.decimal or value.is.money -%}decimal
{%- elif value.is.number -%}double
{%- elif value.is.temporal and value.is.date -%}DateOnly
{%- elif value.is.temporal and value.is.time -%}TimeOnly
{%- elif value.is.temporal and value.is.date_time -%}DateTime
{%- elif value.is.temporal and (value.is.instant or value.is.zoned_date_time) -%}DateTimeOffset
{%- elif value.is.temporal and value.is.duration -%}TimeSpan
{%- elif value.is.record -%}Dictionary<string, JsonElement>
{%- elif value.is.string -%}string
{%- else -%}JsonElement
{%- endif -%}
{%- endmacro -%}
{%- macro input_type(field) -%}
{%- if field.array is not none -%}List<{{ cs_type(field.value) }}>{%- else -%}{{ cs_type(field.value) }}{%- endif -%}{%- if field.optional or field.nullable -%}?{%- endif -%}
{%- endmacro -%}
{%- macro bound(binding) -%}
{%- if binding.port.field is none -%}input{%- else -%}input.{{ cs_ident(binding.port.field.pascal) }}{%- endif -%}
{%- endmacro -%}
{%- set http = operation.facets.http -%}
using System.Net.Http.Headers;
using System.Text.Json;
using {{ pack.inputs.root_namespace }}.Models;
namespace {{ pack.inputs.root_namespace }}.Operations;
public static class {{ file.symbol }}
{
{%- if operation.input and operation.input.kind != 'schema' %}
public sealed class Input
{
{%- for field in operation.input.fields %}
public {{ input_type(field) }} {{ cs_ident(field.name.pascal) }} { get; set; }
{%- endfor %}
}
{%- endif %}
public static async Task{% if operation.output %}<{{ cs_type(operation.output.value) }}>{% endif %} ExecuteAsync(
DryvClient client{% if operation.input %},
{% if operation.input.kind == 'schema' %}{{ cs_type(operation.input.value) }}{% else %}Input{% endif %} input{% endif %},
CancellationToken cancellationToken = default)
{
{%- if http.input_groups.multipart %}
throw new NotSupportedException("Multipart bindings require an explicit C# semantic mapping.");
{%- else %}
var path = "{{ http.path.local }}";
{%- for binding in http.input_groups.path %}
path = DryvClient.ReplacePath(path, "{{ binding.name or binding.port.name.raw }}", {{ bound(binding) }}!);
{%- endfor %}
var query = new List<KeyValuePair<string, object?>>();
{%- for binding in http.input_groups.query %}
query.Add(new("{{ binding.name or binding.port.name.raw }}", {{ bound(binding) }}));
{%- endfor %}
{%- for binding in http.input_groups.query_object %}
foreach (var pair in client.ToDictionary({{ bound(binding) }}!))
{
query.Add(pair);
}
{%- endfor %}
path = DryvClient.AppendQuery(path, query);
using var request = client.Request(new HttpMethod("{{ http.method }}"), path);
{%- for binding in http.input_groups.header %}
if ({{ bound(binding) }} is not null)
{
request.Headers.TryAddWithoutValidation("{{ binding.name or binding.port.name.raw }}", Convert.ToString({{ bound(binding) }}, System.Globalization.CultureInfo.InvariantCulture));
}
{%- endfor %}
{%- if http.input_groups.cookie %}
var cookies = new List<string>();
{%- for binding in http.input_groups.cookie %}
if ({{ bound(binding) }} is not null)
{
cookies.Add("{{ binding.name or binding.port.name.raw }}=" + Uri.EscapeDataString(Convert.ToString({{ bound(binding) }}, System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty));
}
{%- endfor %}
if (cookies.Count > 0)
{
request.Headers.TryAddWithoutValidation("Cookie", string.Join("; ", cookies));
}
{%- endif %}
{%- for binding in http.input_groups.body %}
request.Content = client.JsonBody({{ bound(binding) }}!);
{%- endfor %}
{%- if http.input_groups.form %}
var form = new List<KeyValuePair<string, object?>>();
{%- for binding in http.input_groups.form %}
{%- if binding.port.field is none %}
form.AddRange(client.ToDictionary({{ bound(binding) }}!));
{%- else %}
form.Add(new("{{ binding.name or binding.port.name.raw }}", {{ bound(binding) }}));
{%- endif %}
{%- endfor %}
request.Content = DryvClient.Form(form);
{%- endif %}
{%- if operation.output %}
return await client.SendAsync<{{ cs_type(operation.output.value) }}>(request, cancellationToken).ConfigureAwait(false);
{%- else %}
await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
{%- endif %}
{%- endif %}
}
}