From 11abbc57cdb0db55b6de4bc8d64ec7b3a32297c6 Mon Sep 17 00:00:00 2001
From: a
Date: Tue, 7 Mar 2023 12:46:04 -0600
Subject: [PATCH 1/2] save me
---
src/auth/auth.go | 33 ++++++++++-----------------------
1 file changed, 10 insertions(+), 23 deletions(-)
diff --git a/src/auth/auth.go b/src/auth/auth.go
index 8c522eb..041742f 100644
--- a/src/auth/auth.go
+++ b/src/auth/auth.go
@@ -3,7 +3,6 @@ package auth
import (
"encoding/json"
"fmt"
- "io"
"log"
"net/http"
"os"
@@ -57,42 +56,30 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
),
)
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
- authFile, err := os.Open(filepath.Join(configDir, "gospt/auth.json"))
+ authFilePath := filepath.Join(configDir, "gospt/auth.json")
+ authFile, err := os.Open(authFilePath)
if err != nil {
return nil, err
}
defer authFile.Close()
- authValue, err := io.ReadAll(authFile)
+ tok := &oauth2.Token{}
+ err = json.NewDecoder(authFile).Decode(tok)
if err != nil {
return nil, err
}
- var tok *oauth2.Token
- err = json.Unmarshal(authValue, &tok)
- if err != nil {
- return nil, err
- }
- client := spotify.New(auth.Client(ctx, tok))
+ authClient := auth.Client(ctx, tok)
+ client := spotify.New(authClient)
new_token, err := client.Token()
if err != nil {
return nil, err
}
- if new_token != tok {
- out, err := json.MarshalIndent(new_token, "", " ")
- if err != nil {
- panic(err.Error())
- }
- err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
- if err != nil {
- panic("FAILED TO SAVE AUTH")
- }
- }
- out, err := json.MarshalIndent(tok, "", " ")
+ out, err := json.MarshalIndent(new_token, "", " ")
if err != nil {
- panic(err.Error())
+ return nil, err
}
- err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
+ err = os.WriteFile(authFilePath, out, 0o644)
if err != nil {
- panic("FAILED TO SAVE AUTH")
+ return nil, fmt.Errorf("failed to save auth")
}
return client, nil
}
--
2.45.2
From fd529cf011afa31d48740e22b32b885d1dcf1e37 Mon Sep 17 00:00:00 2001
From: a
Date: Tue, 7 Mar 2023 13:01:05 -0600
Subject: [PATCH 2/2] log
---
src/auth/auth.go | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/src/auth/auth.go b/src/auth/auth.go
index 041742f..bd003d2 100644
--- a/src/auth/auth.go
+++ b/src/auth/auth.go
@@ -1,14 +1,16 @@
package auth
import (
+ "context"
"encoding/json"
"fmt"
- "log"
"net/http"
"os"
"os/exec"
"path/filepath"
+ "tuxpa.in/a/zlog/log"
+
"gitea.asdf.cafe/abs3nt/gospt/src/config"
"gitea.asdf.cafe/abs3nt/gospt/src/gctx"
@@ -24,6 +26,12 @@ var (
configDir, _ = os.UserConfigDir()
)
+type roundTripperFunc func(*http.Request) (*http.Response, error)
+
+func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
+ return fn(req)
+}
+
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" {
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml"))
@@ -67,6 +75,12 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
if err != nil {
return nil, err
}
+ ctx.Context = context.WithValue(ctx.Context, oauth2.HTTPClient, &http.Client{
+ Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
+ log.Trace().Interface("path", r.URL.Path).Msg("request")
+ return http.DefaultTransport.RoundTrip(r)
+ }),
+ })
authClient := auth.Client(ctx, tok)
client := spotify.New(authClient)
new_token, err := client.Token()
@@ -91,7 +105,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Values.Port), nil)
if err != nil {
- log.Fatal(err)
+ panic(err)
}
}()
url := auth.AuthURL(state)
@@ -114,7 +128,6 @@ func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
- log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
--
2.45.2