From eaf5e69fcc0e3ad6b9a6c9568108c150d6167eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Wed, 4 Oct 2023 01:34:02 +0200 Subject: [PATCH] Moved the HTTP code into the provider package. Extended it with a variant that takes a http.Request. --- pkg/http/http.go | 22 ---------------------- pkg/provider/arch/arch.go | 6 +++--- pkg/provider/arch/package.go | 9 +++++++-- pkg/provider/aur/aur.go | 9 ++++----- pkg/provider/aur/rpc.go | 6 +++--- pkg/provider/http.go | 31 +++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 35 deletions(-) delete mode 100644 pkg/http/http.go create mode 100644 pkg/provider/http.go diff --git a/pkg/http/http.go b/pkg/http/http.go deleted file mode 100644 index 4cd48b5..0000000 --- a/pkg/http/http.go +++ /dev/null @@ -1,22 +0,0 @@ -package http - -import ( - "fmt" - "io" - "net/http" -) - -func Fetch(url string) (io.ReadCloser, error) { - resp, err := http.Get(url) - if err != nil { - return nil, fmt.Errorf("fetching %s: %w", url, err) - } - - if resp.StatusCode >= 300 { - resp.Body.Close() - - return nil, fmt.Errorf("fetching %s: Server returned status %s", url, resp.Status) - } - - return resp.Body, nil -} diff --git a/pkg/provider/arch/arch.go b/pkg/provider/arch/arch.go index 197f687..503ca8e 100644 --- a/pkg/provider/arch/arch.go +++ b/pkg/provider/arch/arch.go @@ -8,8 +8,8 @@ import ( "time" "github.com/Necoro/arch-log/pkg/entries" - "github.com/Necoro/arch-log/pkg/http" "github.com/Necoro/arch-log/pkg/log" + "github.com/Necoro/arch-log/pkg/provider" ) type commit struct { @@ -50,7 +50,7 @@ func (e commit) cleanedMessage() string { } func fetch(url string, jsonEntries any) error { - result, err := http.Fetch(url) + result, err := provider.Fetch(url) if err != nil { return err } @@ -162,7 +162,7 @@ func GetPkgBuild(pkg, repo string) (io.ReadCloser, error) { commitRef := repoInfo.refConstraint() url := buildPkgBuildUrl(basePkg, commitRef) - body, err := http.Fetch(url) + body, err := provider.Fetch(url) if err != nil { body.Close() return nil, err diff --git a/pkg/provider/arch/package.go b/pkg/provider/arch/package.go index e084c6b..72ec148 100644 --- a/pkg/provider/arch/package.go +++ b/pkg/provider/arch/package.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/Necoro/arch-log/pkg/entries" - "github.com/Necoro/arch-log/pkg/http" "github.com/Necoro/arch-log/pkg/log" + "github.com/Necoro/arch-log/pkg/provider" ) type result struct { @@ -59,7 +59,7 @@ func buildPkgUrl(pkg string) string { } func fetchPkgInfo(url, repo string) (result, repoInfo, error) { - res, err := http.Fetch(url) + res, err := provider.Fetch(url) if err != nil { return result{}, nil, err } @@ -150,3 +150,8 @@ func determineBaseInfo(pkg, repo string) (string, repoInfo, error) { return result.PkgBase, repoInfo, nil } + +func DetermineBaseInfo(pkg, repo string) (string, error) { + bi, _, err := determineBaseInfo(pkg, repo) + return bi, err +} diff --git a/pkg/provider/aur/aur.go b/pkg/provider/aur/aur.go index 35a9c87..41bb941 100644 --- a/pkg/provider/aur/aur.go +++ b/pkg/provider/aur/aur.go @@ -9,8 +9,8 @@ import ( "time" "github.com/Necoro/arch-log/pkg/entries" - "github.com/Necoro/arch-log/pkg/http" "github.com/Necoro/arch-log/pkg/log" + "github.com/Necoro/arch-log/pkg/provider" ) type feed struct { @@ -56,7 +56,7 @@ func buildUrl(pkg, verb string) string { } func fetch(url string) ([]entry, error) { - result, err := http.Fetch(url) + result, err := provider.Fetch(url) if err != nil { return nil, err } @@ -92,7 +92,7 @@ func setupFetch(pkg, repo string) (string, error) { return "", errors.New("repo is not supported by AUR") } - basePkg, err := determineBasePkg(pkg) + basePkg, err := DetermineBasePkg(pkg) if err != nil { return "", err } @@ -124,9 +124,8 @@ func GetPkgBuild(pkg, repo string) (io.ReadCloser, error) { } url := buildUrl(basePkg, "plain/PKGBUILD") - body, err := http.Fetch(url) + body, err := provider.Fetch(url) if err != nil { - body.Close() return nil, err } diff --git a/pkg/provider/aur/rpc.go b/pkg/provider/aur/rpc.go index cad2131..8ef74fa 100644 --- a/pkg/provider/aur/rpc.go +++ b/pkg/provider/aur/rpc.go @@ -6,8 +6,8 @@ import ( "net/url" "github.com/Necoro/arch-log/pkg/entries" - "github.com/Necoro/arch-log/pkg/http" "github.com/Necoro/arch-log/pkg/log" + "github.com/Necoro/arch-log/pkg/provider" ) type result struct { @@ -24,7 +24,7 @@ func buildRpcUrl(pkg string) string { } func fetchRpcInfo(url string) (result, error) { - res, err := http.Fetch(url) + res, err := provider.Fetch(url) if err != nil { return result{}, err } @@ -47,7 +47,7 @@ func fetchRpcInfo(url string) (result, error) { return infos.Results[0], nil } -func determineBasePkg(pkg string) (string, error) { +func DetermineBasePkg(pkg string) (string, error) { url := buildRpcUrl(pkg) result, err := fetchRpcInfo(url) diff --git a/pkg/provider/http.go b/pkg/provider/http.go new file mode 100644 index 0000000..7e55393 --- /dev/null +++ b/pkg/provider/http.go @@ -0,0 +1,31 @@ +package provider + +import ( + "fmt" + "io" + "net/http" +) + +func Request(req *http.Request) (io.ReadCloser, error) { + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, fmt.Errorf("fetching %s: %w", req.URL, err) + } + + if resp.StatusCode >= 300 { + resp.Body.Close() + + return nil, fmt.Errorf("fetching %s: Server returned status %s", req.URL, resp.Status) + } + + return resp.Body, nil +} + +func Fetch(url string) (io.ReadCloser, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + return Request(req) +}