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

40 lines
859 B
Go
Raw Permalink Normal View History

2020-02-20 14:40:51 +00:00
package github
import (
"encoding/json"
"fmt"
"io"
2020-02-20 14:40:51 +00:00
"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 {
// The default HTTP client's Transport may not
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is
// not read to completion and closed.
io.Copy(io.Discard, resp.Body)
2020-02-20 14:40:51 +00:00
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
}