FetcherRequest

This commit is contained in:
Zsolt Tasnadi
2026-01-20 08:31:59 +01:00
parent 8f51e6d981
commit 6be44be9bf
4 changed files with 66 additions and 16 deletions

View File

@@ -29,15 +29,25 @@ func (f GiteaFetcher) Fetch() []Entry {
if repo == "" {
continue
}
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", f.BaseURL, strings.TrimSpace(repo))
req, _ := http.NewRequest("GET", giteaURL, nil)
repo = strings.TrimSpace(repo)
path := fmt.Sprintf("/api/v1/repos/%s/commits", repo)
headers := map[string]string{}
if f.Token != "" {
req.Header.Set("Authorization", "token "+f.Token)
headers["Authorization"] = "token " + f.Token
}
req := FetcherRequest{
BaseURL: f.BaseURL,
Path: path,
Method: http.MethodGet,
Headers: headers,
}
var r GiteaResponse
if err := getJSON(req, &r); err != nil {
if err := req.Run(&r); err != nil {
continue
}
@@ -46,8 +56,9 @@ func (f GiteaFetcher) Fetch() []Entry {
}
commit := r[0]
cacheKey := "gitea_" + url.QueryEscape(f.BaseURL+path)
if f.Cache.TryUpdate("gitea_"+url.QueryEscape(giteaURL), commit.Sha) {
if f.Cache.TryUpdate(cacheKey, commit.Sha) {
messages = append(messages, Entry{
Title: fmt.Sprintf("📝 [Gitea] - (%s) %s", repo, commit.Commit.Message),
URL: commit.HTMLURL,

View File

@@ -1,7 +1,9 @@
package lib
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
@@ -16,7 +18,33 @@ type Fetcher interface {
Fetch() []Entry
}
func getJSON(req *http.Request, target interface{}) error {
// FetcherRequest represents a common HTTP request configuration.
type FetcherRequest struct {
BaseURL string
Path string
Method string
Headers map[string]string
Body []byte
}
// Run executes the HTTP request and decodes the JSON response into target.
func (r FetcherRequest) Run(target interface{}) error {
url := r.BaseURL + r.Path
var body io.Reader
if r.Body != nil {
body = bytes.NewBuffer(r.Body)
}
req, err := http.NewRequest(r.Method, url, body)
if err != nil {
return err
}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err

View File

@@ -20,13 +20,18 @@ type RedmineFetcher struct {
}
func (f RedmineFetcher) Fetch() []Entry {
redmineURL := fmt.Sprintf("%s/issues.json", f.BaseURL)
req, _ := http.NewRequest("GET", redmineURL, nil)
req.Header.Set("X-Redmine-API-Key", f.Key)
req := FetcherRequest{
BaseURL: f.BaseURL,
Path: "/issues.json?limit=1&sort=updated_on:desc",
Method: http.MethodGet,
Headers: map[string]string{
"X-Redmine-API-Key": f.Key,
},
}
var r RedmineResponse
if err := getJSON(req, &r); err != nil {
if err := req.Run(&r); err != nil {
return []Entry{}
}

View File

@@ -1,7 +1,6 @@
package lib
import (
"bytes"
"fmt"
"net/http"
)
@@ -26,14 +25,21 @@ type WikiFetcher struct {
func (f WikiFetcher) Fetch() []Entry {
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
wikiURL := fmt.Sprintf("%s/graphql", f.BaseURL)
req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q)))
req.Header.Set("Authorization", "Bearer "+f.Token)
req.Header.Set("Content-Type", "application/json")
req := FetcherRequest{
BaseURL: f.BaseURL,
Path: "/graphql",
Method: http.MethodPost,
Headers: map[string]string{
"Authorization": "Bearer " + f.Token,
"Content-Type": "application/json",
},
Body: []byte(q),
}
var r WikiResponse
if err := getJSON(req, &r); err != nil {
if err := req.Run(&r); err != nil {
return []Entry{}
}