account.go
changeset 162 68df3a01e1a7
parent 161 6786f169b59f
child 183 cd5aa242c01f
equal deleted inserted replaced
161:6786f169b59f 162:68df3a01e1a7
    15 	"net/http"
    15 	"net/http"
    16 	"os"
    16 	"os"
    17 	"strconv"
    17 	"strconv"
    18 	"strings"
    18 	"strings"
    19 
    19 
       
    20 	"github.com/pkg/errors"
    20 	"github.com/sendgrid/rest"
    21 	"github.com/sendgrid/rest"
    21 )
    22 )
    22 
    23 
    23 // getAccountsOptions contains option fields for POST and DELETE API calls
    24 // getAccountsOptions contains option fields for POST and DELETE API calls
    24 type getAccountsOptions struct {
    25 type getAccountsOptions struct {
   388 	w.Close()
   389 	w.Close()
   389 
   390 
   390 	// Prepare the request
   391 	// Prepare the request
   391 	req, err := mc.prepareRequest(endPoint, rest.Patch, params)
   392 	req, err := mc.prepareRequest(endPoint, rest.Patch, params)
   392 	if err != nil {
   393 	if err != nil {
   393 		return nil, fmt.Errorf("prepareRequest failed: %s", err.Error())
   394 		return nil, errors.Wrap(err, "prepareRequest failed")
   394 	}
   395 	}
   395 	req.Headers["Content-Type"] = w.FormDataContentType()
   396 	req.Headers["Content-Type"] = w.FormDataContentType()
   396 	req.Body = formBuf.Bytes()
   397 	req.Body = formBuf.Bytes()
   397 
   398 
   398 	// Make API call
   399 	// Make API call
   399 	r, err := restAPI(req)
   400 	r, err := restAPI(req)
   400 	if err != nil {
   401 	if err != nil {
   401 		return nil, fmt.Errorf("account update failed: %s", err.Error())
   402 		return nil, errors.Wrap(err, "account update failed")
   402 	}
   403 	}
   403 
   404 
   404 	// Check for error reply
   405 	// Check for error reply
   405 	var errorResult Error
   406 	var errorResult Error
   406 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
   407 	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
   407 		// The empty object is not an error
   408 		// The empty object is not an error
   408 		if errorResult.Text != "" {
   409 		if errorResult.Text != "" {
   409 			return nil, fmt.Errorf("%s", errorResult.Text)
   410 			return nil, errors.New(errorResult.Text)
   410 		}
   411 		}
   411 	}
   412 	}
   412 
   413 
   413 	// Not an error reply; let's unmarshal the data
   414 	// Not an error reply; let's unmarshal the data
   414 	var account Account
   415 	var account Account
   415 	if err := json.Unmarshal([]byte(r.Body), &account); err != nil {
   416 	if err := json.Unmarshal([]byte(r.Body), &account); err != nil {
   416 		return nil, fmt.Errorf("cannot decode API response: %s", err.Error())
   417 		return nil, errors.Wrap(err, "cannot decode API response")
   417 	}
   418 	}
   418 	return &account, nil
   419 	return &account, nil
   419 }
   420 }
   420 
   421 
   421 // fileToBase64 is a helper function to convert a file's contents to
   422 // fileToBase64 is a helper function to convert a file's contents to