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