Skip to content

Commit e1c20c9

Browse files
zoo-github-actions-auth[bot]github-actions[bot]jessfraz
authored
Update api spec (#121)
* YOYO NEW API SPEC! * fixes Signed-off-by: Jess Frazelle <[email protected]> --------- Signed-off-by: Jess Frazelle <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Jess Frazelle <[email protected]>
1 parent d55ac4a commit e1c20c9

14 files changed

+13022
-3477
lines changed

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.2.32
1+
v0.2.33

client.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/paths.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,23 @@ func (data *Data) generateMethod(doc *openapi3.T, method string, pathName string
269269
// Now we can get the description since we have filled in everything else.
270270
function.Description = function.getDescription(operation)
271271

272+
isMultiPart := false
273+
if operation.RequestBody != nil {
274+
for mediaType := range operation.RequestBody.Value.Content {
275+
if strings.Contains(mediaType, "multipart/form-data") {
276+
isMultiPart = true
277+
break
278+
}
279+
}
280+
}
281+
272282
exampleTemplatePath := "function-example.tmpl"
273283
if _, ok := operation.Extensions["x-dropshot-websocket"]; ok {
274284
exampleTemplatePath = "function-example-ws.tmpl"
275285
}
286+
if isMultiPart {
287+
exampleTemplatePath = "function-example-multipart.tmpl"
288+
}
276289

277290
// Build the example function.
278291
example, err := templateToString(exampleTemplatePath, function)
@@ -285,6 +298,10 @@ func (data *Data) generateMethod(doc *openapi3.T, method string, pathName string
285298
if _, ok := operation.Extensions["x-dropshot-websocket"]; ok {
286299
templatePath = "websocket.tmpl"
287300
}
301+
// If its a multipart request, we need to use a different template.
302+
if isMultiPart {
303+
templatePath = "multipart.tmpl"
304+
}
288305

289306
// Print the template for the function.
290307
f, err := templateToString(templatePath, function)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// {{.Description}}
2+
func Example{{.Tag}}Service_{{.Name}}() {
3+
client, err := {{.PackageName}}.NewClientFromEnv("your apps user agent")
4+
if err != nil {
5+
panic(err)
6+
}
7+
8+
buf := new(bytes.Buffer)
9+
10+
{{if .Response}}
11+
result, err := client.{{.Tag}}.{{.Name}}({{range .Args -}}{{.Example}},{{end -}} buf)
12+
if err != nil {
13+
panic(err)
14+
}
15+
16+
fmt.Printf("%#v", result)
17+
{{else}}
18+
if err := client.{{.Tag}}.{{.Name}}({{range .Args -}}{{.Example}},{{end -}} buf); err != nil {
19+
panic(err)
20+
}
21+
{{end}}
22+
}

cmd/tmpl/multipart.tmpl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// {{.Description}}
2+
func (s *{{.Tag}}Service) {{.Name}}({{range .Args -}}{{.Name}} {{.Type}},{{end -}} body *bytes.Buffer) {{if .Response}}(*{{.Response.Type}}, error){{else}}error{{end}} {
3+
// Create the url.
4+
path := "{{.Path}}"
5+
uri := resolveRelative(s.client.server, path)
6+
7+
8+
// Create the request.
9+
req, err := http.NewRequest("{{.Method}}", uri, body)
10+
if err != nil {
11+
return {{if .Response}}nil,{{end}} fmt.Errorf("error creating request: %v", err)
12+
}
13+
14+
{{if .RequestBody}}
15+
// Add our headers.
16+
req.Header.Add("Content-Type", "{{.RequestBody.MediaType}}")
17+
{{end}}
18+
19+
{{if .Args}}
20+
// Add the parameters to the url.
21+
if err := expandURL(req.URL, map[string]string{
22+
{{range .Args -}}
23+
"{{.Property}}": {{.ToString}},
24+
{{end -}}
25+
}); err != nil {
26+
return {{if .Response}}nil,{{end}} fmt.Errorf("expanding URL with parameters failed: %v", err)
27+
}
28+
{{end}}
29+
30+
// Send the request.
31+
resp, err := s.client.client.Do(req)
32+
if err != nil {
33+
return {{if .Response}}nil,{{end}} fmt.Errorf("error sending request: %v", err)
34+
}
35+
defer resp.Body.Close()
36+
37+
// Check the response.
38+
if err := checkResponse(resp); err != nil {
39+
return {{if .Response}}nil,{{end}} err
40+
}
41+
42+
{{if .Response}}
43+
// Decode the body from the response.
44+
if resp.Body == nil {
45+
return nil, errors.New("request returned an empty body in the response")
46+
}
47+
var decoded {{.Response.Type}}
48+
if err := json.NewDecoder(resp.Body).Decode(&decoded); err != nil {
49+
return nil, fmt.Errorf("error decoding response body: %v", err)
50+
}
51+
52+
// Return the response.
53+
return &decoded, nil
54+
{{else}}
55+
// Return.
56+
return nil
57+
{{end}}
58+
}

cmd/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ func (data *Data) generateOneOfType(name string, s *openapi3.Schema, spec *opena
298298
}, enumDocs)
299299
}
300300

301+
if len(s.OneOf) == 1 && s.OneOf[0].Value.Type == "object" {
302+
// We need to generate the one of type.
303+
return data.generateSchemaType(name, s.OneOf[0].Value, spec)
304+
}
305+
301306
// Check if they all have a type.
302307
types := []string{}
303308
typeName := ""

0 commit comments

Comments
 (0)