mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 08:57:55 +00:00
refactor client api
This commit is contained in:
parent
361639db68
commit
61b959a93b
3 changed files with 9 additions and 14 deletions
|
@ -51,24 +51,15 @@ func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
||||||
url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
|
resp, err := client.ServeRawContent(targetOwner, targetRepo, ref, resource)
|
||||||
res, err := client.do(client.contentTimeout, url)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return resp.Body(), nil
|
||||||
switch res.StatusCode() {
|
|
||||||
case fasthttp.StatusOK:
|
|
||||||
return res.Body(), nil
|
|
||||||
case fasthttp.StatusNotFound:
|
|
||||||
return nil, ErrorNotFound
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
|
func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (*fasthttp.Response, error) {
|
||||||
url := joinURL(client.giteaRoot, giteaAPIRepos, uri)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
|
||||||
res, err := client.do(client.contentTimeout, url)
|
res, err := client.do(client.contentTimeout, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -67,6 +67,10 @@ func (o *Options) generateUri() string {
|
||||||
return path.Join(o.TargetOwner, o.TargetRepo, "raw", o.TargetBranch, o.TargetPath)
|
return path.Join(o.TargetOwner, o.TargetRepo, "raw", o.TargetBranch, o.TargetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *Options) generateUriClientArgs() (targetOwner, targetRepo, ref, resource string) {
|
||||||
|
return o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath
|
||||||
|
}
|
||||||
|
|
||||||
func (o *Options) timestamp() string {
|
func (o *Options) timestamp() string {
|
||||||
return strconv.FormatInt(o.BranchTimestamp.Unix(), 10)
|
return strconv.FormatInt(o.BranchTimestamp.Unix(), 10)
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client,
|
||||||
if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() {
|
if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() {
|
||||||
cachedResponse = cachedValue.(gitea.FileResponse)
|
cachedResponse = cachedValue.(gitea.FileResponse)
|
||||||
} else {
|
} else {
|
||||||
res, err = giteaClient.ServeRawContent(uri)
|
res, err = giteaClient.ServeRawContent(o.generateUriClientArgs())
|
||||||
}
|
}
|
||||||
log.Debug().Msg("acquisition")
|
log.Debug().Msg("acquisition")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue