Skip to content

Commit

Permalink
bug: fixes issues exporting more than 1000 products #611
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 22, 2024
1 parent 42bc042 commit 05f50b4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
60 changes: 47 additions & 13 deletions internal/client/products/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,9 @@ func Export(conn int) (payload [][]byte, err error) {
// parent workgroup
var pwg sync.WaitGroup
var mu sync.Mutex
const entityType = "apiproducts"

u, _ := url.Parse(apiclient.GetApigeeBaseURL())
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), entityType)
// don't print to sysout
apiclient.ClientPrintHttpResponse.Set(false)
respBody, err := apiclient.HttpClient(u.String())
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
if err != nil {
return apiclient.GetEntityPayloadList(), err
}

products := apiProducts{}
err = json.Unmarshal(respBody, &products)
entityType := "apiproducts"
products, err := listAllProducts()
if err != nil {
return apiclient.GetEntityPayloadList(), err
}
Expand Down Expand Up @@ -471,3 +460,48 @@ func readProductsFile(filePath string) ([]APIProduct, error) {

return products, nil
}

func listAllProducts() (products apiProducts, err error) {
var startKey string
products = apiProducts{}

u, _ := url.Parse(apiclient.GetApigeeBaseURL())
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apiproducts")

// don't print to sysout
apiclient.ClientPrintHttpResponse.Set(false)

for {

p := apiProducts{}

if startKey != "" {
q := u.Query()
q.Set("startKey", startKey)
q.Set("count", "1000")
u.RawQuery = q.Encode()
}

respBody, err := apiclient.HttpClient(u.String())
startKey = ""
if err != nil {
return products, err
}

err = json.Unmarshal(respBody, &p)
if err != nil {
return products, err
}

products.APIProduct = append(products.APIProduct, p.APIProduct...)

if len(p.APIProduct) == 1000 {
startKey = p.APIProduct[len(p.APIProduct)-1].Name
} else if len(p.APIProduct) < 1000 {
break
}
}

apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
return products, nil
}
1 change: 1 addition & 0 deletions internal/cmd/products/expprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var ExpCmd = &cobra.Command{
cmd.SilenceUsage = true

const exportFileName = "products.json"
apiclient.DisableCmdPrintHttpResponse()
payload, err := products.Export(conn)
if err != nil {
return err
Expand Down

0 comments on commit 05f50b4

Please sign in to comment.