diff --git a/cmd/amocli/getcontact.go b/cmd/amocli/getcontact.go index 96672eb..41b65ab 100644 --- a/cmd/amocli/getcontact.go +++ b/cmd/amocli/getcontact.go @@ -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) }) diff --git a/contacts.go b/contacts.go index 469c217..9e4ad48 100644 --- a/contacts.go +++ b/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 { diff --git a/lead-tuple.go b/lead-tuple.go index 05e5fd0..f061c1b 100644 --- a/lead-tuple.go +++ b/lead-tuple.go @@ -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, diff --git a/scripts/inn-check.tengo b/scripts/inn-check.xgo similarity index 100% rename from scripts/inn-check.tengo rename to scripts/inn-check.xgo diff --git a/scripts/len-uniq-tuple-leads.tengo b/scripts/len-uniq-tuple-leads.xgo similarity index 100% rename from scripts/len-uniq-tuple-leads.tengo rename to scripts/len-uniq-tuple-leads.xgo diff --git a/scripts/len.tengo b/scripts/len.tengo deleted file mode 100644 index fbdd168..0000000 --- a/scripts/len.tengo +++ /dev/null @@ -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)) diff --git a/scripts/len.xgo b/scripts/len.xgo new file mode 100644 index 0000000..7f9ee6a --- /dev/null +++ b/scripts/len.xgo @@ -0,0 +1,15 @@ + +os := import("os") +fmt := import("fmt") +sjson := import("cjson") + +dec := cjson.new_decoder("") +//file_path := args[0] +n := 0 +for { + v := dec.decode() + if !v {break} + n++ + fmt.println(n) +} +fmt.println(n)