2019-05-20 04:12:40 +00:00
|
|
|
package client
|
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
import (
|
2019-05-28 01:44:06 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-05-24 02:58:46 +00:00
|
|
|
"github.com/sqshq/sampler/metadata"
|
2019-05-28 01:44:06 +00:00
|
|
|
"io/ioutil"
|
2019-05-24 02:58:46 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
2019-05-20 04:12:40 +00:00
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
const (
|
2019-05-28 01:44:06 +00:00
|
|
|
backendUrl = "http://localhost/api/v1"
|
|
|
|
installationPath = "/telemetry/installation"
|
|
|
|
statisticsPath = "/telemetry/statistics"
|
|
|
|
crashPath = "/telemetry/crash"
|
|
|
|
registrationPath = "/license/registration"
|
2019-06-09 04:16:56 +00:00
|
|
|
verificationPath = "/license/verification"
|
2019-05-28 01:44:06 +00:00
|
|
|
jsonContentType = "application/json"
|
2019-05-24 02:58:46 +00:00
|
|
|
)
|
|
|
|
|
2019-07-27 04:15:35 +00:00
|
|
|
// BackendClient is used to verify license and to send telemetry reports
|
2019-05-28 01:44:06 +00:00
|
|
|
// for analyses (anonymous usage data statistics and crash reports)
|
2019-05-24 02:58:46 +00:00
|
|
|
type BackendClient struct {
|
|
|
|
client http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBackendClient() *BackendClient {
|
|
|
|
return &BackendClient{
|
|
|
|
client: http.Client{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BackendClient) ReportInstallation(statistics *metadata.Statistics) {
|
2019-05-28 01:44:06 +00:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(buf).Encode(statistics)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), statistics)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = http.Post(backendUrl+installationPath, jsonContentType, buf)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), statistics)
|
|
|
|
}
|
2019-05-24 02:58:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 04:16:56 +00:00
|
|
|
func (c *BackendClient) ReportStatistics(statistics *metadata.Statistics) {
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(buf).Encode(statistics)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), statistics)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = http.Post(backendUrl+statisticsPath, jsonContentType, buf)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), statistics)
|
|
|
|
}
|
2019-05-24 02:58:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
func (c *BackendClient) ReportCrash(error string, statistics *metadata.Statistics) {
|
2019-05-31 04:36:00 +00:00
|
|
|
|
|
|
|
req := struct {
|
|
|
|
Error string
|
|
|
|
Statistics *metadata.Statistics
|
|
|
|
}{
|
|
|
|
error,
|
|
|
|
statistics,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(buf).Encode(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = http.Post(backendUrl+crashPath, jsonContentType, buf)
|
2019-05-20 04:12:40 +00:00
|
|
|
}
|
2019-05-28 01:44:06 +00:00
|
|
|
|
|
|
|
func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) {
|
|
|
|
|
|
|
|
req := struct {
|
|
|
|
LicenseKey string
|
|
|
|
Statistics *metadata.Statistics
|
|
|
|
}{
|
|
|
|
licenseKey,
|
|
|
|
statistics,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(buf).Encode(req)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), statistics)
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.Post(
|
|
|
|
backendUrl+registrationPath, jsonContentType, buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
return nil, errors.New(string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
var license metadata.License
|
|
|
|
json.NewDecoder(response.Body).Decode(&license)
|
|
|
|
|
|
|
|
return &license, nil
|
|
|
|
}
|
2019-06-09 04:16:56 +00:00
|
|
|
|
|
|
|
func (c *BackendClient) VerifyLicenseKey(licenseKey string) (*metadata.License, error) {
|
|
|
|
|
|
|
|
req := struct {
|
|
|
|
LicenseKey string
|
|
|
|
}{
|
|
|
|
licenseKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(buf).Encode(req)
|
|
|
|
if err != nil {
|
|
|
|
c.ReportCrash(err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.Post(
|
|
|
|
backendUrl+verificationPath, jsonContentType, buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
return nil, errors.New(string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
var license metadata.License
|
|
|
|
json.NewDecoder(response.Body).Decode(&license)
|
|
|
|
|
|
|
|
return &license, nil
|
|
|
|
}
|