Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #11 from bridgetlane/add-key-transactions
Browse files Browse the repository at this point in the history
add key transactions endpoints
  • Loading branch information
paultyng authored Oct 20, 2017
2 parents c788556 + 1b150f9 commit 01e978b
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
56 changes: 56 additions & 0 deletions api/key_transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package api

import (
"fmt"
"net/url"
)

func (c *Client) queryKeyTransactions() ([]KeyTransaction, error) {
transactions := []KeyTransaction{}

reqURL, err := url.Parse("/key_transactions.json")
if err != nil {
return nil, err
}

nextPath := reqURL.String()

for nextPath != "" {
resp := struct {
Transactions []KeyTransaction `json:"key_transactions,omitempty"`
}{}

nextPath, err = c.Do("GET", nextPath, nil, &resp)
if err != nil {
return nil, err
}

transactions = append(transactions, resp.Transactions...)
}

return transactions, nil
}

// GetKeyTransaction returns a specific key transaction by ID.
func (c *Client) GetKeyTransaction(id int) (*KeyTransaction, error) {
reqURL, err := url.Parse(fmt.Sprintf("/key_transactions/%v.json", id))
if err != nil {
return nil, err
}

resp := struct {
Transaction KeyTransaction `json:"key_transaction,omitempty"`
}{}

_, err = c.Do("GET", reqURL.String(), nil, &resp)
if err != nil {
return nil, err
}

return &resp.Transaction, nil
}

// ListKeyTransactions returns all key transactions for the account.
func (c *Client) ListKeyTransactions() ([]KeyTransaction, error) {
return c.queryKeyTransactions()
}
48 changes: 48 additions & 0 deletions api/key_transactions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"net/http"
"testing"
)

func TestKeyTransactions_Basic(t *testing.T) {
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
{
"key_transactions": [{
"id": 1,
"name": "foo",
"transaction_name": "/bar",
"health_status": "unknown",
"reporting": true,
"last_reported_at": "2017-10-19T18:16:08+00:00",
"application_summary": {
"response_time": 0.0,
"throughput": 0.0,
"error_rate": 0,
"apdex_target": 0.5,
"apdex_score": 0.0
},
"links": {
"application": 2
}
}],
"links": {
"key_transaction.application": "/v2/applications/{application_id}"
}
}
`))
}))

apps, err := c.queryKeyTransactions()
if err != nil {
t.Log(err)
t.Fatal("queryKeyTransactions error")
}

if len(apps) == 0 {
t.Fatal("No applications found")
}
}
13 changes: 13 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,16 @@ type ComponentMetric struct {
Name string `json:"name,omitempty"`
Values []string `json:"values"`
}

// KeyTransaction represents information about a New Relic key transaction.
type KeyTransaction struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
TransactionName string `json:"transaction_name,omitempty"`
HealthStatus string `json:"health_status,omitempty"`
Reporting bool `json:"reporting,omitempty"`
LastReportedAt string `json:"last_reported_at,omitempty"`
Summary ApplicationSummary `json:"application_summary,omitempty"`
EndUserSummary ApplicationEndUserSummary `json:"end_user_summary,omitempty"`
Links ApplicationLinks `json:"links,omitempty"`
}
57 changes: 57 additions & 0 deletions cmd/key_transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"github.com/imdario/mergo"
"github.com/paultyng/go-newrelic/api"
"github.com/spf13/cobra"
)

func makeKeyTransactionsCmd(dst cobra.Command) *cobra.Command {
src := cobra.Command{
Use: "key-transactions",
Aliases: []string{"transactions", "kt"},
}

if err := mergo.Merge(&dst, src); err != nil {
panic(err)
}

return &dst
}

var getKeyTransactionsCmd = makeKeyTransactionsCmd(cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
client, err := newAPIClient(cmd)
if err != nil {
return err
}

id, err := cmd.Flags().GetInt("id")
if err != nil {
return err
}

var resources []api.KeyTransaction

if id != 0 {
resource, err := client.GetKeyTransaction(id)
if err != nil {
return err
}

resources = []api.KeyTransaction{*resource}
} else {
resources, err = client.ListKeyTransactions()
if err != nil {
return err
}
}

return outputList(cmd, resources)
},
})

func init() {
getCmd.AddCommand(getKeyTransactionsCmd)
getKeyTransactionsCmd.Flags().Int("id", 0, "ID of the key transaction to get")
}

0 comments on commit 01e978b

Please sign in to comment.