Skip to content

Commit

Permalink
Merge #279
Browse files Browse the repository at this point in the history
279: Feature/Analytics r=alallema a=brunoocasali

- Add `User-Agent` inside the pre-defined headers

Add Go support as requested here meilisearch/integration-guides#150

I tried other options, but they do not fit so well:

- https://pkg.go.dev/debug/buildinfo (does not work very well :/ has a lot of issues golang/go#29814, golang/go#33975
- https://github.com/ahmetb/govvv (I think it is not an option since it seems to be not maintained anymore).


Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
Co-authored-by: Amélie <alallema@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 5, 2022
2 parents fd24eae + 6816b79 commit 70b7e3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client_request.go
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions 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)
}
30 changes: 30 additions & 0 deletions 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)
}

0 comments on commit 70b7e3a

Please sign in to comment.