From 782260019e3e7107d9837d9d4ff7b40663ac4841 Mon Sep 17 00:00:00 2001 From: "Igor A. Melekhine" Date: Tue, 8 Aug 2023 11:18:47 +0300 Subject: [PATCH] appver --- request.go | 2 +- version.go | 17 ++++++++++++++++- version_test.go | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 version_test.go diff --git a/request.go b/request.go index 50552b0..25cf257 100644 --- a/request.go +++ b/request.go @@ -36,7 +36,7 @@ func init() { // newRequest creates new http request to RSP. func newRequest(ctx context.Context, method, link, apiToken string, payload []byte) (*http.Request, error) { req, err := http.NewRequestWithContext(ctx, method, link, bytes.NewBuffer(payload)) - req.Header.Set("User-Agent", userAgent+"/"+Version) + req.Header.Set("User-Agent", version()) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") req.Header.Set("Authorization", "Bearer "+apiToken) diff --git a/version.go b/version.go index f53dfe4..3157cf7 100644 --- a/version.go +++ b/version.go @@ -1,7 +1,22 @@ package qiwi +import "fmt" + +// AppVersion for version string of main application version +var AppVersion string + const ( // Version string. - Version string = "0.3" + Version string = "1.0.0" userAgent string = "Sendtips-QIWI-Go" // UserAgent name ) + +func version() (s string) { + s = fmt.Sprintf("%s/%s", userAgent, Version) + + if AppVersion != "" { + s = fmt.Sprintf("%s (%s)", AppVersion, s) + } + + return s +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..d041b5f --- /dev/null +++ b/version_test.go @@ -0,0 +1,22 @@ +package qiwi + +import ( + "fmt" + "testing" +) + +func TestVersion(t *testing.T) { + s := fmt.Sprintf("%s/%s", userAgent, Version) + + if s != version() { + t.Error("Version string is invalid") + } + + AppVersion = "somever/1.0" + s = fmt.Sprintf("%s (%s/%s)", AppVersion, userAgent, Version) + + if s != version() { + t.Error("Version string is invalid") + } + +}