Add update scopes method (#61)

* add update scopes method

* apiKey field

* example fix

Co-authored-by: Maria Tyschitskaya <tyschitskaya@retailcrm.ru>
This commit is contained in:
tishmaria90 2022-04-01 17:22:53 +03:00 committed by GitHub
parent 54810c21cd
commit 1309825638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 0 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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"`

View File

@ -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"`