Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rate limit and credits info & api error refactoring #114

Merged
merged 12 commits into from
Jul 10, 2024
14 changes: 14 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -303,3 +304,16 @@ func getMeasurementsPath() string {
func getHistoryPath() string {
return filepath.Join(getSessionPath(), "history")
}

func silenceUsageOnCreateMeasurementError(err error) bool {
e, ok := err.(*globalping.MeasurementError)
if ok {
switch e.Code {
case http.StatusBadRequest, http.StatusUnprocessableEntity:
return false
default:
return true
}
}
return true
}
6 changes: 2 additions & 4 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ func (r *Root) RunDNS(cmd *cobra.Command, args []string) error {
opts.Options.IPVersion = globalping.IPVersion6
}

res, showHelp, err := r.client.CreateMeasurement(opts)
res, err := r.client.CreateMeasurement(opts)
if err != nil {
if !showHelp {
cmd.SilenceUsage = true
}
cmd.SilenceUsage = silenceUsageOnCreateMeasurementError(err)
return err
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_Execute_DNS_Default(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_Execute_DNS_IPv4(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -136,7 +136,7 @@ func Test_Execute_DNS_IPv6(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down
6 changes: 2 additions & 4 deletions cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ func (r *Root) RunHTTP(cmd *cobra.Command, args []string) error {
opts.Options.IPVersion = globalping.IPVersion6
}

res, showHelp, err := r.client.CreateMeasurement(opts)
res, err := r.client.CreateMeasurement(opts)
if err != nil {
if !showHelp {
cmd.SilenceUsage = true
}
cmd.SilenceUsage = silenceUsageOnCreateMeasurementError(err)
return err
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_Execute_HTTP_Default(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -108,7 +108,7 @@ func Test_Execute_HTTP_IPv4(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -152,7 +152,7 @@ func Test_Execute_HTTP_IPv6(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down
6 changes: 2 additions & 4 deletions cmd/mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ func (r *Root) RunMTR(cmd *cobra.Command, args []string) error {
opts.Options.IPVersion = globalping.IPVersion6
}

res, showHelp, err := r.client.CreateMeasurement(opts)
res, err := r.client.CreateMeasurement(opts)
if err != nil {
if !showHelp {
cmd.SilenceUsage = true
}
cmd.SilenceUsage = silenceUsageOnCreateMeasurementError(err)
return err
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/mtr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_Execute_MTR_Default(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -87,7 +87,7 @@ func Test_Execute_MTR_IPv4(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -126,7 +126,7 @@ func Test_Execute_MTR_IPv6(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down
6 changes: 2 additions & 4 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,9 @@ func (r *Root) ping(opts *globalping.MeasurementCreate) error {
}

func (r *Root) createMeasurement(opts *globalping.MeasurementCreate) (*view.HistoryItem, error) {
res, showHelp, err := r.client.CreateMeasurement(opts)
res, err := r.client.CreateMeasurement(opts)
if err != nil {
if !showHelp {
r.Cmd.SilenceUsage = true
}
r.Cmd.SilenceUsage = silenceUsageOnCreateMeasurementError(err)
return nil, err
}
r.ctx.MeasurementsCreated++
Expand Down
18 changes: 9 additions & 9 deletions cmd/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Execute_Ping_Default(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -77,7 +77,7 @@ func Test_Execute_Ping_Locations_And_Session(t *testing.T) {

totalCalls := 10
gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(totalCalls).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(totalCalls).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
c1 := viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(4).Return(nil)
Expand Down Expand Up @@ -294,10 +294,10 @@ func Test_Execute_Ping_Infinite(t *testing.T) {
expectedResponse4.ID = measurementID4

gbMock := mocks.NewMockClient(ctrl)
createCall1 := gbMock.EXPECT().CreateMeasurement(expectedOpts1).Return(expectedResponse1, false, nil)
createCall2 := gbMock.EXPECT().CreateMeasurement(expectedOpts2).Return(expectedResponse2, false, nil).After(createCall1)
createCall3 := gbMock.EXPECT().CreateMeasurement(expectedOpts3).Return(expectedResponse3, false, nil).After(createCall2)
gbMock.EXPECT().CreateMeasurement(expectedOpts4).Return(expectedResponse4, false, nil).After(createCall3)
createCall1 := gbMock.EXPECT().CreateMeasurement(expectedOpts1).Return(expectedResponse1, nil)
createCall2 := gbMock.EXPECT().CreateMeasurement(expectedOpts2).Return(expectedResponse2, nil).After(createCall1)
createCall3 := gbMock.EXPECT().CreateMeasurement(expectedOpts3).Return(expectedResponse3, nil).After(createCall2)
gbMock.EXPECT().CreateMeasurement(expectedOpts4).Return(expectedResponse4, nil).After(createCall3)

expectedMeasurement1 := createDefaultMeasurement_MultipleProbes("ping", globalping.StatusFinished)
expectedMeasurement2 := createDefaultMeasurement_MultipleProbes("ping", globalping.StatusInProgress)
Expand Down Expand Up @@ -435,7 +435,7 @@ func Test_Execute_Ping_Infinite_Output_Error(t *testing.T) {
expectedResponse1 := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts1).Return(expectedResponse1, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts1).Return(expectedResponse1, nil)

expectedMeasurement := createDefaultMeasurement("ping")
gbMock.EXPECT().GetMeasurement(measurementID1).Return(expectedMeasurement, nil)
Expand Down Expand Up @@ -490,7 +490,7 @@ func Test_Execute_Ping_IPv4(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -527,7 +527,7 @@ func Test_Execute_Ping_IPv6(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down
6 changes: 2 additions & 4 deletions cmd/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ func (r *Root) RunTraceroute(cmd *cobra.Command, args []string) error {
opts.Options.IPVersion = globalping.IPVersion6
}

res, showHelp, err := r.client.CreateMeasurement(opts)
res, err := r.client.CreateMeasurement(opts)
if err != nil {
if !showHelp {
cmd.SilenceUsage = true
}
cmd.SilenceUsage = silenceUsageOnCreateMeasurementError(err)
return err
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/traceroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Execute_Traceroute_Default(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -83,7 +83,7 @@ func Test_Execute_Traceroute_IPv4(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down Expand Up @@ -121,7 +121,7 @@ func Test_Execute_Traceroute_IPv6(t *testing.T) {
expectedResponse := createDefaultMeasurementCreateResponse()

gbMock := mocks.NewMockClient(ctrl)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, false, nil)
gbMock.EXPECT().CreateMeasurement(expectedOpts).Times(1).Return(expectedResponse, nil)

viewerMock := mocks.NewMockViewer(ctrl)
viewerMock.EXPECT().Output(measurementID1, expectedOpts).Times(1).Return(nil)
Expand Down
11 changes: 10 additions & 1 deletion globalping/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ import (
)

type Client interface {
CreateMeasurement(measurement *MeasurementCreate) (*MeasurementCreateResponse, bool, error)
// Creates a new measurement with parameters set in the request body. The measurement runs asynchronously and you can retrieve its current state at the URL returned in the Location header.
//
// https://www.jsdelivr.com/docs/api.globalping.io#post
radulucut marked this conversation as resolved.
Show resolved Hide resolved
CreateMeasurement(measurement *MeasurementCreate) (*MeasurementCreateResponse, error)
// Returns the status and results of an existing measurement. Measurements are typically available for up to 7 days after creation.
//
// https://www.jsdelivr.com/docs/api.globalping.io#get
radulucut marked this conversation as resolved.
Show resolved Hide resolved
GetMeasurement(id string) (*Measurement, error)
// Returns the status and results of an existing measurement. Measurements are typically available for up to 7 days after creation.
//
// https://www.jsdelivr.com/docs/api.globalping.io#get
radulucut marked this conversation as resolved.
Show resolved Hide resolved
GetMeasurementRaw(id string) ([]byte, error)
}

Expand Down
Loading