Added constants for app name and version, use custom user-agent

This commit is contained in:
Philipp Wolfer 2023-11-24 08:58:31 +01:00
parent f6b4ea4a46
commit 46e6a667c8
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
10 changed files with 75 additions and 6 deletions

View file

@ -23,17 +23,18 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/version"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "scotty",
Use: version.AppName,
Short: "Beam data between music listening services",
Long: `Scotty transfers your listens/scrobbles between ListenBrainz and
various other listening and streaming services.`,
Version: "0.1.0",
Version: version.AppVersion,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
@ -67,7 +68,7 @@ func init() {
func defaultConfigDir() string {
configDir, err := os.UserConfigDir()
cobra.CheckErr(err)
return path.Join(configDir, "scotty")
return path.Join(configDir, version.AppName)
}
// initConfig reads in config file and ENV variables if set.
@ -78,7 +79,7 @@ func initConfig() {
} else {
viper.AddConfigPath(defaultConfigDir())
viper.SetConfigType("toml")
viper.SetConfigName("scotty")
viper.SetConfigName(version.AppName)
}
viper.AutomaticEnv() // read in environment variables that match

View file

@ -27,6 +27,7 @@ import (
"strconv"
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/version"
"golang.org/x/oauth2"
)
@ -43,6 +44,7 @@ func NewClient(token oauth2.TokenSource) Client {
client := resty.New()
client.SetBaseURL(baseURL)
client.SetHeader("Accept", "application/json")
client.SetHeader("User-Agent", version.UserAgent())
client.SetRetryCount(5)
return Client{
HttpClient: client,

View file

@ -27,6 +27,7 @@ import (
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/ratelimit"
"go.uploadedlobster.com/scotty/internal/version"
)
const MaxItemsPerGet = 50
@ -42,6 +43,7 @@ func NewClient(serverUrl string, token string) Client {
client.SetAuthScheme("Bearer")
client.SetAuthToken(token)
client.SetHeader("Accept", "application/json")
client.SetHeader("User-Agent", version.UserAgent())
// Handle rate limiting (see https://docs.funkwhale.audio/developer/api/rate-limit.html)
ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After")

View file

@ -28,6 +28,7 @@ import (
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/ratelimit"
"go.uploadedlobster.com/scotty/internal/version"
)
const (
@ -48,6 +49,7 @@ func NewClient(token string) Client {
client.SetAuthScheme("Token")
client.SetAuthToken(token)
client.SetHeader("Accept", "application/json")
client.SetHeader("User-Agent", version.UserAgent())
// Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting)
ratelimit.EnableHTTPHeaderRateLimit(client, "X-RateLimit-Reset-In")

View file

@ -23,6 +23,7 @@ import (
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/version"
)
type ListenBrainzApiBackend struct {
@ -119,7 +120,8 @@ func (b *ListenBrainzApiBackend) ImportListens(export models.ListensResult, impo
AdditionalInfo: l.AdditionalInfo,
},
}
listen.TrackMetadata.AdditionalInfo["submission_client"] = "Scotty"
listen.TrackMetadata.AdditionalInfo["submission_client"] = version.AppName
listen.TrackMetadata.AdditionalInfo["submission_client_version"] = version.AppVersion
submission.Payload = append(submission.Payload, listen)
}

View file

@ -26,6 +26,7 @@ import (
"strconv"
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/version"
)
const MaxItemsPerGet = 1000
@ -39,6 +40,7 @@ func NewClient(serverUrl string, token string) Client {
client := resty.New()
client.SetBaseURL(serverUrl)
client.SetHeader("Accept", "application/json")
client.SetHeader("User-Agent", version.UserAgent())
client.SetRetryCount(5)
return Client{
HttpClient: client,

View file

@ -30,6 +30,7 @@ import (
"github.com/go-resty/resty/v2"
"go.uploadedlobster.com/scotty/internal/ratelimit"
"go.uploadedlobster.com/scotty/internal/version"
"golang.org/x/oauth2"
)
@ -48,6 +49,7 @@ func NewClient(token oauth2.TokenSource) Client {
client := resty.NewWithClient(httpClient)
client.SetBaseURL(baseURL)
client.SetHeader("Accept", "application/json")
client.SetHeader("User-Agent", version.UserAgent())
// Handle rate limiting (see https://developer.spotify.com/documentation/web-api/concepts/rate-limits)
ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After")

View file

@ -24,6 +24,7 @@ import (
"github.com/delucks/go-subsonic"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/version"
)
type SubsonicApiBackend struct {
@ -38,7 +39,7 @@ func (b *SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend {
Client: &http.Client{},
BaseUrl: config.GetString("server-url"),
User: config.GetString("username"),
ClientName: "Scotty",
ClientName: version.AppName,
}
b.password = config.GetString("token")
return b

View file

@ -0,0 +1,25 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package version
const (
AppName = "scotty"
AppVersion = "0.1.0"
)
func UserAgent() string {
return AppName + "/" + AppVersion
}

View file

@ -0,0 +1,30 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package version_test
import (
"testing"
"go.uploadedlobster.com/scotty/internal/version"
)
func TestUserAgent(t *testing.T) {
expected := "scotty/" + version.AppVersion
ua := version.UserAgent()
if ua != expected {
t.Errorf("Expected UserAgent to be '%v', got '%v'", expected, ua)
}
}