dedup code

This commit is contained in:
6543 2022-06-11 22:15:12 +02:00
parent d1d27f01c7
commit 7eeafdbc5f

View file

@ -5,7 +5,6 @@ import (
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/valyala/fasthttp"
@ -17,8 +16,10 @@ const giteaAPIRepos = "/api/v1/repos/"
var ErrorNotFound = errors.New("not found")
type Client struct {
giteaRoot string
giteaAPIToken string
giteaRoot string
giteaAPIToken string
infoTimeout time.Duration
contentTimeout time.Duration
}
type FileResponse struct {
@ -33,24 +34,20 @@ func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
func NewClient(giteaRoot, giteaAPIToken string) *Client {
return &Client{
giteaRoot: giteaRoot,
giteaAPIToken: "token " + giteaAPIToken,
giteaRoot: giteaRoot,
giteaAPIToken: giteaAPIToken,
infoTimeout: 5 * time.Second,
contentTimeout: 10 * time.Second,
}
}
// TODOs:
// * handle 404 -> page will show 500 atm
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
res := fasthttp.AcquireResponse()
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
res, err := client.do(client.contentTimeout, url)
if err != nil {
return nil, err
}
switch res.StatusCode() {
case fasthttp.StatusOK:
return res.Body(), nil
@ -62,38 +59,31 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str
}
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
fastClient := getFastHTTPClient(10 * time.Second)
url := joinURL(client.giteaRoot, giteaAPIRepos, uri)
res, err := client.do(client.contentTimeout, url)
if err != nil {
return nil, err
}
// resp.SetBodyStream(&strings.Reader{}, -1)
req := fasthttp.AcquireRequest()
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, uri))
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
resp := fasthttp.AcquireResponse()
resp.SetBodyStream(&strings.Reader{}, -1)
if err := fastClient.Do(req, resp); err != nil {
if err != nil {
return nil, err
}
switch resp.StatusCode() {
switch res.StatusCode() {
case fasthttp.StatusOK:
return resp, nil
return res, nil
case fasthttp.StatusNotFound:
return nil, ErrorNotFound
default:
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode())
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
}
}
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
fastClient := getFastHTTPClient(5 * time.Second)
req := fasthttp.AcquireRequest()
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
res := fasthttp.AcquireResponse()
if err := fastClient.Do(req, res); err != nil {
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName)
res, err := client.do(client.infoTimeout, url)
if err != nil {
return time.Time{}, err
}
if res.StatusCode() != fasthttp.StatusOK {
@ -103,14 +93,9 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
}
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
fastClient := getFastHTTPClient(5 * time.Second)
req := fasthttp.AcquireRequest()
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName))
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
res := fasthttp.AcquireResponse()
if err := fastClient.Do(req, res); err != nil {
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName)
res, err := client.do(client.infoTimeout, url)
if err != nil {
return "", err
}
if res.StatusCode() != fasthttp.StatusOK {
@ -118,3 +103,15 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
}
return fastjson.GetString(res.Body(), "default_branch"), nil
}
func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(url)
req.Header.Set(fasthttp.HeaderAuthorization, "token "+client.giteaAPIToken)
res := fasthttp.AcquireResponse()
err := getFastHTTPClient(timeout).Do(req, res)
return res, err
}