media.go
changeset 215 40ed4e2db3b9
parent 207 301d5b94be3f
child 216 403ce36a94ab
equal deleted inserted replaced
214:642d690de0ba 215:40ed4e2db3b9
    17 	"github.com/pkg/errors"
    17 	"github.com/pkg/errors"
    18 	"github.com/sendgrid/rest"
    18 	"github.com/sendgrid/rest"
    19 )
    19 )
    20 
    20 
    21 // UploadMedia uploads the given file and returns an attachment
    21 // UploadMedia uploads the given file and returns an attachment
    22 func (mc *Client) UploadMedia(filePath string) (*Attachment, error) {
    22 // The description and focus arguments can be empty strings.
       
    23 // 'focus' is the "focal point", written as two comma-delimited floating points.
       
    24 func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) {
    23 	var b bytes.Buffer
    25 	var b bytes.Buffer
    24 
    26 
    25 	if filePath == "" {
    27 	if filePath == "" {
    26 		return nil, ErrInvalidParameter
    28 		return nil, ErrInvalidParameter
    27 	}
    29 	}
    41 		return nil, errors.Wrap(err, "media upload")
    43 		return nil, errors.Wrap(err, "media upload")
    42 	}
    44 	}
    43 
    45 
    44 	w.Close()
    46 	w.Close()
    45 
    47 
    46 	req, err := mc.prepareRequest("media", rest.Post, nil)
    48 	var params apiCallParams
       
    49 	if description != "" || focus != "" {
       
    50 		params = make(apiCallParams)
       
    51 		if description != "" {
       
    52 			params["description"] = description
       
    53 		}
       
    54 		if focus != "" {
       
    55 			params["focus"] = focus
       
    56 		}
       
    57 	}
       
    58 
       
    59 	req, err := mc.prepareRequest("media", rest.Post, params)
    47 	if err != nil {
    60 	if err != nil {
    48 		return nil, errors.Wrap(err, "media prepareRequest failed")
    61 		return nil, errors.Wrap(err, "media prepareRequest failed")
    49 	}
    62 	}
    50 	req.Headers["Content-Type"] = w.FormDataContentType()
    63 	req.Headers["Content-Type"] = w.FormDataContentType()
    51 	req.Body = b.Bytes()
    64 	req.Body = b.Bytes()