1
0
Fork 0

Saving response meta

This commit is contained in:
Gregory Eremin 2015-03-05 03:14:14 +07:00
parent 3dbe630464
commit 8ac16bb695
4 changed files with 39 additions and 14 deletions

17
db/token.go Normal file
View File

@ -0,0 +1,17 @@
package db
import (
"time"
)
type (
Token struct {
ID int
Owner string
Token string
Limit int
Remaining int
ResetAt time.Time
CreatedAt time.Time
}
)

View File

@ -3,6 +3,7 @@ package task
import (
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
"github.com/localhots/steward/db"
)
func newGithubClient(token string) *github.Client {
@ -12,8 +13,14 @@ func newGithubClient(token string) *github.Client {
return github.NewClient(trans.Client())
}
// func (c *GithubClient) saveResponseMeta(res *gh.Response) {
// c.limit = res.Limit
// c.remaining = res.Remaining
// c.limitEnds = res.Reset.Time
// }
func saveResponseMeta(token string, res *github.Response) {
if res == nil {
return
}
db.UpdateToken(&db.Token{
Token: token,
Limit: res.Limit,
Remaining: res.Remaining,
ResetAt: res.Reset.Time,
})
}

View File

@ -1,7 +1,6 @@
package task
import (
"github.com/google/go-github/github"
"github.com/localhots/steward/db"
"github.com/localhots/steward/job"
)
@ -16,15 +15,16 @@ type (
)
func SyncContrib(t SyncContribTask) {
contribs := fetchContrib(newGithubClient(t.Token), t.Owner, t.Repo)
contribs := fetchContrib(t.Token, t.Owner, t.Repo)
for _, c := range contribs {
db.ImportRepo(c)
}
}
func fetchContrib(client *github.Client, owner, repo string) (res []*db.Contrib) {
func fetchContrib(token, owner, repo string) (res []*db.Contrib) {
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
// c.saveResponseMeta(resp)
saveResponseMeta(token, resp)
if err != nil {
if err.Error() == "EOF" {
// Empty repository, not an actual error

View File

@ -15,7 +15,7 @@ type (
)
func SyncRepos(t SyncReposTask) {
repos := fetchRepos(newGithubClient(t.Token), t.Owner)
repos := fetchRepos(t.Token, t.Owner)
for _, repo := range repos {
db.ImportRepo(&db.Repo{
Owner: t.Owner,
@ -24,10 +24,11 @@ func SyncRepos(t SyncReposTask) {
}
}
func fetchRepos(client *github.Client, owner string) {
func fetchRepos(token, owner string) {
var (
names = []string{}
opt = &github.RepositoryListByOrgOptions{
client = newGithubClient(token)
names = []string{}
opt = &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
}
)
@ -35,7 +36,7 @@ func fetchRepos(client *github.Client, owner string) {
for {
opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
// c.saveResponseMeta(resp) // Save current usage/limits
saveResponseMeta(token, resp)
if err != nil {
panic(err)
}