shad-go/lectures/01-basics/github/github.go

35 lines
667 B
Go
Raw Normal View History

2020-02-20 14:40:51 +00:00
package github
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
2020-02-20 14:42:27 +00:00
var IssuesURL string
2020-02-20 14:40:51 +00:00
type IssuesSearchResult struct{}
// SearchIssues queries the GitHub issue tracker.
func SearchIssues(terms []string) (*IssuesSearchResult, error) {
q := url.QueryEscape(strings.Join(terms, " "))
resp, err := http.Get(IssuesURL + "?q=" + q)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("search query failed: %s", resp.Status)
}
var result IssuesSearchResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}