dryvPacks

@dryv/go-client-sdkv0.1.0

A standard-library Go HTTP client SDK with typed models, enums and operations.

Go Client SDKClientsUnitPublished 7 days ago2 downloads

dryv pack add @dryv/go-client-sdk
3.8 KBRaw ↗
jinja
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies %}{% if dependency.target.id == link.id %}{{ dependency.symbol }}{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro go_type(value) -%}
{%- if value is none -%}any
{%- elif value.is.schema -%}{{ symbol_for(value.schema) or 'any' }}
{%- elif value.is.enum -%}{{ symbol_for(value.property.enum) or 'any' }}
{%- elif value.is.boolean -%}bool
{%- elif value.is.integer -%}int64
{%- elif value.is.number -%}float64
{%- elif value.is.string or value.is.temporal -%}string
{%- elif value.is.record -%}map[string]any
{%- else -%}any
{%- endif -%}
{%- endmacro -%}
{%- macro input_field_type(field) -%}
{%- if field.optional %}*{% endif -%}
{{- go_type(field.value) -}}
{%- endmacro -%}
{%- macro bound(binding) -%}
{%- if binding.port.field is none -%}input{%- else -%}input.{{ binding.port.field.pascal }}{%- endif -%}
{%- endmacro -%}
{%- set http = operation.facets.http -%}
// Code generated by Dryv. DO NOT EDIT.

package {{ pack.inputs.package_name }}

import "context"

{%- if operation.input and operation.input.kind != 'schema' %}

// {{ file.symbol }}Input contains the named input fields for {{ operation.name.title }}.
type {{ file.symbol }}Input struct {
{%- for field in operation.input.fields %}
	{{ field.name.pascal }} {{ input_field_type(field) }} `json:"{{ field.name.raw }}{% if field.optional %},omitempty{% endif %}"`
{%- endfor %}
}
{%- endif %}

// {{ file.symbol }} calls {{ http.method }} {{ http.path.local }}.
func {{ file.symbol }}(
	ctx context.Context,
	client *Client
{%- if operation.input %},
	input {% if operation.input.kind == 'schema' %}{{ go_type(operation.input.value) }}{% else %}{{ file.symbol }}Input{% endif %}
{%- endif %},
) {% if operation.output %}({{ go_type(operation.output.value) }}, error){% else %}error{% endif %} {
{%- if operation.output %}
	var zero {{ go_type(operation.output.value) }}
{%- endif %}
	path := "{{ http.path.local }}"
{%- for binding in http.input_groups.path %}
	path = replacePath(path, "{{ binding.name }}", {{ bound(binding) }})
{%- endfor %}

	var err error
{%- for binding in http.input_groups.query_object %}
	path, err = appendQueryObject(path, {{ bound(binding) }})
	if err != nil {
{%- if operation.output %}
		return zero, err
{%- else %}
		return err
{%- endif %}
	}
{%- endfor %}
{%- if http.input_groups.query %}
	path = appendQuery(path, map[string]any{
{%- for binding in http.input_groups.query %}
		"{{ binding.name }}": {{ bound(binding) }},
{%- endfor %}
	})
{%- endif %}

	var body []byte
	contentType := ""
{%- for binding in http.input_groups.body %}
	body, err = jsonBody({{ bound(binding) }})
	if err != nil {
{%- if operation.output %}
		return zero, err
{%- else %}
		return err
{%- endif %}
	}
	contentType = "application/json"
{%- endfor %}
{%- if http.input_groups.form %}
	body = formBody(map[string]any{
{%- for binding in http.input_groups.form %}
		"{{ binding.name }}": {{ bound(binding) }},
{%- endfor %}
	})
	contentType = "application/x-www-form-urlencoded"
{%- endif %}
{%- if http.input_groups.multipart %}
{%- if operation.output %}
	return zero, unsupportedBinding("multipart")
{%- else %}
	return unsupportedBinding("multipart")
{%- endif %}
{%- endif %}

	headers := map[string]any{
{%- for binding in http.input_groups.header %}
		"{{ binding.name }}": {{ bound(binding) }},
{%- endfor %}
	}
	cookies := map[string]any{
{%- for binding in http.input_groups.cookie %}
		"{{ binding.name }}": {{ bound(binding) }},
{%- endfor %}
	}

	req, err := client.newRequest(ctx, "{{ http.method }}", path, body, contentType, headers, cookies)
	if err != nil {
{%- if operation.output %}
		return zero, err
{%- else %}
		return err
{%- endif %}
	}

{%- if operation.output %}
	var output {{ go_type(operation.output.value) }}
	if err := client.do(req, &output); err != nil {
		return zero, err
	}
	return output, nil
{%- else %}
	return client.do(req, nil)
{%- endif %}
}