Move to go-modrinth lib (v2 API) and always supply UA in HTTP requests

This commit is contained in:
comp500
2022-08-02 02:53:31 +01:00
parent 5c02b31e20
commit 044c34e07c
10 changed files with 97 additions and 315 deletions

View File

@@ -13,6 +13,18 @@ import (
"strings"
)
const UserAgent = "packwiz/packwiz"
func GetWithUA(url string, contentType string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept", contentType)
return http.DefaultClient.Do(req)
}
const DownloadCacheImportFolder = "import"
type DownloadSession interface {
@@ -143,8 +155,7 @@ func downloadNewFile(task *downloadTask, cacheFolder string, hashesToObtain []st
if len(hashesToObtain) > 0 {
var data io.ReadCloser
if task.url != "" {
resp, err := http.Get(task.url)
// TODO: content type, user-agent?
resp, err := GetWithUA(task.url, "application/octet-stream")
if err != nil {
return CompletedDownload{}, fmt.Errorf("failed to download %s: %w", task.url, err)
}

View File

@@ -3,7 +3,6 @@ package core
import (
"encoding/xml"
"errors"
"net/http"
"strings"
)
@@ -53,7 +52,7 @@ var ModLoaders = map[string]ModLoaderComponent{
func FetchMavenVersionList(url string) func(mcVersion string) ([]string, string, error) {
return func(mcVersion string) ([]string, string, error) {
res, err := http.Get(url)
res, err := GetWithUA(url, "application/xml")
if err != nil {
return []string{}, "", err
}
@@ -69,7 +68,7 @@ func FetchMavenVersionList(url string) func(mcVersion string) ([]string, string,
func FetchMavenVersionPrefixedList(url string, friendlyName string) func(mcVersion string) ([]string, string, error) {
return func(mcVersion string) ([]string, string, error) {
res, err := http.Get(url)
res, err := GetWithUA(url, "application/xml")
if err != nil {
return []string{}, "", err
}