From 24eb7bf57171e65ae74cbaa6307f7064d5075212 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 28 Jul 2019 13:15:34 -0400 Subject: [PATCH] minor refactoring --- README.md | 4 ++-- client/backend.go | 26 +++++++++++++++++--------- component/gauge/gauge.go | 4 ++-- component/util/format.go | 3 +-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 72d21c3..602892a 100644 --- a/README.md +++ b/README.md @@ -391,9 +391,9 @@ textboxes:
Java application uptime example -- Prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm) - ```yml +# prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm) + textboxes: - title: Java application uptime multistep-init: diff --git a/client/backend.go b/client/backend.go index a26cb95..e1610a1 100644 --- a/client/backend.go +++ b/client/backend.go @@ -16,10 +16,9 @@ const ( crashPath = "/telemetry/crash" registrationPath = "/license/registration" verificationPath = "/license/verification" - jsonContentType = "application/json" ) -// BackendClient is used to verify license and to send telemetry reports +// BackendClient is used to verify license and to send telemetry // for analyses (anonymous usage data statistics and crash reports) type BackendClient struct { client http.Client @@ -39,7 +38,8 @@ func (c *BackendClient) ReportInstallation(statistics *metadata.Statistics) { c.ReportCrash(err.Error(), statistics) } - _, err = http.Post(backendUrl+installationPath, jsonContentType, buf) + _, err = sendRequest(backendUrl+installationPath, buf) + if err != nil { c.ReportCrash(err.Error(), statistics) } @@ -53,7 +53,7 @@ func (c *BackendClient) ReportStatistics(statistics *metadata.Statistics) { c.ReportCrash(err.Error(), statistics) } - _, err = http.Post(backendUrl+statisticsPath, jsonContentType, buf) + _, err = sendRequest(backendUrl+statisticsPath, buf) if err != nil { c.ReportCrash(err.Error(), statistics) } @@ -75,7 +75,7 @@ func (c *BackendClient) ReportCrash(error string, statistics *metadata.Statistic return } - _, _ = http.Post(backendUrl+crashPath, jsonContentType, buf) + _, _ = sendRequest(backendUrl+crashPath, buf) } func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) { @@ -94,8 +94,7 @@ func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metada c.ReportCrash(err.Error(), statistics) } - response, err := http.Post( - backendUrl+registrationPath, jsonContentType, buf) + response, err := sendRequest(backendUrl+registrationPath, buf) if err != nil { return nil, err @@ -126,8 +125,7 @@ func (c *BackendClient) VerifyLicenseKey(licenseKey string) (*metadata.License, c.ReportCrash(err.Error(), nil) } - response, err := http.Post( - backendUrl+verificationPath, jsonContentType, buf) + response, err := sendRequest(backendUrl+verificationPath, buf) if err != nil { return nil, err @@ -143,3 +141,13 @@ func (c *BackendClient) VerifyLicenseKey(licenseKey string) (*metadata.License, return &license, nil } + +func sendRequest(url string, body *bytes.Buffer) (resp *http.Response, err error) { + c := http.DefaultClient + req, err := http.NewRequest("POST", url, body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + return c.Do(req) +} diff --git a/component/gauge/gauge.go b/component/gauge/gauge.go index 542ba54..2e61c37 100644 --- a/component/gauge/gauge.go +++ b/component/gauge/gauge.go @@ -61,10 +61,10 @@ func (g *Gauge) ConsumeSample(sample *data.Sample) { if err != nil { g.HandleConsumeFailure("Failed to parse a number", err, sample) return - } else { - g.HandleConsumeSuccess() } + g.HandleConsumeSuccess() + switch sample.Label { case MinValueLabel: g.minValue = float diff --git a/component/util/format.go b/component/util/format.go index 3ea1715..c3eb904 100644 --- a/component/util/format.go +++ b/component/util/format.go @@ -22,9 +22,8 @@ var AsciiLogo = []string{ func FormatValue(value float64, scale int) string { if math.Abs(value) == math.MaxFloat64 { return "Inf" - } else { - return formatTrailingDigits(addRadixChars(value), scale) } + return formatTrailingDigits(addRadixChars(value), scale) } func FormatDelta(value float64, scale int) string {