cli/context/config_viewer.go
Mislav Marohnić 5aca575964 Wire up OAuth authentication flow to initialize config file
The config file is now `~/.config/gh`.
2019-10-18 19:08:11 +02:00

59 lines
1.1 KiB
Go

package context
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type viewer struct {
Login string
}
type responseData struct {
Data struct {
Viewer *viewer
}
}
// TODO: figure out how to enable using the "api" package here
//
// Right now "api" is coupled to "context", so we can't import "api" from here.
func getViewer(token string) (user *viewer, err error) {
url := "https://api.github.com/graphql"
query := `{ viewer { login } }`
reqBody, err := json.Marshal(map[string]interface{}{"query": query})
if err != nil {
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return
}
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
data := responseData{}
err = json.Unmarshal(body, &data)
if err != nil {
return
}
user = data.Data.Viewer
return
}