Skip to content

Commit 73bf93f

Browse files
committed
feat: support nexus api-version header
Updated the Go SDK to set the `API-Version` header to the Nexus OpenAPI specification version when making API requests. Closes SSE-61.
1 parent aed681a commit 73bf93f

File tree

7 files changed

+86
-9
lines changed

7 files changed

+86
-9
lines changed

.github/ISSUE_TEMPLATE/release_checklist.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ labels: release
1111
After completing each task put an `x` in the corresponding box,
1212
and paste the link to the relevant PR.
1313
-->
14-
- [ ] Make sure the [VERSION](https://github.com/oxidecomputer/oxide.go/blob/main/VERSION) and [oxide/version.go](https://github.com/oxidecomputer/oxide.go/blob/main/oxide/version.go) files have the new version you want to release.
14+
- [ ] Make sure the [VERSION](https://github.com/oxidecomputer/oxide.go/blob/main/VERSION) and [internal/generate/version.go](https://github.com/oxidecomputer/oxide.go/blob/main/internal/generate/version.go) files have the new version you want to release.
1515
- [ ] Make sure the changelog file in the `.changelog/` directory is set to the new version you want to release.
1616
- [ ] Make sure all examples and docs reference the new version.
1717
- [ ] Make sure you've pulled the latest tag on main, and generate changelog by running `make changelog`. Add the date of the release to the title, and update associated Oxide API version.
1818
- [ ] Release the new version by running `make tag`.
1919
- [ ] Update GitHub release description with release notes generated from `make changelog`.
2020
- [ ] Create a release branch from the commit of the release tag.
21-
- [ ] Bump the version in [VERSION](https://github.com/oxidecomputer/oxide.go/blob/main/VERSION) and [oxide/version.go](https://github.com/oxidecomputer/oxide.go/blob/main/oxide/version.go).
21+
- [ ] Bump the version in [VERSION](https://github.com/oxidecomputer/oxide.go/blob/main/VERSION) and [internal/generate/version.go](https://github.com/oxidecomputer/oxide.go/blob/main/internal/generate/version.go).
2222
- [ ] Create a new file for the next release in [.changelog/](https://github.com/oxidecomputer/oxide.go/blob/main/.changelog/).

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ make all
1818

1919
1. Make sure the following files have the new version you want to release.
2020
- [`VERSION`](./VERSION)
21-
- [`oxide/version.go`](./oxide/version.go)
21+
- [`oxide/version.go`](./oxide/version.go): Updated via [`internal/generate/version.go`](./internal/generate/version.go)
2222
2. Make sure you have run `make all` and pushed any changes. The release
2323
will fail if running `make all` causes any changes to the generated
2424
code.

internal/generate/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func generateSDK() error {
5959
return err
6060
}
6161

62+
versionFile := "../../oxide/version.go"
63+
if err := generateVersion(versionFile, spec); err != nil {
64+
return err
65+
}
66+
6267
return nil
6368
}
6469

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// sdkVersion is the Oxide Go SDK sdkVersion. This is used to dynamically
2+
// populate the user agent for [Client]. It is purposefully unexported to
3+
// prevent external users from reading it. This must be changed along with the
4+
// VERSION file in the root of this repository.
5+
const sdkVersion = "{{ .SDKVersion }}"
6+
7+
// openAPIVersion is the OpenAPI specification version the Oxide Go SDK was
8+
// generated from. This is used to dynamically populate the 'API-Version' header
9+
// for [Client]. It is purposefully unexported to prevent external users from
10+
// reading it.
11+
const openAPIVersion = "{{ .OpenAPIVersion }}"

internal/generate/version.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
"text/template"
10+
11+
"github.com/getkin/kin-openapi/openapi3"
12+
)
13+
14+
// generateVersion generates the version.go file with both SDK and API versions.
15+
func generateVersion(file string, spec *openapi3.T) error {
16+
f, err := openGeneratedFile(file)
17+
if err != nil {
18+
return err
19+
}
20+
defer f.Close()
21+
22+
apiVersion := ""
23+
if spec.Info != nil && spec.Info.Version != "" {
24+
apiVersion = spec.Info.Version
25+
}
26+
if apiVersion == "" {
27+
return fmt.Errorf("failed generating %s: api version cannnot be empty", file)
28+
}
29+
30+
t, err := template.ParseFiles("./templates/version.tpl")
31+
if err != nil {
32+
return fmt.Errorf("failed generating %s: %w", file, err)
33+
}
34+
35+
data := struct {
36+
SDKVersion string
37+
OpenAPIVersion string
38+
}{
39+
SDKVersion: "v0.8.0",
40+
OpenAPIVersion: apiVersion,
41+
}
42+
43+
if err := t.Execute(f, data); err != nil {
44+
return fmt.Errorf("failed generating %s: %w", file, err)
45+
}
46+
47+
return nil
48+
}

oxide/lib.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func NewClient(cfg *Config) (*Client, error) {
194194

195195
// defaultUserAgent builds and returns the default user agent string.
196196
func defaultUserAgent() string {
197-
return fmt.Sprintf("oxide.go/%s", version)
197+
return fmt.Sprintf("oxide.go/%s", sdkVersion)
198198
}
199199

200200
// getProfile determines the path of the user's credentials file
@@ -305,6 +305,7 @@ func (c *Client) buildRequest(ctx context.Context, body io.Reader, method, uri s
305305

306306
req.Header.Set("Content-Type", "application/json")
307307
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
308+
req.Header.Set("API-Version", openAPIVersion)
308309

309310
// Add the parameters to the url.
310311
if err := expandURL(req.URL, params); err != nil {

oxide/version.go

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

0 commit comments

Comments
 (0)