From 7eeafdbc5fc8dc761234af06e5d57086e2b3a4c2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 11 Jun 2022 22:15:12 +0200 Subject: [PATCH] dedup code --- server/gitea/client.go | 83 ++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/server/gitea/client.go b/server/gitea/client.go index 85e9879..a39c4a2 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -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 +}