Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Analytics #279

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions client_request.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
Comment on lines +7 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of writing to function instead of one? Is it for having a public and a private one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I could add just one function but I was thinking about how to test it, so I created two functions one with the possibility to add a version (private) and another used in the app (public).

This way I could test the format and I could guarantee that the user will always send the right value "VERSION" to the function.
Because I could let this responsibility to the caller:

	request.Header.Set("User-Agent", GetQualifiedVersion(VERSION))

But how can I ensure by tests the user is using the VERSION and not something else?

PS: I still need to understand more about the Go concepts to improve this 😅, feel free to point a better solution :)

30 changes: 30 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}