diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 48c5e3e..536961f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,9 +28,9 @@ This library uses [semantic versioning](http://semver.org/). For each release, t 1. Make sure the tests are passing in CI for the branch you're building 2. Create a new branch for the release, for example `release/1.2.3` 3. Update the CHANGELOG.md with any customer-affecting changes since the last release and add this to the git index -4. Replace all references of the current version number with the new version number and add this to the git index +4. Set the `VERSION` constant in `version.go` to the new version number and add this to the git index 5. Create a PR for the release branch 6. Once the PR is approved, merge it into `main` -7. Run `git tag ` with the new version and push the tag to git +7. Add a tag and push to origin - e.g.: `git tag v1.2.3` && `git push origin v1.2.3` 8. Create the release on Github, from the new tag, including populating the release notes 9. Update the [Ably Changelog](https://changelog.ably.com/) (via [headwayapp](https://headwayapp.co/)) with these changes (again, you can just copy the notes you added to the CHANGELOG) diff --git a/client.go b/client.go index 5c10ef1..44c89a1 100644 --- a/client.go +++ b/client.go @@ -20,6 +20,7 @@ package control import ( "bytes" "encoding/json" + "fmt" "io" "net/http" ) @@ -27,12 +28,20 @@ import ( // The URL of the Ably Control API. const API_URL = "https://control.ably.net/v1" +// defaultAblyAgent is the default value to set as the Ably-Agent HTTP header, +// and can be extended for an individual Client by calling the AppendAblyAgent +// method. +const defaultAblyAgent = "ably-control-go/" + VERSION + // Client represents a REST client for the Ably Control API. type Client struct { token string accountID string // Url is the base url for the REST API. Url string + + /// ablyAgent is the value to set as the Ably-Agent HTTP header. + ablyAgent string } // NewClient creates a new REST client. @@ -46,8 +55,9 @@ func NewClient(token string) (Client, Me, error) { // / NewClientWithURL is the same as NewClient but also takes a custom url. func NewClientWithURL(token, url string) (Client, Me, error) { client := Client{ - token: token, - Url: url, + token: token, + Url: url, + ablyAgent: defaultAblyAgent, } me, err := client.Me() if err != nil { @@ -57,6 +67,12 @@ func NewClientWithURL(token, url string) (Client, Me, error) { return client, me, nil } +// AppendAblyAgent appends an extra entry to the value sent as the Ably-Agent +// HTTP header. +func (c *Client) AppendAblyAgent(product, version string) { + c.ablyAgent = fmt.Sprintf("%s %s/%s", c.ablyAgent, product, version) +} + func (c *Client) request(method, path string, in, out interface{}) error { var inR io.Reader if in != nil { @@ -71,6 +87,7 @@ func (c *Client) request(method, path string, in, out interface{}) error { return err } req.Header.Set("Authorization", "Bearer "+c.token) + req.Header.Set("Ably-Agent", c.ablyAgent) if in != nil { req.Header.Set("Content-Type", "application/json") } diff --git a/client_test.go b/client_test.go index 1662746..7a9bfc5 100644 --- a/client_test.go +++ b/client_test.go @@ -3,6 +3,8 @@ package control import ( "fmt" "math/rand" + "net/http" + "net/http/httptest" "os" "testing" "time" @@ -65,3 +67,33 @@ func newTestClient(t *testing.T) (Client, Me) { return client, me } + +// TestAblyAgent tests that client requests set the Ably-Agent HTTP header. +func TestAblyAgent(t *testing.T) { + // start a test HTTP server which tracks the value of the Ably-Agent + // HTTP header and returns an empty JSON object. + var ablyAgent string + handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + ablyAgent = req.Header.Get("Ably-Agent") + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Length", "2") + w.WriteHeader(http.StatusOK) + w.Write([]byte("{}")) + }) + srv := httptest.NewServer(handler) + + // initialise a client, which will make a request to /me + client, _, err := NewClientWithURL("s3cr3t", srv.URL) + assert.NoError(t, err) + + // check the Ably-Agent HTTP header was set + assert.Equal(t, "ably-control-go/"+VERSION, ablyAgent) + + // add an extra Ably-Agent entry + client.AppendAblyAgent("test", "1.2.3") + + // check requests now set the updated Ably-Agent HTTP header + _, err = client.Me() + assert.NoError(t, err) + assert.Equal(t, "ably-control-go/"+VERSION+" test/1.2.3", ablyAgent) +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..f64f73b --- /dev/null +++ b/version.go @@ -0,0 +1,6 @@ +package control + +// VERSION is the version of this package. +// +// It is sent in requests to the Control API in the Ably-Agent HTTP header. +const VERSION = "0.1.0"