1 2// Client which conforms to the OpenAPI3 specification for this service. 3type Client struct { 4 service ihttp.Service 5} 6 7// Creates a new Client, with reasonable defaults 8func NewClient(service ihttp.Service) (*Client) { 9 // create a client with sane default values 10 client := Client{ 11 service: service, 12 } 13 return &client 14} 15 16 17// The interface specification for the client above. 18type ClientInterface interface { 19{{range . -}} 20{{$hasParams := .RequiresParamObject -}} 21{{$pathParams := .PathParams -}} 22{{$opid := .OperationId -}} 23 // {{$opid}} request {{if .HasBody}} with any body{{end}} 24 {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Response, error) 25{{range .Bodies}} 26 {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Response, error) 27{{end}}{{/* range .Bodies */}} 28{{end}}{{/* range . $opid := .OperationId */}} 29} 30 31 32{{/* Generate client methods */}} 33{{range . -}} 34{{$hasParams := .RequiresParamObject -}} 35{{$pathParams := .PathParams -}} 36{{$lenServers := 0 -}} 37{{if .Spec.Servers}} 38{{$lenServers = len .Spec.Servers -}} 39{{end}} 40{{$opid := .OperationId -}} 41 42func (c *Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Response, error) { 43 req, err := New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(c.service.{{if eq $lenServers 0}}ServerAPIURL(){{else}}ServerURL(){{end}}{{genParamNames .PathParams}}{{if $hasParams}}, params{{end}}{{if .HasBody}}, contentType, body{{end}}) 44 if err != nil { 45 return nil, err 46 } 47 req = req.WithContext(ctx) 48 return c.service.DoHTTPRequestWithResponse(req, nil) 49} 50 51{{range .Bodies}} 52func (c *Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Response, error) { 53 req, err := New{{$opid}}{{.Suffix}}Request(c.service.{{if eq $lenServers 0}}ServerAPIURL(){{else}}ServerURL(){{end}}{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body) 54 if err != nil { 55 return nil, err 56 } 57 req = req.WithContext(ctx) 58 return c.service.DoHTTPRequestWithResponse(req, nil) 59} 60{{end}}{{/* range .Bodies */}} 61{{end}} 62 63{{/* Generate request builders */}} 64{{range .}} 65{{$hasParams := .RequiresParamObject -}} 66{{$pathParams := .PathParams -}} 67{{$bodyRequired := .BodyRequired -}} 68{{$opid := .OperationId -}} 69 70{{range .Bodies}} 71// New{{$opid}}Request{{.Suffix}} calls the generic {{$opid}} builder with {{.ContentType}} body 72func New{{$opid}}Request{{.Suffix}}(server string{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Request, error) { 73 var bodyReader io.Reader 74 buf, err := json.Marshal(body) 75 if err != nil { 76 return nil, err 77 } 78 bodyReader = bytes.NewReader(buf) 79 return New{{$opid}}RequestWithBody(server{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, "{{.ContentType}}", bodyReader) 80} 81{{end}} 82 83// New{{$opid}}Request{{if .HasBody}}WithBody{{end}} generates requests for {{$opid}}{{if .HasBody}} with any type of body{{end}} 84func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Request, error) { 85 var err error 86{{range $paramIdx, $param := .PathParams}} 87 var pathParam{{$paramIdx}} string 88 {{if .IsPassThrough}} 89 pathParam{{$paramIdx}} = {{.GoVariableName}} 90 {{end}} 91 {{if .IsJson}} 92 var pathParamBuf{{$paramIdx}} []byte 93 pathParamBuf{{$paramIdx}}, err = json.Marshal({{.GoVariableName}}) 94 if err != nil { 95 return nil, err 96 } 97 pathParam{{$paramIdx}} = string(pathParamBuf{{$paramIdx}}) 98 {{end}} 99 {{if .IsStyled}} 100 pathParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationPath, {{.GoVariableName}}) 101 if err != nil { 102 return nil, err 103 } 104 {{end}} 105{{end}} 106 serverURL, err := url.Parse(server) 107 if err != nil { 108 return nil, err 109 } 110 111 operationPath := fmt.Sprintf("{{genParamFmtString .Path}}"{{range $paramIdx, $param := .PathParams}}, pathParam{{$paramIdx}}{{end}}) 112 if operationPath[0] == '/' { 113 operationPath = "." + operationPath 114 } 115 116 queryURL, err := serverURL.Parse(operationPath) 117 if err != nil { 118 return nil, err 119 } 120 121{{if .QueryParams}} 122 queryValues := queryURL.Query() 123{{range $paramIdx, $param := .QueryParams}} 124 {{if not .Required}} if params.{{.GoName}} != nil { {{end}} 125 {{if .IsPassThrough}} 126 queryValues.Add("{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}}) 127 {{end}} 128 {{if .IsJson}} 129 if queryParamBuf, err := json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}); err != nil { 130 return nil, err 131 } else { 132 queryValues.Add("{{.ParamName}}", string(queryParamBuf)) 133 } 134 135 {{end}} 136 {{if .IsStyled}} 137 if queryFrag, err := runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationQuery, {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil { 138 return nil, err 139 } else if parsed, err := url.ParseQuery(queryFrag); err != nil { 140 return nil, err 141 } else { 142 for k, v := range parsed { 143 for _, v2 := range v { 144 queryValues.Add(k, v2) 145 } 146 } 147 } 148 {{end}} 149 {{if not .Required}}}{{end}} 150{{end}} 151 queryURL.RawQuery = queryValues.Encode() 152{{end}}{{/* if .QueryParams */}} 153 req, err := http.NewRequest("{{.Method}}", queryURL.String(), {{if .HasBody}}body{{else}}nil{{end}}) 154 if err != nil { 155 return nil, err 156 } 157 158 {{if .HasBody}}req.Header.Add("Content-Type", contentType){{end}} 159{{range $paramIdx, $param := .HeaderParams}} 160 {{if not .Required}} if params.{{.GoName}} != nil { {{end}} 161 var headerParam{{$paramIdx}} string 162 {{if .IsPassThrough}} 163 headerParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}} 164 {{end}} 165 {{if .IsJson}} 166 var headerParamBuf{{$paramIdx}} []byte 167 headerParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}) 168 if err != nil { 169 return nil, err 170 } 171 headerParam{{$paramIdx}} = string(headerParamBuf{{$paramIdx}}) 172 {{end}} 173 {{if .IsStyled}} 174 headerParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, {{if not .Required}}*{{end}}params.{{.GoName}}) 175 if err != nil { 176 return nil, err 177 } 178 {{end}} 179 req.Header.Set("{{.ParamName}}", headerParam{{$paramIdx}}) 180 {{if not .Required}}}{{end}} 181{{end}} 182 183{{range $paramIdx, $param := .CookieParams}} 184 {{if not .Required}} if params.{{.GoName}} != nil { {{end}} 185 var cookieParam{{$paramIdx}} string 186 {{if .IsPassThrough}} 187 cookieParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}} 188 {{end}} 189 {{if .IsJson}} 190 var cookieParamBuf{{$paramIdx}} []byte 191 cookieParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}) 192 if err != nil { 193 return nil, err 194 } 195 cookieParam{{$paramIdx}} = url.QueryEscape(string(cookieParamBuf{{$paramIdx}})) 196 {{end}} 197 {{if .IsStyled}} 198 cookieParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("simple", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationCookie, {{if not .Required}}*{{end}}params.{{.GoName}}) 199 if err != nil { 200 return nil, err 201 } 202 {{end}} 203 cookie{{$paramIdx}} := &http.Cookie{ 204 Name:"{{.ParamName}}", 205 Value:cookieParam{{$paramIdx}}, 206 } 207 req.AddCookie(cookie{{$paramIdx}}) 208 {{if not .Required}}}{{end}} 209{{end}} 210 return req, nil 211} 212 213{{end}}{{/* Range */}} 214