Skip to content

Commit a0578d1

Browse files
Made fetch functions more generic
1 parent 30a26d7 commit a0578d1

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

goso.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -320,34 +320,27 @@ func fmtText(text string) string {
320320
return t
321321
}
322322

323-
func FetchGoogle(conf *Config) (*GoogleSearchResult, error) {
323+
func FetchGoogle(conf *Config, results map[int]*Result) error {
324324
url := fmt.Sprintf("https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s",
325325
conf.ApiKey, conf.SearchEngine, netUrl.QueryEscape(conf.Query))
326326
req, err := http.NewRequest(http.MethodGet, url, nil)
327327
if err != nil {
328-
return nil, err
328+
return err
329329
}
330330
res, err := conf.Client.Do(req)
331331
if err != nil {
332-
return nil, fmt.Errorf("failed connecting to Google API: check your internet connection")
332+
return fmt.Errorf("failed connecting to Google API: check your internet connection")
333333
}
334334
defer res.Body.Close()
335335
if res.StatusCode > 299 {
336-
return nil, fmt.Errorf("failed connecting to Google API: %s", res.Status)
336+
return fmt.Errorf("failed connecting to Google API: %s", res.Status)
337337
}
338338
var gsResp GoogleSearchResult
339339
err = json.NewDecoder(res.Body).Decode(&gsResp)
340340
if err != nil {
341-
return nil, err
341+
return err
342342
}
343-
return &gsResp, nil
344-
}
345-
346-
func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result, error) {
347-
348-
results := make(map[int]*Result)
349-
questions := make([]string, 0, len(gr.Items))
350-
for _, item := range gr.Items {
343+
for _, item := range gsResp.Items {
351344
var upvoteCount int
352345
var dateCreated time.Time
353346
if len(item.Pagemap.Question) > 0 {
@@ -360,9 +353,7 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
360353
dateCreated, _ = time.Parse("2006-01-02T15:04:05", question.Datecreated)
361354
}
362355
u, _ := netUrl.Parse(item.Link)
363-
questionStr := strings.Split(u.Path, "/")[2]
364-
questions = append(questions, questionStr)
365-
questionId, _ := strconv.Atoi(questionStr)
356+
questionId, _ := strconv.Atoi(strings.Split(u.Path, "/")[2])
366357
results[questionId] = &Result{
367358
Title: item.Title,
368359
Link: item.Link,
@@ -371,24 +362,34 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
371362
Date: dateCreated,
372363
}
373364
}
365+
return nil
366+
}
367+
368+
func FetchStackOverflow(conf *Config, results map[int]*Result) error {
369+
questions := make([]string, len(results))
370+
var idx int
371+
for question := range maps.Keys(results) {
372+
questions[idx] = strconv.Itoa(question)
373+
idx++
374+
}
374375
url := fmt.Sprintf("https://api.stackexchange.com/2.3/questions/%s/answers?order=desc&sort=votes&site=stackoverflow&filter=withbody",
375376
netUrl.QueryEscape(strings.Join(questions, ";")))
376377
req, err := http.NewRequest(http.MethodGet, url, nil)
377378
if err != nil {
378-
return nil, err
379+
return err
379380
}
380381
res, err := conf.Client.Do(req)
381382
if err != nil {
382-
return nil, err
383+
return err
383384
}
384385
defer res.Body.Close()
385386
if res.StatusCode > 299 {
386-
return nil, fmt.Errorf("failed connecting to Stack Overflow API: %s", res.Status)
387+
return fmt.Errorf("failed connecting to Stack Overflow API: %s", res.Status)
387388
}
388389
var soResp StackOverflowResult
389390
err = json.NewDecoder(res.Body).Decode(&soResp)
390391
if err != nil {
391-
return nil, err
392+
return err
392393
}
393394
for _, item := range soResp.Items {
394395
result, ok := results[item.QuestionID]
@@ -405,12 +406,12 @@ func FetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
405406
Date: time.Unix(int64(item.CreationDate), 0).UTC(),
406407
})
407408
}
408-
return results, nil
409+
return nil
409410
}
410411

