49 lines
941 B
Go
49 lines
941 B
Go
package lib
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type Forgejo struct {
|
|
Server url.URL
|
|
Token string
|
|
Branch string
|
|
}
|
|
|
|
var ErrNotFound = errors.New("404 not found")
|
|
|
|
// headers must ot be nil
|
|
func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, repo, branch, file string) (ret *http.Response, err error) {
|
|
u := f.Server
|
|
u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file
|
|
p := u.Query()
|
|
p.Set("ref", branch)
|
|
u.RawQuery = p.Encode()
|
|
fmt.Println(u.String())
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for k, v := range headers {
|
|
if v == "" {
|
|
continue
|
|
}
|
|
req.Header.Set(k, v)
|
|
}
|
|
req.Header.Set("Authorization", "token "+f.Token)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
err = ErrNotFound
|
|
return
|
|
}
|
|
|
|
return resp, nil
|
|
}
|