github.com/schmorrison/Zoho@v1.1.4/recruit/filesandattachments.go (about)

     1  package recruit
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	zoho "github.com/schmorrison/Zoho"
     8  )
     9  
    10  // https://www.zoho.com/recruit/developer-guide/apiv2/upload-attachment.html
    11  func (c *API) UploadAttachment(
    12  	file string,
    13  	params map[string]zoho.Parameter,
    14  	module Module,
    15  	recordId string,
    16  ) (data UploadAttachmentResponse, err error) {
    17  	endpoint := zoho.Endpoint{
    18  		Name: "UploadAttachment",
    19  		URL: fmt.Sprintf(
    20  			"https://recruit.zoho.%s/recruit/v2/%s/%s/Attachments",
    21  			c.ZohoTLD,
    22  			module,
    23  			recordId,
    24  		),
    25  		Method:       zoho.HTTPPost,
    26  		ResponseData: &UploadAttachmentResponse{},
    27  		Attachment:   file,
    28  		BodyFormat:   zoho.FILE,
    29  		URLParameters: map[string]zoho.Parameter{
    30  			"attachments_category_id": "",
    31  			"attachments_category":    "",
    32  			"attachment_url":          "",
    33  		},
    34  	}
    35  
    36  	for k, v := range params {
    37  		endpoint.URLParameters[k] = v
    38  	}
    39  
    40  	err = c.Zoho.HTTPRequest(&endpoint)
    41  	if err != nil {
    42  		return UploadAttachmentResponse{}, fmt.Errorf("failed to upload Attachment: %s", err)
    43  	}
    44  
    45  	if v, ok := endpoint.ResponseData.(*UploadAttachmentResponse); ok {
    46  		return *v, nil
    47  	}
    48  
    49  	return UploadAttachmentResponse{}, fmt.Errorf("data returned was nil")
    50  }
    51  
    52  type UploadAttachmentResponse struct {
    53  	Data []struct {
    54  		Code    string `json:"code"`
    55  		Details struct {
    56  			ModifiedTime time.Time `json:"Modified_Time"`
    57  			ModifiedBy   struct {
    58  				Name string `json:"name"`
    59  				ID   string `json:"id"`
    60  			} `json:"Modified_By"`
    61  			CreatedTime time.Time `json:"Created_Time"`
    62  			ID          string    `json:"id"`
    63  			CreatedBy   struct {
    64  				Name string `json:"name"`
    65  				ID   string `json:"id"`
    66  			} `json:"Created_By"`
    67  		} `json:"details"`
    68  		Message string `json:"message"`
    69  		Status  string `json:"status"`
    70  	} `json:"data"`
    71  }