1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge pull request #395 from Seanstoppable/circleciclient

Decouple CircleCI client from widget
This commit is contained in:
Chris Cummer 2019-02-28 13:33:27 -08:00 committed by GitHub
commit 0c436a9f2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View File

@ -8,17 +8,24 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os"
"github.com/wtfutil/wtf/wtf"
) )
const APIEnvKey = "WTF_CIRCLE_API_KEY" type Client struct {
apiKey string
}
func BuildsFor() ([]*Build, error) { func NewClient(apiKey string) *Client {
client := Client{
apiKey: apiKey,
}
return &client
}
func (client *Client) BuildsFor() ([]*Build, error) {
builds := []*Build{} builds := []*Build{}
resp, err := circleRequest("recent-builds") resp, err := client.circleRequest("recent-builds")
if err != nil { if err != nil {
return builds, err return builds, err
} }
@ -34,9 +41,9 @@ var (
circleAPIURL = &url.URL{Scheme: "https", Host: "circleci.com", Path: "/api/v1/"} circleAPIURL = &url.URL{Scheme: "https", Host: "circleci.com", Path: "/api/v1/"}
) )
func circleRequest(path string) (*http.Response, error) { func (client *Client) circleRequest(path string) (*http.Response, error) {
params := url.Values{} params := url.Values{}
params.Add("circle-token", apiKey()) params.Add("circle-token", client.apiKey)
url := circleAPIURL.ResolveReference(&url.URL{Path: path, RawQuery: params.Encode()}) url := circleAPIURL.ResolveReference(&url.URL{Path: path, RawQuery: params.Encode()})
@ -60,13 +67,6 @@ func circleRequest(path string) (*http.Response, error) {
return resp, nil return resp, nil
} }
func apiKey() string {
return wtf.Config.UString(
"wtf.mods.circleci.apiKey",
os.Getenv(APIEnvKey),
)
}
func parseJson(obj interface{}, text io.Reader) { func parseJson(obj interface{}, text io.Reader) {
jsonStream, err := ioutil.ReadAll(text) jsonStream, err := ioutil.ReadAll(text)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package circleci
import ( import (
"fmt" "fmt"
"os"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
@ -9,11 +10,20 @@ import (
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
*Client
} }
const apiEnvKey = "WTF_CIRCLE_API_KEY"
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application) *Widget {
apiKey := wtf.Config.UString(
"wtf.mods.circleci.apiKey",
os.Getenv(apiEnvKey),
)
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "CircleCI", "circleci", false), TextWidget: wtf.NewTextWidget(app, "CircleCI", "circleci", false),
Client: NewClient(apiKey),
} }
return &widget return &widget
@ -26,7 +36,7 @@ func (widget *Widget) Refresh() {
return return
} }
builds, err := BuildsFor() builds, err := widget.Client.BuildsFor()
widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.Name)) widget.View.SetTitle(fmt.Sprintf("%s - Builds", widget.Name))