From 3e6d9de5eaaec218b3718ba63f18be26f32e5a68 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Sat, 2 Apr 2022 19:40:42 -0300 Subject: [PATCH 1/2] Create a way to send raw version & qualified version --- version.go | 13 +++++++++++++ version_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 version.go create mode 100644 version_test.go diff --git a/version.go b/version.go new file mode 100644 index 00000000..32ee3a35 --- /dev/null +++ b/version.go @@ -0,0 +1,13 @@ +package meilisearch + +import "fmt" + +const VERSION = "0.19.1" + +func GetQualifiedVersion() (qualifiedVersion string) { + return getQualifiedVersion(VERSION) +} + +func getQualifiedVersion(version string) (qualifiedVersion string) { + return fmt.Sprintf("Meilisearch Go (v%s)", version) +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 00000000..3a6a0e90 --- /dev/null +++ b/version_test.go @@ -0,0 +1,30 @@ +package meilisearch + +import ( + "testing" + "fmt" + "regexp" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" +) + +func TestVersion_GetQualifiedVersion(t *testing.T) { + version := GetQualifiedVersion() + + require.NotNil(t, version) + require.Equal(t, version, fmt.Sprintf("Meilisearch Go (v%s)", VERSION)) +} + +func TestVersion_qualifiedVersionFormat(t *testing.T) { + version := getQualifiedVersion("2.2.5") + + require.NotNil(t, version) + require.Equal(t, version, "Meilisearch Go (v2.2.5)") +} + +func TestVersion_constVERSIONFormat(t *testing.T) { + match, _ := regexp.MatchString("[0-9]+.[0-9]+.[0-9]+", VERSION) + + assert.True(t, match) +} From c54697b2ceb9a96bc28476d6fb302e5a9505b31a Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Sat, 2 Apr 2022 19:41:08 -0300 Subject: [PATCH 2/2] Add User-Agent header with QualifiedVersion --- client_request.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client_request.go b/client_request.go index 14411dff..811fda65 100644 --- a/client_request.go +++ b/client_request.go @@ -137,6 +137,8 @@ func (c *Client) sendRequest(req *internalRequest, internalError *Error, respons request.Header.Set("Authorization", "Bearer "+c.config.APIKey) } + request.Header.Set("User-Agent", GetQualifiedVersion()) + // request is sent if c.config.Timeout != 0 { err = c.httpClient.DoTimeout(request, response, c.config.Timeout)