contacts.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package amo
  2. import "surdeus.su/core/amo/contacts"
  3. import "surdeus.su/core/ss/urlenc"
  4. import "errors"
  5. import "surdeus.su/core/amo/api"
  6. import "fmt"
  7. //import "log"
  8. func (client *Client) GetContact(
  9. contactID int,
  10. opts ...urlenc.Builder,
  11. ) (*contacts.Contact, error) {
  12. deal := new(contacts.Contact)
  13. res := fmt.Sprintf(
  14. "/api/v4/contacts/%d?%s",
  15. contactID,
  16. urlenc.Join(opts...).Encode(),
  17. )
  18. err := client.API.Get(res, deal)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return deal, nil
  23. }
  24. // Get list of contacts.
  25. func (client *Client) GetContacts(
  26. opts ...urlenc.Builder,
  27. ) ([]contacts.Contact, NextFunc[[]Contact], error) {
  28. res := fmt.Sprintf(
  29. "/api/v4/contacts?%s",
  30. urlenc.Join(opts...).Encode(),
  31. )
  32. return client.GetContactsByURL(res)
  33. }
  34. func (client *Client) GetContactsByURL(
  35. u string,
  36. ) ([]Contact, NextFunc[[]Contact], error) {
  37. var fn NextFunc[[]Contact]
  38. contacts := contacts.Contacts{}
  39. err := client.API.Get(u, &contacts)
  40. if err != nil {
  41. if errors.Is(err, api.ErrNoContent) {
  42. return nil, nil, nil
  43. }
  44. return nil, nil, err
  45. }
  46. //ret = append(ret, coms.Embedded.Companies...)
  47. nextHref := contacts.Links.Next.Href
  48. if nextHref != "" {
  49. fn = MakeNextFunc(
  50. nextHref,
  51. client.GetContactsByURL,
  52. )
  53. }
  54. return contacts.Embedded.Contacts, fn, nil
  55. }
  56. func (client *Client) UpdateContact(contact *contacts.Contact) error {
  57. return client.updateEntity(
  58. "/api/v4/contacts",
  59. contact.ID,
  60. contact,
  61. )
  62. }