refactoring
This commit is contained in:
@@ -2,7 +2,6 @@ package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
@@ -12,17 +11,12 @@ type WikiFetcher struct {
|
||||
Cache *Cache
|
||||
}
|
||||
|
||||
func (f WikiFetcher) Fetch() string {
|
||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ updatedAt title }}}"}`
|
||||
func (f WikiFetcher) Fetch() []string {
|
||||
q := `{"query":"{ pages { list(orderBy: UPDATED, orderByDirection: DESC, limit: 1){ path, updatedAt, title }}}"}`
|
||||
wikiURL := fmt.Sprintf("%s/graphql", f.Config.WikiBaseURL)
|
||||
req, _ := http.NewRequest("POST", wikiURL, bytes.NewBuffer([]byte(q)))
|
||||
req.Header.Set("Authorization", "Bearer "+f.Config.WikiToken)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var r struct {
|
||||
Data struct {
|
||||
@@ -35,14 +29,17 @@ func (f WikiFetcher) Fetch() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
json.NewDecoder(res.Body).Decode(&r)
|
||||
if err := getJSON(req, &r); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if len(r.Data.Pages.List) == 0 {
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
u := r.Data.Pages.List[0]
|
||||
if f.Cache.TryUpdate("wiki", u.UpdatedAt) {
|
||||
url := fmt.Sprintf("%s/%s", f.Config.WikiBaseURL, u.Path)
|
||||
return fmt.Sprintf("Wiki: %s <%s>", u.Title, url)
|
||||
return []string{fmt.Sprintf("Wiki: %s <%s>", u.Title, url)}
|
||||
}
|
||||
return ""
|
||||
return []string{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user