feat: added the general way to get contacts.

This commit is contained in:
Andrey Parhomenko 2024-08-02 00:16:26 +05:00
parent 5177e03bef
commit 3662d08e56
7 changed files with 58 additions and 77 deletions

View file

@ -3,54 +3,24 @@ package main
import "surdeus.su/core/amo" import "surdeus.su/core/amo"
import "surdeus.su/core/ss/urlenc" import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/cli/mtool" import "surdeus.su/core/cli/mtool"
import "encoding/json"
import "strconv" type ContactGetter struct{}
import "log" func (l ContactGetter) GetValues(
import "fmt" c *amo.Client,
//import "os" 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"). var getContacts = mtool.T("get-contacts").
Func(func(flags *mtool.Flags){ Func(func(flags *mtool.Flags){
var ( RunGetter(ContactGetter{}, flags)
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)
}) })

View file

@ -27,33 +27,39 @@ func (client *Client) GetContact(
// Get list of contacts. // Get list of contacts.
func (client *Client) GetContacts( func (client *Client) GetContacts(
opts ...urlenc.Builder, opts ...urlenc.Builder,
) ([]contacts.Contact, error) { ) ([]contacts.Contact, NextFunc[[]Contact], error) {
res := fmt.Sprintf( res := fmt.Sprintf(
"/api/v4/contacts?%s", "/api/v4/contacts?%s",
urlenc.Join(opts...).Encode(), urlenc.Join(opts...).Encode(),
) )
ret := []contacts.Contact{} return client.GetContactsByURL(res)
}
for { func (client *Client) GetContactsByURL(
var cts contacts.Contacts u string,
err := client.API.Get(res, &cts) ) ([]Contact, NextFunc[[]Contact], error) {
if err != nil { var fn NextFunc[[]Contact]
if errors.Is(err, api.ErrNoContent) {
break contacts := contacts.Contacts{}
} err := client.API.Get(u, &contacts)
return nil, err if err != nil {
if errors.Is(err, api.ErrNoContent) {
return nil, nil, nil
} }
return nil, nil, err
ret = append(ret, cts.Embedded.Contacts...)
if cts.Links.Next.Href == "" {
break
}
res = cts.Links.Next.Href
} }
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 { func (client *Client) UpdateContact(contact *contacts.Contact) error {

View file

@ -90,7 +90,7 @@ func (client *Client) getLeadTuplesByFuncURL(
contactMap := map[int] *Contact{} contactMap := map[int] *Contact{}
var contacts []Contact var contacts []Contact
if len(contactIDs) > 0 { if len(contactIDs) > 0 {
contacts, err = client.GetContacts( contacts, _, err = client.GetContacts(
urlenc.Array[int]{ urlenc.Array[int]{
"id", "id",
contactIDs, contactIDs,

View file

@ -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
View 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)