leads.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package amo
  2. import "surdeus.su/core/amo/api"
  3. import "surdeus.su/core/amo/leads"
  4. import "surdeus.su/core/ss/urlenc"
  5. import "errors"
  6. import "fmt"
  7. //import "log"
  8. // Get list of leads.
  9. func (client *Client) GetLeads(
  10. opts ...urlenc.Builder,
  11. ) ([]Lead, NextFunc[[]Lead], error) {
  12. res := fmt.Sprintf(
  13. "/api/v4/leads?%s",
  14. urlenc.Join(opts...).Encode(),
  15. )
  16. return client.GetLeadsByURL(res)
  17. }
  18. func (client *Client) GetLeadsByURL(
  19. u string,
  20. ) ([]Lead, NextFunc[[]Lead], error) {
  21. var fn NextFunc[[]Lead]
  22. lds := leads.Leads{}
  23. err := client.API.Get(u, &lds)
  24. if err != nil {
  25. // Check for empty.
  26. if errors.Is(err, api.ErrNoContent) {
  27. return nil, nil, nil
  28. }
  29. // Some other error.
  30. return nil, nil, err
  31. }
  32. nextHref := lds.Links.Next.Href
  33. if nextHref != "" {
  34. fn = MakeNextFunc(
  35. nextHref,
  36. client.GetLeadsByURL,
  37. )
  38. }
  39. return lds.Embedded.Leads, fn, nil
  40. }
  41. // Get lead with the specified ID.
  42. func (client *Client) GetLead(
  43. leadID int,
  44. opts ...urlenc.Builder,
  45. ) (*leads.Lead, error) {
  46. deal := new(leads.Lead)
  47. resource := fmt.Sprintf(
  48. "/api/v4/leads/%d?%s",
  49. leadID,
  50. urlenc.Join(opts...).Encode(),
  51. )
  52. err := client.API.Get(resource, deal)
  53. return deal, err
  54. }
  55. func (client *Client) UpdateLead(
  56. lead *leads.Lead,
  57. ) error {
  58. return client.updateEntity("/api/v4/leads", lead.ID, lead)
  59. }