Move update versioning to query parameter, and fix up scope API tests (#244)

* Move update versioning to query parameter, and fix up scope API tests

* Add version checks for update requests in other services
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-07 17:56:55 -04:00
1 parent 0d0660b061
commit bbd200101f
16 files changed
+240 -102

No files matched your search

+34 -3
View File
@@ -14,6 +14,15 @@ import (
)
func toPath(segments []string, action string) string {
// If it's just a collection name, don't do a fmt.Sprintf
if len(segments) == 1 {
ret := segments[0]
if action != "" {
ret = fmt.Sprintf("%s:%s", ret, action)
}
return fmt.Sprintf(`"%s"`, ret)
}
var printfString, printfArg []string
for i, s := range segments {
if i%2 == 0 {
@@ -40,7 +49,13 @@ func getArgsAndPaths(in []string, action string) (colArgs, resArgs []string, col
pathSegment = append(pathSegment, collectionName, varName)
}
return argNames[:len(argNames)-1], argNames, toPath(pathSegment[:len(pathSegment)-1], action), toPath(pathSegment, action)
colArgs, resArgs, colPath, resPath = argNames[:len(argNames)-1], argNames, toPath(pathSegment[:len(pathSegment)-1], action), toPath(pathSegment, action)
// Scopes create and list operations always need scope ID
if colPath == `"scopes"` {
colArgs = append(colArgs, "scopeId")
}
return
}
type templateInput struct {
@@ -185,6 +200,11 @@ func (c *{{ .ClientName }}Client) List(ctx context.Context, {{ range .Collection
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
{{ if ( eq .CollectionPath "\"scopes\"" ) }}
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
{{ end }}
resp, err := c.client.Do(req)
if err != nil {
@@ -287,6 +307,11 @@ func (c *{{ .ClientName }}Client) Create(ctx context.Context, {{ range .Collecti
if err != nil {
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
}
{{ if ( eq .CollectionPath "\"scopes\"" ) }}
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
{{ end }}
resp, err := c.client.Do(req)
if err != nil {
@@ -331,7 +356,6 @@ func (c *{{ .ClientName }}Client) Update(ctx context.Context, {{ range .Resource
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
{{ end }}
req, err := c.client.NewRequest(ctx, "PATCH", {{ .ResourcePath }}, opts.valueMap, apiOpts...)
@@ -339,6 +363,10 @@ func (c *{{ .ClientName }}Client) Update(ctx context.Context, {{ range .Resource
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
@@ -395,7 +423,6 @@ func (c *{{ $input.ClientName }}Client) {{ $fullName }}(ctx context.Context, {{
}
version = existingTarget.Version
}
opts.valueMap["version"] = version
{{ end }}
if len({{ $value }}) > 0 {
@@ -411,6 +438,10 @@ func (c *{{ $input.ClientName }}Client) {{ $fullName }}(ctx context.Context, {{
return nil, nil, fmt.Errorf("error creating {{ $fullName }} request: %w", err)
}
q := url.Values{}
q.Add("version", fmt.Sprintf("%d", version))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during {{ $fullName }} call: %w", err)