411412
func GetAnswers(conf *Config,
412-
fetchResults func(*Config) (*GoogleSearchResult, error),
413-
fetchAnswers func(*Config, *GoogleSearchResult) (map[int]*Result, error),
413+
fetchResults func(*Config, map[int]*Result) error,
414+
fetchAnswers func(*Config, map[int]*Result) error,
414415
) (string, error) {
415416
var err error
416417
if term.IsTerminal(0) {
@@ -435,11 +436,12 @@ func GetAnswers(conf *Config,
435436
if lexer == nil {
436437
lexer = lexers.Fallback
437438
}
438-
gsResp, err := fetchResults(conf)
439+
results := make(map[int]*Result)
440+
err = fetchResults(conf, results)
439441
if err != nil {
440442
return "", err
441443
}
442-
results, err := fetchAnswers(conf, gsResp)
444+
err = fetchAnswers(conf, results)
443445
if err != nil {
444446
return "", err
445447
}

goso_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goso
33
import (
44
"encoding/json"
55
"fmt"
6+
"maps"
67
netUrl "net/url"
78
"os"
89
"path/filepath"
@@ -19,25 +20,19 @@ func openFile(path string) (*os.File, func(), error) {
1920
}
2021
return f, func() { f.Close() }, nil
2122
}
22-
func fetchGoogle(conf *Config) (*GoogleSearchResult, error) {
23+
24+
func fetchGoogle(conf *Config, results map[int]*Result) error {
2325
var gsResp GoogleSearchResult
2426
f, close, err := openFile("goso")
2527
if err != nil {
26-
return nil, err
28+
return err
2729
}
2830
defer close()
2931
err = json.NewDecoder(f).Decode(&gsResp)
3032
if err != nil {
31-
return nil, err
33+
return err
3234
}
33-
return &gsResp, nil
34-
}
35-
36-
func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result, error) {
37-
38-
results := make(map[int]*Result)
39-
questions := make([]string, 0, len(gr.Items))
40-
for _, item := range gr.Items {
35+
for _, item := range gsResp.Items {
4136
var upvoteCount int
4237
var dateCreated time.Time
4338
if len(item.Pagemap.Question) > 0 {
@@ -50,9 +45,7 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
5045
dateCreated, _ = time.Parse("2006-01-02T15:04:05", question.Datecreated)
5146
}
5247
u, _ := netUrl.Parse(item.Link)
53-
questionStr := strings.Split(u.Path, "/")[2]
54-
questions = append(questions, questionStr)
55-
questionId, _ := strconv.Atoi(questionStr)
48+
questionId, _ := strconv.Atoi(strings.Split(u.Path, "/")[2])
5649
results[questionId] = &Result{
5750
Title: item.Title,
5851
Link: item.Link,
@@ -61,17 +54,27 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
6154
Date: dateCreated,
6255
}
6356
}
57+
return nil
58+
}
59+
60+
func fetchStackOverflow(conf *Config, results map[int]*Result) error {
61+
questions := make([]string, len(results))
62+
var idx int
63+
for question := range maps.Keys(results) {
64+
questions[idx] = strconv.Itoa(question)
65+
idx++
66+
}
6467
_ = strings.Join(questions, ";")
6568
var soResp StackOverflowResult
6669
f, close, err := openFile("answers")
6770
if err != nil {
68-
return nil, err
71+
return err
6972
}
7073
defer close()
7174

7275
err = json.NewDecoder(f).Decode(&soResp)
7376
if err != nil {
74-
return nil, err
77+
return err
7578
}
7679
for _, item := range soResp.Items {
7780
result, ok := results[item.QuestionID]
@@ -88,7 +91,7 @@ func fetchStackOverflow(conf *Config, gr *GoogleSearchResult) (map[int]*Result,
8891
Date: time.Unix(int64(item.CreationDate), 0).UTC(),
8992
})
9093
}
91-
return results, nil
94+
return nil
9295
}
9396

9497
func BenchmarkGetAnswers(b *testing.B) {

0 commit comments

Comments
 (0)