amo/contacts.go

72 lines
1.4 KiB
Go

package amo
import "surdeus.su/core/amo/contacts"
import "surdeus.su/core/ss/urlenc"
import "errors"
import "surdeus.su/core/amo/api"
import "fmt"
//import "log"
func (client *Client) GetContact(
contactID int,
opts ...urlenc.Builder,
) (*contacts.Contact, error) {
deal := new(contacts.Contact)
res := fmt.Sprintf(
"/api/v4/contacts/%d?%s",
contactID,
urlenc.Join(opts...).Encode(),
)
err := client.API.Get(res, deal)
if err != nil {
return nil, err
}
return deal, nil
}
// Get list of contacts.
func (client *Client) GetContacts(
opts ...urlenc.Builder,
) ([]contacts.Contact, NextFunc[[]Contact], error) {
res := fmt.Sprintf(
"/api/v4/contacts?%s",
urlenc.Join(opts...).Encode(),
)
return client.GetContactsByURL(res)
}
func (client *Client) GetContactsByURL(
u string,
) ([]Contact, NextFunc[[]Contact], error) {
var fn NextFunc[[]Contact]
contacts := contacts.Contacts{}
err := client.API.Get(u, &contacts)
if err != nil {
if errors.Is(err, api.ErrNoContent) {
return nil, nil, nil
}
return nil, nil, err
}
//ret = append(ret, coms.Embedded.Companies...)
nextHref := contacts.Links.Next.Href
if nextHref != "" {
fn = MakeNextFunc(
nextHref,
client.GetContactsByURL,
)
}
return contacts.Embedded.Contacts, fn, nil
}
func (client *Client) UpdateContact(contact *contacts.Contact) error {
return client.updateEntity(
"/api/v4/contacts",
contact.ID,
contact,
)
}