Skip to content

Commit

Permalink
Moved the HTTP code into the provider package.
Browse files Browse the repository at this point in the history
Extended it with a variant that takes a http.Request.
  • Loading branch information
Necoro committed Oct 3, 2023
1 parent 2721c7e commit eaf5e69
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 35 deletions.
22 changes: 0 additions & 22 deletions pkg/http/http.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/provider/arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/provider/arch/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
9 changes: 4 additions & 5 deletions pkg/provider/aur/aur.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/aur/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions pkg/provider/http.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit eaf5e69

Please sign in to comment.