feat: added the general way to get contacts.
This commit is contained in:
parent
5177e03bef
commit
3662d08e56
7 changed files with 58 additions and 77 deletions
|
@ -3,54 +3,24 @@ package main
|
|||
import "surdeus.su/core/amo"
|
||||
import "surdeus.su/core/ss/urlenc"
|
||||
import "surdeus.su/core/cli/mtool"
|
||||
import "encoding/json"
|
||||
import "strconv"
|
||||
import "log"
|
||||
import "fmt"
|
||||
//import "os"
|
||||
|
||||
type ContactGetter struct{}
|
||||
func (l ContactGetter) GetValues(
|
||||
c *amo.Client,
|
||||
opts ...urlenc.Builder,
|
||||
) ([]amo.Contact, amo.NextFunc[[]amo.Contact], error) {
|
||||
return c.GetContacts(opts...)
|
||||
}
|
||||
|
||||
func (l ContactGetter) GetNameMul() string {
|
||||
return "companies"
|
||||
}
|
||||
|
||||
func (l ContactGetter) GetFuncName() string {
|
||||
return "amo.GetCompanies"
|
||||
}
|
||||
|
||||
var getContacts = mtool.T("get-contacts").
|
||||
Func(func(flags *mtool.Flags){
|
||||
var (
|
||||
secretPath string
|
||||
)
|
||||
|
||||
flags.StringVar(
|
||||
&secretPath,
|
||||
"secret",
|
||||
"",
|
||||
"path to JSON file with AMO CRM secrets",
|
||||
"AMO_SECRET",
|
||||
)
|
||||
idStrs := flags.Parse()
|
||||
ids := make([]int, len(idStrs))
|
||||
for i, idStr := range idStrs {
|
||||
var err error
|
||||
ids[i], err = strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
log.Printf("Error: Atoi(%q): %s\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c, err := amo.NewClient(secretPath)
|
||||
if err != nil {
|
||||
log.Fatalf("NewAmoClient(...): %s\n", err)
|
||||
}
|
||||
|
||||
contacts, err := c.GetContacts(
|
||||
urlenc.Array[int]{
|
||||
"id",
|
||||
ids,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("GetContacts(...): %s\n", err)
|
||||
}
|
||||
|
||||
bts, err := json.MarshalIndent(contacts, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("json.Marshal(...): %s\n", err)
|
||||
}
|
||||
fmt.Printf("%s\n", bts)
|
||||
RunGetter(ContactGetter{}, flags)
|
||||
})
|
||||
|
|
44
contacts.go
44
contacts.go
|
@ -27,33 +27,39 @@ func (client *Client) GetContact(
|
|||
// Get list of contacts.
|
||||
func (client *Client) GetContacts(
|
||||
opts ...urlenc.Builder,
|
||||
) ([]contacts.Contact, error) {
|
||||
) ([]contacts.Contact, NextFunc[[]Contact], error) {
|
||||
res := fmt.Sprintf(
|
||||
"/api/v4/contacts?%s",
|
||||
urlenc.Join(opts...).Encode(),
|
||||
)
|
||||
|
||||
ret := []contacts.Contact{}
|
||||
return client.GetContactsByURL(res)
|
||||
}
|
||||
|
||||
for {
|
||||
var cts contacts.Contacts
|
||||
err := client.API.Get(res, &cts)
|
||||
if err != nil {
|
||||
if errors.Is(err, api.ErrNoContent) {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
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
|
||||
}
|
||||
|
||||
ret = append(ret, cts.Embedded.Contacts...)
|
||||
|
||||
if cts.Links.Next.Href == "" {
|
||||
break
|
||||
}
|
||||
res = cts.Links.Next.Href
|
||||
|
||||
return nil, nil, err
|
||||
}
|
||||
return ret, nil
|
||||
|
||||
//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 {
|
||||
|
|
|
@ -90,7 +90,7 @@ func (client *Client) getLeadTuplesByFuncURL(
|
|||
contactMap := map[int] *Contact{}
|
||||
var contacts []Contact
|
||||
if len(contactIDs) > 0 {
|
||||
contacts, err = client.GetContacts(
|
||||
contacts, _, err = client.GetContacts(
|
||||
urlenc.Array[int]{
|
||||
"id",
|
||||
contactIDs,
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
|
||||
os := import("os")
|
||||
fmt := import("fmt")
|
||||
json := import("json")
|
||||
|
||||
args := os.args()[2:]
|
||||
file_path := args[0]
|
||||
|
||||
arr := json.decode(os.read_file(file_path))
|
||||
fmt.println(len(arr))
|
15
scripts/len.xgo
Normal file
15
scripts/len.xgo
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
os := import("os")
|
||||
fmt := import("fmt")
|
||||
sjson := import("cjson")
|
||||
|
||||
dec := cjson.new_decoder("<stdin>")
|
||||
//file_path := args[0]
|
||||
n := 0
|
||||
for {
|
||||
v := dec.decode()
|
||||
if !v {break}
|
||||
n++
|
||||
fmt.println(n)
|
||||
}
|
||||
fmt.println(n)
|
Loading…
Reference in a new issue