Last active
May 4, 2020 19:43
-
-
Save plutov/78fb62adc5cece9e48b3581692435251 to your computer and use it in GitHub Desktop.
testable6.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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