Auth works

This commit is contained in:
2015-03-05 19:57:36 +07:00
parent 7fc7b1889b
commit 4295811647
12 changed files with 185 additions and 74 deletions
+44
View File
@@ -0,0 +1,44 @@
package task
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"github.com/localhots/empact/config"
"github.com/localhots/empact/db"
)
type (
FetchAccessTokenTask struct {
Code string
Result chan string
*db.Task
}
)
func FetchAccessToken(tk Tasker) {
t := tk.(*FetchAccessTokenTask)
payload := url.Values{}
payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret)
payload.Set("code", t.Code)
payload.Set("redirect_uri", config.C().RedirectURI)
buf := bytes.NewBuffer([]byte(payload.Encode()))
resp, err := http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
pairs, _ := url.ParseQuery(string(body))
t.Result <- pairs.Get("access_token")
}
+7
View File
@@ -6,6 +6,13 @@ import (
"github.com/localhots/empact/db"
)
type (
Tasker interface {
Save()
T() *db.Task
}
)
func newGithubClient(token string) *github.Client {
trans := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
+3 -2
View File
@@ -7,11 +7,12 @@ import (
type (
SyncContribTask struct {
Repo string
db.Task
*db.Task
}
)
func SyncContrib(t SyncContribTask) {
func SyncContrib(tk Tasker) {
t := tk.(*SyncContribTask)
client := newGithubClient(t.Token)
contribs, resp, err := client.Repositories.ListContributorsStats(t.Owner, t.Repo)
saveResponseMeta(t.Token, resp)
+4 -4
View File
@@ -7,13 +7,13 @@ import (
type (
SyncReposTask struct {
db.Task
*db.Task
}
)
func SyncRepos(t SyncReposTask) {
client := newGithubClient(token)
names := []string{}
func SyncRepos(tk Tasker) {
t := tk.(*SyncReposTask)
client := newGithubClient(t.Token)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
}