github.com/schmorrison/Zoho@v1.1.4/invoice/create_items.go (about)

     1  package invoice
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  type CreateItemResponse struct {
    10  	Code    int    `json:"code"`
    11  	Message string `json:"message"`
    12  	Item    struct {
    13  		ItemID        string  `json:"item_id"`
    14  		Name          string  `json:"name"`
    15  		Status        string  `json:"status"`
    16  		Description   string  `json:"description"`
    17  		Rate          float64 `json:"rate"`
    18  		Unit          string  `json:"unit"`
    19  		TaxID         string  `json:"tax_id"`
    20  		TaxName       string  `json:"tax_name"`
    21  		TaxPercentage float64 `json:"tax_percentage"`
    22  		TaxType       string  `json:"tax_type"`
    23  		SKU           string  `json:"sku"`
    24  		ProductType   string  `json:"product_type"`
    25  	} `json:"item"`
    26  }
    27  
    28  type CreateItemRequest struct {
    29  	Name         string        `json:"name"`
    30  	Description  string        `json:"description"`
    31  	Rate         string        `json:"rate"`
    32  	TaxID        string        `json:"tax_id"`
    33  	CustomFields []interface{} `json:"custom_fields"`
    34  	ItemType     string        `json:"item_type"`
    35  	ProductType  string        `json:"product_type"`
    36  	Unit         string        `json:"unit"`
    37  }
    38  
    39  func (c *API) CreateItem(request CreateItemRequest) (data CreateItemResponse, err error) {
    40  	endpoint := zoho.Endpoint{
    41  		Name:         ItemsModule,
    42  		URL:          fmt.Sprintf("https://invoice.zoho.%s/api/v3/%s", c.ZohoTLD, ItemsModule),
    43  		Method:       zoho.HTTPPost,
    44  		ResponseData: &CreateItemResponse{},
    45  		RequestBody:  request,
    46  		BodyFormat:   zoho.JSON_STRING,
    47  		Headers: map[string]string{
    48  			InvoiceAPIEndpointHeader: c.OrganizationID,
    49  		},
    50  	}
    51  
    52  	if err = c.Zoho.HTTPRequest(&endpoint); err != nil {
    53  		return CreateItemResponse{}, fmt.Errorf("Failed to create item: %s", err)
    54  	}
    55  
    56  	if v, ok := endpoint.ResponseData.(*CreateItemResponse); ok {
    57  		// Check if the request succeeded
    58  		if v.Code != 0 {
    59  			return *v, fmt.Errorf("Failed to create item: %s", v.Message)
    60  		}
    61  		return *v, nil
    62  	}
    63  
    64  	return CreateItemResponse{}, fmt.Errorf("Data retrieved was not 'CreateItemResponse'")
    65  }