This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
render_client.go
150 lines (132 loc) · 5.12 KB
/
render_client.go
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
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"strings"
surface "github.com/googleapis/gnostic/surface"
)
const parameters = "parameters"
// ParameterList returns a string representation of a method's parameters
func ParameterList(parametersType *surface.Type) string {
result := "ctx context.Context,\n"
if parametersType != nil {
result += parameters + " " + parametersType.Name
}
return result
}
func (renderer *Renderer) RenderClient() ([]byte, error) {
f := NewLineWriter()
f.WriteLine("// GENERATED FILE: DO NOT EDIT!")
f.WriteLine(``)
f.WriteLine("package " + renderer.Package)
// imports will be automatically added by imports pkg
f.WriteLine(`// Client represents an API client.`)
f.WriteLine(`type Client struct {`)
f.WriteLine(` service string`)
f.WriteLine(` APIKey string`)
f.WriteLine(` client *http.Client`)
f.WriteLine(`}`)
f.WriteLine(`// NewClient creates an API client.`)
f.WriteLine(`func NewClient(service string, c *http.Client) *Client {`)
f.WriteLine(` client := &Client{}`)
f.WriteLine(` client.service = service`)
f.WriteLine(` if c != nil {`)
f.WriteLine(` client.client = c`)
f.WriteLine(` } else {`)
f.WriteLine(` client.client = http.DefaultClient`)
f.WriteLine(` }`)
f.WriteLine(` return client`)
f.WriteLine(`}`)
for _, method := range renderer.Model.Methods {
parametersType := renderer.Model.TypeWithTypeName(method.ParametersTypeName)
responsesType := renderer.Model.TypeWithTypeName(method.ResponsesTypeName)
f.WriteLine(commentForText(method.Description))
f.WriteLine(`func (client *Client) ` + method.ClientName + `(`)
f.WriteLine(ParameterList(parametersType) + `) (`)
if method.ResponsesTypeName == "" {
f.WriteLine(`err error,`)
} else {
f.WriteLine(`response *` + method.ResponsesTypeName + `,`)
f.WriteLine(`err error,`)
}
f.WriteLine(` ) {`)
path := method.Path
path = strings.Replace(path, "{+", "{", -1)
f.WriteLine(`path := client.service + "` + path + `"`)
if parametersType != nil {
if parametersType.HasFieldWithPosition(surface.Position_PATH) {
for _, field := range parametersType.Fields {
if field.Position == surface.Position_PATH {
f.WriteLine(`path = strings.Replace(path, "{` + field.Name + `}", fmt.Sprintf("%v", ` +
parameters + "." + strings.Title(field.ParameterName) + `), 1)`)
}
}
}
if parametersType.HasFieldWithPosition(surface.Position_QUERY) {
f.WriteLine(`v := url.Values{}`)
for _, field := range parametersType.Fields {
if field.Position == surface.Position_QUERY {
if field.NativeType == "string" {
f.WriteLine(`if (` + parameters + "." + strings.Title(field.ParameterName) + ` != "") {`)
f.WriteLine(` v.Set("` + field.Name + `", ` + parameters + "." + strings.Title(field.ParameterName) + `)`)
f.WriteLine(`}`)
}
}
}
f.WriteLine(`if client.APIKey != "" {`)
f.WriteLine(` v.Set("key", client.APIKey)`)
f.WriteLine(`}`)
f.WriteLine(`if len(v) > 0 {`)
f.WriteLine(` path = path + "?" + v.Encode()`)
f.WriteLine(`}`)
}
}
if method.Method == "POST" {
f.WriteLine(`payload := new(bytes.Buffer)`)
if parametersType != nil && parametersType.FieldWithPosition(surface.Position_BODY) != nil {
f.WriteLine(`json.NewEncoder(payload).Encode(` + parameters + "." + strings.Title(parametersType.FieldWithPosition(surface.Position_BODY).FieldName) + `)`)
}
f.WriteLine(`req, err := http.NewRequest("` + method.Method + `", path, payload)`)
f.WriteLine(`reqHeaders := make(http.Header)`)
f.WriteLine(`reqHeaders.Set("Content-Type", "application/json")`)
f.WriteLine(`req.Header = reqHeaders`)
} else {
f.WriteLine(`req, err := http.NewRequest("` + method.Method + `", path, nil)`)
}
f.WriteLine(`if err != nil {return}`)
f.WriteLine(`req = req.WithContext(ctx)`)
f.WriteLine(`resp, err := client.client.Do(req)`)
f.WriteLine(`if err != nil {return}`)
f.WriteLine(`defer resp.Body.Close()`)
f.WriteLine(`if resp.StatusCode != 200 {`)
if responsesType != nil {
f.WriteLine(` return nil, errors.New(resp.Status)`)
} else {
f.WriteLine(` return errors.New(resp.Status)`)
}
f.WriteLine(`}`)
if responsesType != nil {
f.WriteLine(`response = &` + responsesType.TypeName + `{}`)
f.WriteLine(`body, err := ioutil.ReadAll(resp.Body)`)
f.WriteLine(`if err != nil {return nil, err}`)
f.WriteLine(`err = json.Unmarshal(body, response)`)
f.WriteLine(`if err != nil {return nil, err}`)
f.WriteLine("return response, nil")
} else {
f.WriteLine("return nil")
}
f.WriteLine("}")
}
return f.Bytes(), nil
}