71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package amo
|
|
|
|
import "surdeus.su/core/amo/api"
|
|
import "surdeus.su/core/amo/leads"
|
|
import "surdeus.su/core/ss/urlenc"
|
|
import "errors"
|
|
import "fmt"
|
|
//import "log"
|
|
|
|
// Get list of leads.
|
|
func (client *Client) GetLeads(
|
|
opts ...urlenc.Builder,
|
|
) ([]Lead, NextFunc[[]Lead], error) {
|
|
res := fmt.Sprintf(
|
|
"/api/v4/leads?%s",
|
|
urlenc.Join(opts...).Encode(),
|
|
)
|
|
|
|
return client.GetLeadsByURL(res)
|
|
}
|
|
|
|
func (client *Client) GetLeadsByURL(
|
|
u string,
|
|
) ([]Lead, NextFunc[[]Lead], error) {
|
|
var fn NextFunc[[]Lead]
|
|
|
|
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
|
|
}
|
|
// Some other error.
|
|
return nil, nil, err
|
|
}
|
|
|
|
nextHref := lds.Links.Next.Href
|
|
if nextHref != "" {
|
|
fn = MakeNextFunc(
|
|
nextHref,
|
|
client.GetLeadsByURL,
|
|
)
|
|
}
|
|
|
|
return lds.Embedded.Leads, fn, nil
|
|
|
|
}
|
|
|
|
// Get lead with the specified ID.
|
|
func (client *Client) GetLead(
|
|
leadID int,
|
|
opts ...urlenc.Builder,
|
|
) (*leads.Lead, error) {
|
|
deal := new(leads.Lead)
|
|
resource := fmt.Sprintf(
|
|
"/api/v4/leads/%d?%s",
|
|
leadID,
|
|
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)
|
|
}
|
|
|