Skip to content

Commit

Permalink
Add user agent parameter to NewClient() (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-nasso authored and wecc committed Feb 13, 2017
1 parent a19abc7 commit 80a570a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Change Log
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [2.0.0] - 2017-02-13
### Changed
- **BREAKING:** `NewClient()` now requires a user agent string.

## [1.0.0] - 2017-01-26
### Added
- Initial release
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ up visit https://customer.glesys.com to create an API key for your Project.
#### Set up a Client

```go
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")
```

#### Create a Server
Expand All @@ -41,6 +41,12 @@ server, err := client.Servers.Create(context.Background(), glesys.CreateServerPa
servers, err := client.Servers.List(context.Background())
```

#### User agent

To be able to monitor usage and help track down issues, we encourage you to
provide a user agent string identifying your application or library. Recommended
syntax is `my-library/version` or `www.example.com`.

#### Context

glesys-go uses Go's [context](https://golang.org/pkg/context) library to handle
Expand All @@ -59,7 +65,7 @@ https://godoc.org/github.com/glesys/glesys-go.
1. Fork the repo.
2. Make sure to run the tests to verify that you're starting with a clean slate.
3. Add a test for your change, make sure it fails. Refactoring existing code or
improving documenation does not require new tests.
improving documentation does not require new tests.
4. Make the changes and ensure the test pass.
5. Commit your changes, push to your fork and submit a Pull Request.

Expand Down
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
)

const version = "2.0.0"

type httpClientInterface interface {
Do(*http.Request) (*http.Response, error)
}
Expand All @@ -26,23 +28,23 @@ type Client struct {
baseURL *url.URL
httpClient httpClientInterface
project string
userAgent string

IPs *IPService
Servers *ServerService
Version string
}

// NewClient creates a new Client for interacting with the GleSYS API. This is
// the main entrypoint for API interactions.
func NewClient(project, apiKey string) *Client {
func NewClient(project, apiKey, userAgent string) *Client {
baseURL, _ := url.Parse("https://api.glesys.com")

c := &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: http.DefaultClient,
project: project,
Version: "1.0.0",
userAgent: userAgent,
}

c.IPs = &IPService{client: c}
Expand Down Expand Up @@ -91,9 +93,11 @@ func (c *Client) newRequest(ctx context.Context, method, path string, params int
return nil, err
}

userAgent := strings.TrimSpace(fmt.Sprintf("%s glesys-go/%s", c.userAgent, version))

request = request.WithContext(ctx)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", fmt.Sprintf("glesys-go/%s", c.Version))
request.Header.Set("User-Agent", userAgent)
request.SetBasicAuth(c.project, c.apiKey)

return request, nil
Expand Down
14 changes: 11 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ func (c *mockHTTPClient) Do(request *http.Request) (*http.Response, error) {
}

func TestRequestHasCorrectHeaders(t *testing.T) {
client := NewClient("project-id", "api-key")
client := NewClient("project-id", "api-key", "test-application/0.0.1")

request, err := client.newRequest(context.Background(), "GET", "/", nil)
assert.NoError(t, err)

assert.Equal(t, "application/json", request.Header.Get("Content-Type"), "header Content-Type is correct")
assert.Equal(t, "glesys-go/1.0.0", request.Header.Get("User-Agent"), "header User-Agent is correct")
assert.Equal(t, "test-application/0.0.1 glesys-go/2.0.0", request.Header.Get("User-Agent"), "header User-Agent is correct")

assert.NotEmpty(t, request.Header.Get("Authorization"), "header Authorization is not empty")
}

func TestEmptyUserAgent(t *testing.T) {
client := NewClient("project-id", "api-key", "")

request, err := client.newRequest(context.Background(), "GET", "/", nil)
assert.NoError(t, err)
assert.Equal(t, "glesys-go/2.0.0", request.Header.Get("User-Agent"), "header User-Agent is correct")
}

func TestGetResponseErrorMessage(t *testing.T) {
client := NewClient("project-id", "api-key")
client := NewClient("project-id", "api-key", "test-application/0.0.1")

json := `{ "response": {"status": { "code": 400, "text": "Unauthorized" } } }`
response := http.Response{
Expand Down
6 changes: 5 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
// an API key. Signup is available at https://glesys.com/signup and API keys can
// be created at https://customer.glesys.com.
//
// client := glesys.NewClient("CL12345", "your-api-key")
// client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")
//
// CL12345 is the key of the Project you want to work with.
//
// To be able to monitor usage and help track down issues, we encourage you to
// provide a user agent string identifying your application or library.
// Recommended syntax is "my-library/version" or "www.example.com".
//
// The different modules of the GleSYS API are available on the client.
// For example:
//
Expand Down
20 changes: 10 additions & 10 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ExampleIPService_Available() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

ips, _ := client.IPs.Available(context.Background(), glesys.AvailableIPsParams{
DataCenter: "Falkenberg",
Expand All @@ -22,21 +22,21 @@ func ExampleIPService_Available() {
}

func ExampleIPService_Release() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

client.IPs.Release(context.Background(), "1.2.3.4")
}

func ExampleIPService_Reserve() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

ip, _ := client.IPs.Reserve(context.Background(), "1.2.3.4")

fmt.Println(ip.Address)
}

func ExampleIPService_Reserved() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

ips, _ := client.IPs.Reserved(context.Background())

Expand All @@ -46,7 +46,7 @@ func ExampleIPService_Reserved() {
}

func ExampleServerService_Create() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

server, _ := client.Servers.Create(context.Background(), glesys.CreateServerParams{
Bandwidth: 100,
Expand Down Expand Up @@ -78,23 +78,23 @@ func ExampleServerService_Create() {
}

func ExampleServerService_Destroy() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

client.Servers.Destroy(context.Background(), "vz12345", glesys.DestroyServerParams{
KeepIP: true, // KeepIP defaults to false
})
}

func ExampleServerService_Details() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

server, _ := client.Servers.Details(context.Background(), "vz12345")

fmt.Println(server.Hostname)
}

func ExampleServerService_List() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

servers, _ := client.Servers.List(context.Background())

Expand All @@ -104,13 +104,13 @@ func ExampleServerService_List() {
}

func ExampleServerService_Start() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

client.Servers.Start(context.Background(), "vz12345")
}

func ExampleServerService_Stop() {
client := glesys.NewClient("CL12345", "your-api-key")
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

client.Servers.Stop(context.Background(), "vz12345", glesys.StopServerParams{
Type: "reboot", // Type "soft", "hard" and "reboot" available
Expand Down

0 comments on commit 80a570a

Please sign in to comment.