shad-go/lectures/06-http/httptest/code.go

43 lines
767 B
Go
Raw Normal View History

2020-04-02 11:42:31 +00:00
package httptest
import (
"context"
"io/ioutil"
"net/http"
"strconv"
)
2020-04-02 14:01:16 +00:00
func NewAPICLient(baseURL string) *APIClient {
if baseURL == "" {
baseURL = BaseURLProd
}
return &APIClient{baseURL: baseURL, httpc: new(http.Client)}
}
2020-04-02 11:42:31 +00:00
const (
BaseURLProd = "https://github.com/api"
)
type APIClient struct {
baseURL string
httpc *http.Client
}
func (c *APIClient) GetReposCount(ctx context.Context, userID string) (int, error) {
url := c.baseURL + "/users/" + userID + "/repos/count"
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := c.httpc.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
return strconv.Atoi(string(body))
}
// OMIT