media.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 17:27:15 +0200
changeset 156 70aadba26338
parent 138 23d3a518d0ad
child 162 68df3a01e1a7
permissions -rw-r--r--
Add field "All" to LimitParams, change Limit behaviour If All is true, the library will send several requests (if needed) until the API server has sent all the results. If not, and if a Limit is set, the library will try to fetch at least this number of results.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     7
package madon
126
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"bytes"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"encoding/json"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"fmt"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"io"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"mime/multipart"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"os"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	"path/filepath"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	"github.com/sendgrid/rest"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
// UploadMedia uploads the given file and returns an attachment
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    22
func (mc *Client) UploadMedia(filePath string) (*Attachment, error) {
126
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	var b bytes.Buffer
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	if filePath == "" {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
		return nil, ErrInvalidParameter
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	f, err := os.Open(filePath)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	if err != nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
		return nil, fmt.Errorf("cannot read file: %s", err.Error())
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	defer f.Close()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	w := multipart.NewWriter(&b)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	formWriter, err := w.CreateFormFile("file", filepath.Base(filePath))
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	if err != nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
		return nil, fmt.Errorf("media upload: cannot create form: %s", err.Error())
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	if _, err = io.Copy(formWriter, f); err != nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		return nil, fmt.Errorf("media upload: cannot create form: %s", err.Error())
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	w.Close()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    46
	req, err := mc.prepareRequest("media", rest.Post, nil)
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 126
diff changeset
    47
	if err != nil {
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 126
diff changeset
    48
		return nil, fmt.Errorf("media prepareRequest failed: %s", err.Error())
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 126
diff changeset
    49
	}
126
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	req.Headers["Content-Type"] = w.FormDataContentType()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	req.Body = b.Bytes()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	// Make API call
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	r, err := restAPI(req)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	if err != nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		return nil, fmt.Errorf("media upload failed: %s", err.Error())
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
	// Check for error reply
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	var errorResult Error
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
		// The empty object is not an error
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
		if errorResult.Text != "" {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
			return nil, fmt.Errorf("%s", errorResult.Text)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
		}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
	// Not an error reply; let's unmarshal the data
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	var attachment Attachment
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	err = json.Unmarshal([]byte(r.Body), &attachment)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	if err != nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
		return nil, fmt.Errorf("cannot decode API response (media): %s", err.Error())
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	return &attachment, nil
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
}