package goctk import ( "encoding/json" ) // Product defines the struct returned from the Product.Product query in CTK // See: https://ws.core.rackspace.com/ctkapi/browse/Product/Product type Product struct { Category string `json:"category" bson:"category"` Sku int `json:"sku" bson:"sku"` Description string `json:"description" bson:"description"` Name string `json:"name" bson:"name"` ActiveDate string `json:"active_date" bson:"active_date"` SkuAttributeTypeName string `json:"sku_attribute_type.name" bson:"sku_attribute_type_name"` InactiveDate string `json:"inactive_date" bson:"inactive_date"` Active int `json:"active" bson:"active"` ItemQuantity int `json:"item_quantity" bson:"item_quantity"` ExternalURL string `json:"external_url" bson:"external_url"` ID int `json:"id" bson:"id"` Icon string `json:"icon" bson:"icon"` } // A is used as a wrapper for the unknown types of load_arg options that we could pass in. type A map[string]interface{} func (c *Client) ProductGet(loadArg A) ([]Product, error) { var ( // Set a boolean to catch the last iteration when // the returned result is less than max. last = false // Base offset to start with offset = 0 // Make the response var products = make([]Product, 0) ) type ( ctkRes struct { Count int `json:"count"` LoadArg A `json:"load_arg"` Limit int `json:"limit"` Result []Product `json:"result"` Offset int `json:"offset"` Class string `json:"class"` } ) for { loadArg["offset"] = offset t := map[string]interface{}{ "class": "Product.Product", "load_arg": loadArg, "load_method": "loadList", "attributes": []string{ "active", "active_date", "category", "description", "external_url", "icon", "id", "inactive_date", "item_quantity", "name", "sku", "sku_attribute_type.name", }, } js, err := json.Marshal(&t) if err != nil { return nil, err } res, err := c.Query(js) if err != nil { return nil, err } var r ctkRes if err = json.Unmarshal(res, &[]interface{}{&r}); err != nil { return nil, err } products = append(products, r.Result...) // Increment the offset to the next iteration offset += 250 // If the returned count is less than the max returned, // then that means we are on our last iteration. if r.Count < 250 { last = true } // If the last flag was set, break out of the loop if last { break } } return products, nil }