Skip to content

Instantly share code, notes, and snippets.

@plutov
Last active May 4, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plutov/78fb62adc5cece9e48b3581692435251 to your computer and use it in GitHub Desktop.
Save plutov/78fb62adc5cece9e48b3581692435251 to your computer and use it in GitHub Desktop.
testable6.go
package testable
import (
"encoding/json"
"fmt"
"net/http"
)
type Repo struct {
StargazersCount int `json:"stargazers_count"`
}
type RepositoriesAPI interface {
GetRepos(username string) ([]Repo, error)
}
type Mock struct{}
func (m *Mock) GetRepos(username string) ([]Repo, error) {
return []Repo{
Repo{
StargazersCount: 2,
},
Repo{
StargazersCount: 6,
},
}, nil
}
type GitHub struct{}
func (g *GitHub) GetRepos(username string) ([]Repo, error) {
res, err := http.Get(fmt.Sprintf("https://api.github.com/users/%s/repos", username))
if err != nil {
return nil, err
}
defer res.Body.Close()
repos := []Repo{}
if err := json.NewDecoder(res.Body).Decode(&repos); err != nil {
return nil, err
}
return repos, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment