diff --git a/client.go b/client.go index 50d5224..1e92466 100644 --- a/client.go +++ b/client.go @@ -2148,6 +2148,48 @@ func (c *Client) IntegrationModuleEdit(integrationModule IntegrationModule) ( return resp, status, nil } +// UpdateScopes updates permissions for the API key +// +// For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#post--api-v5-integration-modules-code-update-scopes +// +// Example: +// +// var client = retailcrm.New("https://demo.url", "09jIJ") +// +// data, status, err := client.UpdateScopes("moysklad3", retailcrm.UpdateScopesRequest{ +// Requires: retailcrm.ScopesRequired{Scopes: []string{"scope1", "scope2"}}}, +// }) +// +// if err.Error() != "" { +// fmt.Printf("%v", err.Error()) +// } +// +// if status >= http.StatusBadRequest { +// fmt.Printf("%v", err.Error()) +// } +// +// if data.Success == true { +// fmt.Printf("%v\n", data.APIKey) +// } +func (c *Client) UpdateScopes(code string, request UpdateScopesRequest) (UpdateScopesResponse, int, error) { + var resp UpdateScopesResponse + updateJSON, _ := json.Marshal(&request) + + data, status, err := c.PostRequest( + fmt.Sprintf("/integration-modules/%s/update-scopes", code), + bytes.NewBuffer(updateJSON)) + if err != nil { + return resp, status, err + } + + err = json.Unmarshal(data, &resp) + if err != nil { + return resp, status, err + } + + return resp, status, nil +} + // Orders returns list of orders matched the specified filters // // For more information see http://www.retailcrm.pro/docs/Developers/ApiVersion5#get--api-v5-orders diff --git a/client_test.go b/client_test.go index d01fba8..5d545e3 100644 --- a/client_test.go +++ b/client_test.go @@ -6564,3 +6564,65 @@ func TestClient_CustomFieldsCreate_Fail(t *testing.T) { t.Error(successFail) } } + +func TestClient_UpdateScopes(t *testing.T) { + c := client() + + code := RandomString(8) + + defer gock.Off() + + request := UpdateScopesRequest{Requires: ScopesRequired{Scopes: []string{"scope1", "scope2"}}} + + jr, _ := json.Marshal(&request) + + gock.New(crmURL). + Post(fmt.Sprintf("/integration-modules/%s/update-scopes", code)). + BodyString(string(jr[:])). + Reply(200). + BodyString(`{"success": true, "apiKey": "newApiKey"}`) + + m, status, err := c.UpdateScopes(code, request) + if err != nil { + t.Errorf("%v", err) + } + + if status != http.StatusOK { + t.Errorf("%v", err) + } + + if m.Success != true { + t.Errorf("%v", err) + } +} + +func TestClient_UpdateScopes_Fail(t *testing.T) { + c := client() + + code := RandomString(8) + + defer gock.Off() + + request := UpdateScopesRequest{Requires: ScopesRequired{Scopes: []string{"scope1", "scope2"}}} + + jr, _ := json.Marshal(&request) + + gock.New(crmURL). + Post(fmt.Sprintf("/integration-modules/%s/update-scopes", code)). + BodyString(string(jr[:])). + Reply(400). + BodyString(`{"success": false, "errorMsg": "Not enabled simple connection"}`) + + m, status, err := c.UpdateScopes(code, request) + if err == nil { + t.Error("Error must be return") + } + + if status != http.StatusBadRequest { + t.Errorf("%v", err) + } + + if m.Success != false { + t.Error(successFail) + } +} diff --git a/response.go b/response.go index 5f326a6..e1eb9a5 100644 --- a/response.go +++ b/response.go @@ -403,6 +403,12 @@ type IntegrationModuleResponse struct { IntegrationModule *IntegrationModule `json:"integrationModule,omitempty"` } +// UpdateScopesResponse update scopes response. +type UpdateScopesResponse struct { + ErrorResponse + APIKey string `json:"apiKey"` +} + // IntegrationModuleEditResponse type. type IntegrationModuleEditResponse struct { Success bool `json:"success"` diff --git a/types.go b/types.go index 1c7579b..928a5af 100644 --- a/types.go +++ b/types.go @@ -976,6 +976,15 @@ type IntegrationModule struct { Integrations *Integrations `json:"integrations,omitempty"` } +// UpdateScopesRequest type. +type UpdateScopesRequest struct { + Requires ScopesRequired `json:"requires"` +} + +type ScopesRequired struct { + Scopes []string +} + // Integrations type. type Integrations struct { Telephony *Telephony `json:"telephony,omitempty"`