Helper method for HTTP requests

This commit is contained in:
Felipe Martin 2020-11-19 20:27:37 +01:00
parent 5d64f5d035
commit ba5b373fa0
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 24 additions and 15 deletions

20
pkg/helpers/http.go Normal file
View File

@ -0,0 +1,20 @@
package helpers
import (
"net/http"
"net/url"
)
func DoRequest(method string, requestURL string) (*http.Response, error) {
parsedURL, _ := url.Parse(requestURL)
request := http.Request{
Method: method,
URL: parsedURL,
Header: map[string][]string{
"User-Agent": {"github.com/fmartingr/games-screenshot-manager"},
},
ProtoMajor: 2,
ProtoMinor: 1,
}
return http.DefaultClient.Do(&request)
}

View File

@ -5,8 +5,6 @@ import (
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
@ -18,6 +16,7 @@ import (
)
const providerName string = "steam"
const gameListURL string = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"
type SteamApp struct {
AppID uint64 `json:"appid"`
@ -57,19 +56,9 @@ func getBasePathForOS() string {
return path
}
func GetSteamAppsList(c chan SteamAppList) {
func getSteamAppList(c chan SteamAppList) {
log.Println("Updating steam game list...")
steamGetAppListURL, _ := url.Parse("https://api.steampowered.com/ISteamApps/GetAppList/v2/")
request := http.Request{
Method: "GET",
URL: steamGetAppListURL,
Header: map[string][]string{
"User-Agent": {"games-screenshot-manager/0.0.1"},
},
ProtoMajor: 2,
ProtoMinor: 1,
}
response, err := http.DefaultClient.Do(&request)
response, err := helpers.DoRequest("GET", gameListURL)
if err != nil {
panic(err)
}
@ -160,7 +149,7 @@ func GetScreenshotsForGame(user string, game *games.Game) {
func GetGames() []games.Game {
var localGames []games.Game
c := make(chan SteamAppList)
go GetSteamAppsList(c)
go getSteamAppList(c)
users := GuessUsers()
steamApps := <-c
for _, userID := range users {