♻️ Add custom http functions so that User-Agent is always sent
This commit is contained in:
parent
cfef5a6c7a
commit
2208b1de55
3 changed files with 62 additions and 15 deletions
|
|
@ -4,9 +4,9 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
u "net/url"
|
||||
|
||||
"codeberg.org/danjones000/strip-beats/config"
|
||||
h "codeberg.org/danjones000/strip-beats/utils/http"
|
||||
)
|
||||
|
||||
type MbRecording struct {
|
||||
|
|
@ -77,17 +77,9 @@ func GetMbRecording(id string) (MbRecording, error) {
|
|||
|
||||
func FillMbRecording(rec *MbRecording) error {
|
||||
url := fmt.Sprintf("https://musicbrainz.org/ws/2/recording/%s", rec.Id)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
q := req.URL.Query()
|
||||
q.Add("fmt", "json")
|
||||
q.Add("inc", "releases+media+artist-credits+isrcs+genres")
|
||||
req.URL.RawQuery = q.Encode()
|
||||
req.Header.Set("User-Agent", config.UserAgent)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := h.GetWithQuery(url, u.Values{
|
||||
"fmt": []string{"json"},
|
||||
"inc": []string{"releases+media+artist-credits+isrcs+genres"}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
u "net/url"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/danjones000/strip-beats/config"
|
||||
h "codeberg.org/danjones000/strip-beats/utils/http"
|
||||
)
|
||||
|
||||
type FPrint struct {
|
||||
|
|
@ -61,13 +61,14 @@ func LookupFingerprint(print FPrint) (IdResults, error) {
|
|||
return res, errors.New("Missing acoustic_id_key from config")
|
||||
}
|
||||
url := "https://api.acoustid.org/v2/lookup"
|
||||
|
||||
form := u.Values{
|
||||
"format": {"json"},
|
||||
"client": {key},
|
||||
"duration": {fmt.Sprintf("%.0f", print.Duration)},
|
||||
"fingerprint": {print.Fingerprint},
|
||||
"meta": {"recordingids"}}
|
||||
resp, err := http.PostForm(url, form)
|
||||
resp, err := h.PostForm(url, form)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
|
|
|||
54
utils/http/http.go
Normal file
54
utils/http/http.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
h "net/http"
|
||||
u "net/url"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/danjones000/strip-beats/config"
|
||||
)
|
||||
|
||||
func addUserAgent(req *h.Request) {
|
||||
req.Header.Set("User-Agent", config.UserAgent)
|
||||
}
|
||||
|
||||
func NewRequest(method, url string, body io.Reader) (*h.Request, error) {
|
||||
req, err := h.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addUserAgent(req)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func Do(req *h.Request) (*h.Response, error) {
|
||||
addUserAgent(req)
|
||||
return h.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func GetWithQuery(url string, query u.Values) (*h.Response, error) {
|
||||
req, err := h.NewRequest(h.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q := req.URL.Query()
|
||||
for k, vs := range query {
|
||||
for _, v := range vs {
|
||||
q.Add(k, v)
|
||||
}
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
return Do(req)
|
||||
}
|
||||
|
||||
func PostForm(url string, form u.Values) (*h.Response, error) {
|
||||
body := strings.NewReader(form.Encode())
|
||||
req, err := h.NewRequest(h.MethodPost, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
return Do(req)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue