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

@ -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