From 80a570a1c14879147de723ed82915648d545984f Mon Sep 17 00:00:00 2001 From: Emil Andersson Date: Mon, 13 Feb 2017 15:26:32 +0100 Subject: [PATCH] Add user agent parameter to NewClient() (#1) --- CHANGELOG.md | 15 +++++++++++++++ README.md | 10 ++++++++-- client.go | 12 ++++++++---- client_test.go | 14 +++++++++++--- doc.go | 6 +++++- doc_test.go | 20 ++++++++++---------- 6 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..942512a --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 356a484..13283bf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. diff --git a/client.go b/client.go index f4b33e3..96bf96c 100644 --- a/client.go +++ b/client.go @@ -11,6 +11,8 @@ import ( "strings" ) +const version = "2.0.0" + type httpClientInterface interface { Do(*http.Request) (*http.Response, error) } @@ -26,15 +28,15 @@ 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{ @@ -42,7 +44,7 @@ func NewClient(project, apiKey string) *Client { baseURL: baseURL, httpClient: http.DefaultClient, project: project, - Version: "1.0.0", + userAgent: userAgent, } c.IPs = &IPService{client: c} @@ -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 diff --git a/client_test.go b/client_test.go index 157dcc9..2cb5765 100644 --- a/client_test.go +++ b/client_test.go @@ -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{ diff --git a/doc.go b/doc.go index 6c3ed04..50c1ec2 100644 --- a/doc.go +++ b/doc.go @@ -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: // diff --git a/doc_test.go b/doc_test.go index 404b345..90fd535 100644 --- a/doc_test.go +++ b/doc_test.go @@ -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", @@ -22,13 +22,13 @@ 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") @@ -36,7 +36,7 @@ func ExampleIPService_Reserve() { } 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()) @@ -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, @@ -78,7 +78,7 @@ 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 @@ -86,7 +86,7 @@ func ExampleServerService_Destroy() { } 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") @@ -94,7 +94,7 @@ func ExampleServerService_Details() { } 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()) @@ -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