From f9704985047f582b620892c8edc554a5f068b83d Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 31 May 2024 01:45:36 +0500 Subject: [PATCH] feat: added getting for companies and contacts. --- cmd/amocli/getcom.go | 55 +++++++++++++++++++++++++++++++++++++++ cmd/amocli/getcontact.go | 56 ++++++++++++++++++++++++++++++++++++++++ cmd/amocli/main.go | 6 ++++- cmd/amocli/updatecom.go | 4 +-- companies.go | 32 +++++++++++++++++++++++ companies/companies.go | 15 +++++++++++ contacts.go | 33 +++++++++++++++++++++++ contacts/contacts.go | 20 ++++++++++++-- 8 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 cmd/amocli/getcom.go create mode 100644 cmd/amocli/getcontact.go diff --git a/cmd/amocli/getcom.go b/cmd/amocli/getcom.go new file mode 100644 index 0000000..70444b3 --- /dev/null +++ b/cmd/amocli/getcom.go @@ -0,0 +1,55 @@ +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" + +var getComs = mtool.T("get-coms").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) + } + + coms, err := c.GetCompanies( + urlenc.Array[int]{ + "id", + ids, + }, + ) + if err != nil { + log.Fatalf("GetCompanies(...): %s\n", err) + } + + bts, err := json.MarshalIndent(coms, "", " ") + if err != nil { + log.Fatalf("json.Marshal(...): %s\n", err) + } + fmt.Printf("%s\n", bts) +}) diff --git a/cmd/amocli/getcontact.go b/cmd/amocli/getcontact.go new file mode 100644 index 0000000..96672eb --- /dev/null +++ b/cmd/amocli/getcontact.go @@ -0,0 +1,56 @@ +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" + +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) +}) diff --git a/cmd/amocli/main.go b/cmd/amocli/main.go index f9f9791..7998eb8 100644 --- a/cmd/amocli/main.go +++ b/cmd/amocli/main.go @@ -9,5 +9,9 @@ func main() { var tool = mtool.T("amocli").Subs( getLead, - updateLead, + + getComs, + updateComs, + + getContacts, ) diff --git a/cmd/amocli/updatecom.go b/cmd/amocli/updatecom.go index 2a9d4f5..8c6d2fd 100644 --- a/cmd/amocli/updatecom.go +++ b/cmd/amocli/updatecom.go @@ -7,8 +7,8 @@ import "encoding/json" import "log" import "os" -var updateLead = -mtool.T("update-companies").Func(func(flags *mtool.Flags){ +var updateComs = +mtool.T("update-coms").Func(func(flags *mtool.Flags){ var ( secretPath string ) diff --git a/companies.go b/companies.go index df17c1e..7c86205 100644 --- a/companies.go +++ b/companies.go @@ -2,6 +2,8 @@ package amo import "surdeus.su/core/ss/urlenc" import "surdeus.su/core/amo/companies" +import "surdeus.su/core/amo/api" +import "errors" import "fmt" func (client *Client) GetCompany( @@ -21,6 +23,36 @@ func (client *Client) GetCompany( } return deal, nil } +// Get list of leads. +func (client *Client) GetCompanies( + opts ...urlenc.Builder, +) ([]companies.Company, error) { + res := fmt.Sprintf( + "/api/v4/companies?%s", + urlenc.Join(opts...).Encode(), + ) + + ret := []companies.Company{} + for { + var coms companies.Companies + err := client.API.Get(res, &coms) + if err != nil { + if errors.Is(err, api.ErrNoContent) { + break + } + return nil, err + } + + ret = append(ret, coms.Embedded.Companies...) + + if coms.Links.Next.Href == "" { + break + } + res = coms.Links.Next.Href + + } + return ret, nil +} func (client *Client) UpdateCompany( company *companies.Company, diff --git a/companies/companies.go b/companies/companies.go index 72fbe14..51960da 100644 --- a/companies/companies.go +++ b/companies/companies.go @@ -34,3 +34,18 @@ type Embedded struct { Tags []interface{} `json:"tags"` Contacts []*Contacts `json:"contacts"` } + +type Companies struct { + Page int `json:"_page"` + Links struct { + Self struct { + Href string `json:"href"` + } `json:"self"` + Next struct { + Href string `json:"href"` + } `json:"next"` + } `json:"_links"` + Embedded struct { + Companies []Company `json:"companies"` + } `json:"_embedded"` +} diff --git a/contacts.go b/contacts.go index 662c276..e0c2b7c 100644 --- a/contacts.go +++ b/contacts.go @@ -2,6 +2,8 @@ package amo import "surdeus.su/core/amo/contacts" import "surdeus.su/core/ss/urlenc" +import "errors" +import "surdeus.su/core/amo/api" import "fmt" func (client *Client) GetContact( @@ -21,6 +23,37 @@ func (client *Client) GetContact( } return deal, nil } +// Get list of contacts. +func (client *Client) GetContacts( + opts ...urlenc.Builder, +) ([]contacts.Contact, error) { + res := fmt.Sprintf( + "/api/v4/contacts?%s", + urlenc.Join(opts...).Encode(), + ) + + ret := []contacts.Contact{} + + for { + var cts contacts.Contacts + err := client.API.Get(res, &cts) + if err != nil { + if errors.Is(err, api.ErrNoContent) { + break + } + return nil, err + } + + ret = append(ret, cts.Embedded.Contacts...) + + if cts.Links.Next.Href == "" { + break + } + res = cts.Links.Next.Href + + } + return ret, nil +} func (client *Client) UpdateContact(contact *contacts.Contact) error { return client.updateEntity( diff --git a/contacts/contacts.go b/contacts/contacts.go index ef7424d..6e68ecc 100644 --- a/contacts/contacts.go +++ b/contacts/contacts.go @@ -43,7 +43,7 @@ type Customers struct { Links Links `json:"_links"` } -type Companies struct { +type Company struct { Id int `json:"id"` Links Links `json:"_links"` } @@ -53,5 +53,21 @@ type Embedded struct { Leads []Leads `json:"leads"` Customers []Customers `json:"customers"` CatalogElements []interface{} `json:"catalog_elements"` - Companies []Companies `json:"companies"` + Companies []Company `json:"companies"` } + +type Contacts struct { + Page int `json:"_page"` + Links struct { + Self struct { + Href string `json:"href"` + } `json:"self"` + Next struct { + Href string `json:"href"` + } `json:"next"` + } `json:"_links"` + Embedded struct { + Contacts []Contact `json:"contacts"` + } `json:"_embedded"` +} +