amo/leads.go

72 lines
1.3 KiB
Go
Raw Normal View History

2024-05-30 11:43:10 +03:00
package amo
import "surdeus.su/core/amo/api"
import "surdeus.su/core/amo/leads"
import "surdeus.su/core/ss/urlenc"
import "errors"
import "fmt"
2024-06-04 23:16:12 +03:00
//import "log"
2024-05-30 11:43:10 +03:00
// Get list of leads.
func (client *Client) GetLeads(
opts ...urlenc.Builder,
2024-06-04 23:16:12 +03:00
) ([]Lead, NextFunc[[]Lead], error) {
2024-05-30 11:43:10 +03:00
res := fmt.Sprintf(
"/api/v4/leads?%s",
urlenc.Join(opts...).Encode(),
)
2024-06-04 23:16:12 +03:00
return client.GetLeadsByURL(res)
}
2024-05-30 11:43:10 +03:00
2024-06-04 23:16:12 +03:00
func (client *Client) GetLeadsByURL(
u string,
) ([]Lead, NextFunc[[]Lead], error) {
var fn NextFunc[[]Lead]
2024-05-30 11:43:10 +03:00
2024-06-04 23:16:12 +03:00
lds := leads.Leads{}
err := client.API.Get(u, &lds)
if err != nil {
// Check for empty.
if errors.Is(err, api.ErrNoContent) {
return nil, nil, nil
2024-05-30 11:43:10 +03:00
}
2024-06-04 23:16:12 +03:00
// Some other error.
return nil, nil, err
}
2024-05-30 11:43:10 +03:00
2024-06-04 23:16:12 +03:00
nextHref := lds.Links.Next.Href
if nextHref != "" {
fn = MakeNextFunc(
nextHref,
client.GetLeadsByURL,
)
2024-05-30 11:43:10 +03:00
}
2024-06-04 23:16:12 +03:00
return lds.Embedded.Leads, fn, nil
2024-05-30 11:43:10 +03:00
}
// Get lead with the specified ID.
func (client *Client) GetLead(
leadID int,
2024-05-30 11:43:10 +03:00
opts ...urlenc.Builder,
) (*leads.Lead, error) {
deal := new(leads.Lead)
resource := fmt.Sprintf(
"/api/v4/leads/%d?%s",
leadID,
2024-05-30 11:43:10 +03:00
urlenc.Join(opts...).Encode(),
)
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)
2024-05-30 11:43:10 +03:00
}