Skip to content

Commit

Permalink
chore: rename search fetch method
Browse files Browse the repository at this point in the history
  • Loading branch information
luigibarbato committed Oct 9, 2023
1 parent c10aa06 commit 17eb32c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
42 changes: 11 additions & 31 deletions internal/client/wikipedia/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
var (
ErrEmptyQuery = errors.New("query string must not be empty")
ErrEmptyLanguage = errors.New("language string must not be empty")
ErrDisambiguationResult = errors.New("disambiguation result")
)

// Create a new WikiClient
Expand Down Expand Up @@ -113,13 +114,13 @@ func (c *Client) updateLastCall(now time.Time) {
c.lastCall = now
}

func (w *Client) FetchEntityDetails(query string, lang string) (search.EntityDetails, error) {
func (w *Client) FetchContextDetails(query string, lang string) (search.ContextDetails, error) {
if query == "" {
return search.EntityDetails{}, ErrEmptyQuery
return search.ContextDetails{}, ErrEmptyQuery
}

if lang == "" {
return search.EntityDetails{}, ErrEmptyLanguage
return search.ContextDetails{}, ErrEmptyLanguage
}

args := map[string]string{
Expand All @@ -132,36 +133,35 @@ func (w *Client) FetchEntityDetails(query string, lang string) (search.EntityDet

res, err := w.doRequest(args, lang)
if err != nil {
return search.EntityDetails{}, err
return search.ContextDetails{}, err
}

if len(res.Query.Search) == 0 {
return search.EntityDetails{}, nil
return search.ContextDetails{}, nil
}

title := res.Query.Search[0].Title

wikiPage, err := MakeWikipediaPage(-1, title, "", false, w, lang)
if len(wikiPage.Disambiguation) != 0 {
title = wikiPage.Disambiguation[0]
wikiPage, err = MakeWikipediaPage(-1, title, "", false, w, lang)
return search.ContextDetails{}, nil
}

if err != nil {
return search.EntityDetails{}, err
return search.ContextDetails{}, err
}

summary, err := wikiPage.GetSummary(w, lang)
if err != nil {
return search.EntityDetails{}, err
return search.ContextDetails{}, err
}

thumbnail, err := wikiPage.GetThumbURL(w, lang)
if err != nil {
return search.EntityDetails{}, err
return search.ContextDetails{}, err
}

return search.EntityDetails{
return search.ContextDetails{
Title: wikiPage.Title,
Language: wikiPage.Language,
Link: wikiPage.URL,
Expand All @@ -170,23 +170,3 @@ func (w *Client) FetchEntityDetails(query string, lang string) (search.EntityDet
Thumbnail: thumbnail,
}, nil
}

func (c *Client) Suggest(_input, lang string) (string, error) {
args := map[string]string{
"action": "query",
"list": "search",
"srlimit": "1",
"srprop": "",
"srinfo": "suggestion",
"srsearch": _input,
}

res, err := c.doRequest(args, lang)
if err != nil {
return "", err
}
if res.Error.Code != "" {
return "", errors.New(res.Error.Info)
}
return res.Query.SearchInfo.Suggestion, nil
}
2 changes: 1 addition & 1 deletion internal/client/wikipedia/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestWikiSearch(t *testing.T) {
t.Parallel()

w := wikipedia.NewClient()
actual, err := w.FetchEntityDetails(tc.input.query, tc.input.lang)
actual, err := w.FetchContextDetails(tc.input.query, tc.input.lang)

if tc.output.err != nil {
assert.Equal(t, tc.output.err.Error(), err.Error())
Expand Down
6 changes: 3 additions & 3 deletions internal/search/search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package search

type EntityDetails struct {
type ContextDetails struct {
Title string
Link string
Summary string
Expand All @@ -9,10 +9,10 @@ type EntityDetails struct {
Source string
}

func (i EntityDetails) IsValid() bool {
func (i ContextDetails) IsValid() bool {
return i.Title != "" && i.Link != "" && i.Summary != "" && i.Thumbnail != "" && i.Language != "" && i.Source != ""
}

type SearchClient interface {
FetchEntityDetails(query, locale string) (EntityDetails, error)
FetchContextDetails(query, locale string) (ContextDetails, error)
}
2 changes: 1 addition & 1 deletion internal/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Server) GetV1Version(ctx echo.Context) error {

func (s *Server) GetV1InformerWikiQuery(ctx echo.Context, query string) error {
// TODO: add language support
searchRes, err := s.search.FetchEntityDetails(query, "en")
searchRes, err := s.search.FetchContextDetails(query, "en")
if err != nil {
e := api.Error{
Code: http.StatusInternalServerError,
Expand Down

0 comments on commit 17eb32c

Please sign in to comment.