Move backend to subdir
[brackets.git] / backend / client / client.go
diff --git a/backend/client/client.go b/backend/client/client.go
new file mode 100644 (file)
index 0000000..394ee5f
--- /dev/null
@@ -0,0 +1,26 @@
+package client
+
+import (
+       "context"
+       "fmt"
+       "github.com/zmb3/spotify"
+       "golang.org/x/oauth2/clientcredentials"
+       "os"
+)
+
+func Get() (*spotify.Client, error) {
+       config := &clientcredentials.Config{
+               ClientID:     os.Getenv("SPOTIFY_ID"),
+               ClientSecret: os.Getenv("SPOTIFY_SECRET"),
+               TokenURL:     spotify.TokenURL,
+       }
+
+       token, err := config.Token(context.Background())
+       if err != nil {
+               return nil, fmt.Errorf("client: %w", err)
+       }
+
+       client := spotify.Authenticator{}.NewClient(token)
+       client.AutoRetry = true
+       return &client, nil
+}