diff --git a/modules/newrelic/client.go b/modules/newrelic/client.go index 6e15940f..dc4bf870 100644 --- a/modules/newrelic/client.go +++ b/modules/newrelic/client.go @@ -1,16 +1,25 @@ package newrelic import ( - "os" - - "github.com/wtfutil/wtf/wtf" nr "github.com/yfronto/newrelic" ) -func Application() (*nr.Application, error) { - client := nr.NewClient(apiKey()) +type Client struct { + applicationId int + nrClient *nr.Client +} - application, err := client.GetApplication(wtf.Config.UInt("wtf.mods.newrelic.applicationId")) +func NewClient(apiKey string, applicationId int) *Client { + return &Client{ + applicationId: applicationId, + nrClient: nr.NewClient(apiKey), + } + +} + +func (client *Client) Application() (*nr.Application, error) { + + application, err := client.nrClient.GetApplication(client.applicationId) if err != nil { return nil, err } @@ -18,21 +27,13 @@ func Application() (*nr.Application, error) { return application, nil } -func Deployments() ([]nr.ApplicationDeployment, error) { - client := nr.NewClient(apiKey()) +func (client *Client) Deployments() ([]nr.ApplicationDeployment, error) { opts := &nr.ApplicationDeploymentOptions{Page: 1} - deployments, err := client.GetApplicationDeployments(wtf.Config.UInt("wtf.mods.newrelic.applicationId"), opts) + deployments, err := client.nrClient.GetApplicationDeployments(client.applicationId, opts) if err != nil { return nil, err } return deployments, nil } - -func apiKey() string { - return wtf.Config.UString( - "wtf.mods.newrelic.apiKey", - os.Getenv("WTF_NEW_RELIC_API_KEY"), - ) -} diff --git a/modules/newrelic/widget.go b/modules/newrelic/widget.go index 95538163..a8ee6950 100644 --- a/modules/newrelic/widget.go +++ b/modules/newrelic/widget.go @@ -2,6 +2,7 @@ package newrelic import ( "fmt" + "os" "github.com/rivo/tview" "github.com/wtfutil/wtf/wtf" @@ -10,11 +11,13 @@ import ( type Widget struct { wtf.TextWidget + client *Client } func NewWidget(app *tview.Application) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(app, "New Relic", "newrelic", false), + client: NewClient(apiKey(), wtf.Config.UInt("wtf.mods.newrelic.applicationId")), } return &widget @@ -23,8 +26,8 @@ func NewWidget(app *tview.Application) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - app, appErr := Application() - deploys, depErr := Deployments() + app, appErr := widget.client.Application() + deploys, depErr := widget.client.Deployments() appName := "error" if appErr == nil { @@ -86,3 +89,10 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string { return str } + +func apiKey() string { + return wtf.Config.UString( + "wtf.mods.newrelic.apiKey", + os.Getenv("WTF_NEW_RELIC_API_KEY"), + ) +}