package amo import ( "fmt" "time" "errors" "surdeus.su/core/amo/api" "surdeus.su/core/amo/companies" "surdeus.su/core/amo/contacts" "surdeus.su/core/amo/leads" "surdeus.su/core/amo/users" "surdeus.su/core/amo/events" ) type IAmoClient interface { GetUser(userId string) (*users.User, error) GetLead(leadId string, query string) (*leads.Lead, error) UpdateLead(lead *leads.Lead) error GetCompany(companyId string, query string) (*companies.Company, error) UpdateCompany(company *companies.Company) error GetContact(contactId string, query string) (*contacts.Contact, error) UpdateContact(contact *contacts.Contact) error } type TokenPair = api.TokenPair type Options = api.ClientOptions type Client struct { Api *api.Client } type OauthTokenResponse = api.OauthTokenResponse func NewAmoClient(secretPath string) (*Client, error) { apiClient, err := api.NewApi(secretPath) if err != nil { return nil, err } return &Client{ Api: apiClient, }, nil } func (client *Client) updateEntity(url string, id int, body interface{}) error { err := client.Api.Patch(fmt.Sprintf("%s/%d", url, id), body, nil) return err } func (client *Client) GetUser(userId int) (*users.User, error) { user := new(users.User) err := client.Api.Get(fmt.Sprintf("/api/v4/users/%d", userId), user) return user, err } func (client *Client) GetLead(leadId int, query string) (*leads.Lead, error) { deal := new(leads.Lead) resource := fmt.Sprintf("/api/v4/leads/%d", leadId) if query != "" { resource = resource + "?" + query } err := client.Api.Get(resource, deal) return deal, err } func (client *Client) UpdateLead(lead *leads.Lead) error { return client.updateEntity("/api/v4/leads", lead.Id, lead) } func (client *Client) GetCompany(companyId int, query string) (*companies.Company, error) { deal := new(companies.Company) resource := fmt.Sprintf("/api/v4/companies/%d", companyId) if query != "" { resource = resource + "?" + query } err := client.Api.Get(resource, deal) return deal, err } // Returns the events from AmoCRM by specified request. // If there are no such events returns an empty slice of events. func (client *Client) GetEvents(req events.EventsRequest) ([]events.Event, error) { res := "/api/v4/events" format := req.Format() if format != "" { res += "?" + format } var abs bool ret := []events.Event{} for { resp := events.EventsResponse{} err := client.Api.Get(res, &resp, abs) if err != nil { // Return empty if no content avialable. if errors.Is(err, api.NoContentErr) { return ret, nil } return nil, err } ret = append(ret, resp.Embedded.Events...) if resp.Links.Next.Href == "" { break } time.Sleep(time.Millisecond * 300) abs = true res = resp.Links.Next.Href } return ret, nil } func (client *Client) UpdateCompany(company *companies.Company) error { return client.updateEntity("/api/v4/companies", company.Id, company) } func (client *Client) GetContact(contactId int, query string) (*contacts.Contact, error) { deal := new(contacts.Contact) resource := fmt.Sprintf("/api/v4/contacts/%d", contactId) if query != "" { resource = resource + "?" + query } err := client.Api.Get(resource, deal) return deal, err } func (client *Client) UpdateContact(contact *contacts.Contact) error { return client.updateEntity("/api/v4/contacts", contact.Id, contact) }