This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from bridgetlane/add-key-transactions
add key transactions endpoints
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |