Skip to content

Commit

Permalink
Merge pull request #21 from ably/ably-agent
Browse files Browse the repository at this point in the history
Set the Ably-Agent HTTP header
  • Loading branch information
lmars authored Jan 9, 2023
2 parents 7c24385 + 86393e4 commit 8421c6f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <VERSION_NUMBER>` 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)
21 changes: 19 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ package control
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)

// 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.
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
}
Expand Down
32 changes: 32 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package control
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 8421c6f

Please sign in to comment.