From 12ae59c7b1cf05a3d05217b70eb0c4472b130bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Tue, 14 Jul 2020 10:22:36 +0300 Subject: [PATCH] settings method --- v5/client.go | 37 +++++++++++++++++++++++++++++++++ v5/client_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ v5/response.go | 6 ++++++ v5/types.go | 17 +++++++++++++++ 4 files changed, 113 insertions(+) diff --git a/v5/client.go b/v5/client.go index 533a218..d358468 100644 --- a/v5/client.go +++ b/v5/client.go @@ -4000,6 +4000,43 @@ func (c *Client) Segments(parameters SegmentsRequest) (SegmentsResponse, int, *e return resp, status, nil } +// Settings returns system settings +// +// For more information see https://help.retailcrm.pro/Developers/ApiVersion5#get--api-v5-settings +// +// Example: +// +// var client = v5.New("https://demo.url", "09jIJ") +// +// data, status, err := client.Settings() +// +// if err.Error() != "" { +// fmt.Printf("%v", err.RuntimeErr) +// } +// +// if status >= http.StatusBadRequest { +// fmt.Printf("%v", err.ApiErr()) +// } +// +// fmt.Printf("%#v\n", data) +func (c *Client) Settings() (SettingsResponse, int, *errs.Failure) { + var resp SettingsResponse + + data, status, err := c.GetRequest("/settings") + if err.Error() != "" { + return resp, status, err + } + + json.Unmarshal(data, &resp) + + if resp.Success == false { + buildErr(data, err) + return resp, status, err + } + + return resp, status, nil +} + // Inventories returns leftover stocks and purchasing prices // // For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-store-inventories diff --git a/v5/client_test.go b/v5/client_test.go index f00c640..912d090 100644 --- a/v5/client_test.go +++ b/v5/client_test.go @@ -4978,6 +4978,59 @@ func TestClient_Segments(t *testing.T) { } } +func TestClient_Settings(t *testing.T) { + c := client() + + defer gock.Off() + + gock.New(crmURL). + Get("/settings"). + Reply(200). + BodyString(`{ + "success": true, + "settings": { + "default_currency": { + "value": "RUB", + "updated_at": "2019-02-13 13:57:20" + }, + "system_language": { + "value": "RU", + "updated_at": "2019-02-13 14:02:23" + }, + "timezone": { + "value": "Europe/Moscow", + "updated_at": "2019-02-13 13:57:20" + } + } +} +`) + + data, status, err := c.Settings() + if err.Error() != "" { + t.Errorf("%v", err.Error()) + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err.ApiError()) + } + + if data.Success != true { + t.Errorf("%v", err.ApiError()) + } + + if data.Settings.DefaultCurrency.Value != "RUB" { + t.Errorf("Invalid default_currency value: %v", data.Settings.DefaultCurrency.Value) + } + + if data.Settings.SystemLanguage.Value != "RU" { + t.Errorf("Invalid system_language value: %v", data.Settings.SystemLanguage.Value) + } + + if data.Settings.Timezone.Value != "Europe/Moscow" { + t.Errorf("Invalid timezone value: %v", data.Settings.Timezone.Value) + } +} + func TestClient_Segments_Fail(t *testing.T) { c := client() diff --git a/v5/response.go b/v5/response.go index e97d144..641bee3 100644 --- a/v5/response.go +++ b/v5/response.go @@ -218,6 +218,12 @@ type SegmentsResponse struct { Segments []Segment `json:"segments,omitempty,brackets"` } +// SettingsResponse type +type SettingsResponse struct { + Success bool `json:"success"` + Settings Settings `json:"settings,omitempty,brackets"` +} + // CountriesResponse type type CountriesResponse struct { Success bool `json:"success"` diff --git a/v5/types.go b/v5/types.go index c6742b6..b7e198b 100644 --- a/v5/types.go +++ b/v5/types.go @@ -622,6 +622,23 @@ type Segment struct { Active bool `json:"active,omitempty"` } +/* + * Settings related types + */ + +// SettingsNode represents an item in settings. All settings nodes contains only string value and update time for now. +type SettingsNode struct { + Value string `json:"value"` + UpdatedAt string `json:"updated_at"` +} + +// Settings type. Contains retailCRM configuration. +type Settings struct { + DefaultCurrency SettingsNode `json:"default_currency"` + SystemLanguage SettingsNode `json:"system_language"` + Timezone SettingsNode `json:"timezone"` +} + /** Reference related types */