forked from grafana/grafana-openapi-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientClient.gotmpl
158 lines (136 loc) · 6.92 KB
/
clientClient.gotmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Code generated by go-swagger; DO NOT EDIT.
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
package {{ .Name }}
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"fmt"
"io"
"net/http"
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
{{ imports .DefaultImports }}
{{ imports .Imports }}
)
// New creates a new {{ humanize .Name }} API client.
func New(transport runtime.ClientTransport, formats strfmt.Registry) ClientService {
return &Client{transport: transport, formats: formats}
}
/*
Client {{ if .Summary }}{{ .Summary }}{{ if .Description }}
{{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}for {{ humanize .Name }} API{{ end }}
*/
type Client struct {
transport runtime.ClientTransport
formats strfmt.Registry
}
// ClientOption is the option for Client methods
type ClientOption func(*runtime.ClientOperation)
// ClientService is the interface for Client methods
type ClientService interface {
{{ range .Operations }}
{{- $allParamsRequired := not .Params }}
{{- if eq (len .Params) 1 }}{{- $allParamsRequired = (index .Params 0).Required }}{{- end }}
{{- if eq (len .Params) 2 }}{{- $allParamsRequired = and (index .Params 0).Required (index .Params 1).Required }}{{- end }}
{{- /*
META COMMENT:
If there are two or less params and they are all required, remove params object as a default argument and add another function (WithParams) if users want to specify longform params
Param generation taken from https://github.com/go-swagger/go-swagger/blob/master/generator/templates/client/parameter.gotmpl
*/}}
{{ if $allParamsRequired }}
{{ pascalize .Name }}({{- template "operationArguments" . }}) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }}
{{ end }} {{- /* if $allParamsRequired */}}
{{- pascalize .Name }}{{ if $allParamsRequired }}WithParams{{ end }}(params *{{ pascalize .Name }}Params{{ if .HasStreamingResponse }}, writer io.Writer{{ end }}, opts ...ClientOption) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }}
{{ end }} {{- /* range .Operations */}}
SetTransport(transport runtime.ClientTransport)
}
{{ range .Operations }}
{{- $allParamsRequired := not .Params }}
{{- if eq (len .Params) 1 }}{{- $allParamsRequired = (index .Params 0).Required }}{{- end }}
{{- if eq (len .Params) 2 }}{{- $allParamsRequired = and (index .Params 0).Required (index .Params 1).Required }}{{- end }}
/*
{{ pascalize .Name }} {{ if .Summary }}{{ pluralizeFirstWord (humanize .Summary) }}{{ if .Description }}
{{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ humanize .Name }} API{{ end }}
*/
{{- if $allParamsRequired }}
func (a *Client) {{ pascalize .Name }}({{- template "operationArguments" . }}) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }} {
params := New{{ pascalize .Name }}Params(){{ range .Params }}.With{{ pascalize .Name }}({{ varname .Name }}){{ end }}
return a.{{ pascalize .Name }}WithParams(params{{ if .HasStreamingResponse }}, writer{{ end }}, opts...)
}
{{- end }}
func (a *Client) {{ pascalize .Name }}{{ if $allParamsRequired }}WithParams{{ end }}(params *{{ pascalize .Name }}Params{{ if .HasStreamingResponse }}, writer io.Writer{{ end }}, opts ...ClientOption) {{ if .SuccessResponse }}({{ range .SuccessResponses }}*{{ pascalize .Name }}, {{ end }}{{ end }}error{{ if .SuccessResponse }}){{ end }} {
if params == nil {
params = New{{ pascalize .Name }}Params()
}
op := &runtime.ClientOperation{
ID: {{ printf "%q" .Name }},
Method: {{ printf "%q" .Method }},
PathPattern: {{ printf "%q" .Path }},
ProducesMediaTypes: {{ printf "%#v" .ProducesMediaTypes }},
ConsumesMediaTypes: {{ printf "%#v" .ConsumesMediaTypes }},
Schemes: {{ printf "%#v" .Schemes }},
Params: params,
Reader: &{{ pascalize .Name }}Reader{formats: a.formats{{ if .HasStreamingResponse }}, writer: writer{{ end }}},
Context: params.Context,
Client: params.HTTPClient,
}
for _, opt := range opts {
if opt != nil {
opt(op)
}
}
{{ $length := len .SuccessResponses }}
{{ if .SuccessResponse }}result{{else}}_{{ end }}, err := a.transport.Submit(op)
if err != nil {
return {{ if .SuccessResponse }}{{ padSurround "nil" "nil" 0 $length }}, {{ end }}err
}
{{- if .SuccessResponse }}
{{- if eq $length 1 }}
success, ok := result.(*{{ pascalize .SuccessResponse.Name }})
if ok {
return success,nil
}
// unexpected success response
{{- if .DefaultResponse }}{{/* if a default response is provided, fill this and return an error */}}
unexpectedSuccess := result.(*{{ pascalize .DefaultResponse.Name }})
return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code())
{{- else }}
// safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue
msg := fmt.Sprintf("unexpected success response for {{ .Name }}: API contract not enforced by server. Client expected to get an error, but got: %T", result)
panic(msg)
{{- end }}
{{- else }}{{/* several possible success responses */}}
switch value := result.(type) {
{{- range $i, $v := .SuccessResponses }}
case *{{ pascalize $v.Name }}:
return {{ padSurround "value" "nil" $i $length }}, nil
{{- end }}
}
{{- if .DefaultResponse }}{{/* if a default response is provided, fill this and return an error */}}
// unexpected success response
unexpectedSuccess := result.(*{{ pascalize .DefaultResponse.Name }})
return {{ padSurround "nil" "nil" 0 $length }}, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code())
{{- else }}
// safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue
msg := fmt.Sprintf("unexpected success response for {{ $.Name }}: API contract not enforced by server. Client expected to get an error, but got: %T", result)
panic(msg)
{{- end }}
{{- end }}
{{- else }}
return nil
{{- end }}
}
{{- end }}
// SetTransport changes the transport on the client
func (a *Client) SetTransport(transport runtime.ClientTransport) {
a.transport = transport
}
// WithAuthInfo changes the transport on the client
func WithAuthInfo(authInfo runtime.ClientAuthInfoWriter) ClientOption {
return func(op *runtime.ClientOperation) {
op.AuthInfo = authInfo
}
}