vendor/github.com/sendgrid/rest/rest.go
changeset 256 6d9efbef00a9
parent 242 2a9ec03fe5a1
child 260 445e01aede7e
equal deleted inserted replaced
255:4f153a23adab 256:6d9efbef00a9
     1 // Package rest allows for quick and easy access any REST or REST-like API.
     1 // Package rest allows for quick and easy access any REST or REST-like API.
     2 package rest
     2 package rest
     3 
     3 
     4 import (
     4 import (
     5 	"bytes"
     5 	"bytes"
       
     6 	"context"
     6 	"io/ioutil"
     7 	"io/ioutil"
     7 	"net/http"
     8 	"net/http"
     8 	"net/url"
     9 	"net/url"
     9 )
    10 )
       
    11 
       
    12 // Version represents the current version of the rest library
       
    13 const Version = "2.6.4"
    10 
    14 
    11 // Method contains the supported HTTP verbs.
    15 // Method contains the supported HTTP verbs.
    12 type Method string
    16 type Method string
    13 
    17 
    14 // Supported HTTP verbs.
    18 // Supported HTTP verbs.
    38 func (e *RestError) Error() string {
    42 func (e *RestError) Error() string {
    39 	return e.Response.Body
    43 	return e.Response.Body
    40 }
    44 }
    41 
    45 
    42 // DefaultClient is used if no custom HTTP client is defined
    46 // DefaultClient is used if no custom HTTP client is defined
    43 var DefaultClient = &Client{HTTPClient: http.DefaultClient}
    47 var DefaultClient = &Client{HTTPClient: &http.Client{}}
    44 
    48 
    45 // Client allows modification of client headers, redirect policy
    49 // Client allows modification of client headers, redirect policy
    46 // and other settings
    50 // and other settings
    47 // See https://golang.org/pkg/net/http
    51 // See https://golang.org/pkg/net/http
    48 type Client struct {
    52 type Client struct {
    92 }
    96 }
    93 
    97 
    94 // BuildResponse builds the response struct.
    98 // BuildResponse builds the response struct.
    95 func BuildResponse(res *http.Response) (*Response, error) {
    99 func BuildResponse(res *http.Response) (*Response, error) {
    96 	body, err := ioutil.ReadAll(res.Body)
   100 	body, err := ioutil.ReadAll(res.Body)
    97 	if err != nil {
       
    98 		return nil, err
       
    99 	}
       
   100 	defer func() {
       
   101 		if err := res.Body.Close(); err != nil {
       
   102 			return // maybe log in the future
       
   103 		}
       
   104 	}()
       
   105 	response := Response{
   101 	response := Response{
   106 		StatusCode: res.StatusCode,
   102 		StatusCode: res.StatusCode,
   107 		Body:       string(body),
   103 		Body:       string(body),
   108 		Headers:    res.Header,
   104 		Headers:    res.Header,
   109 	}
   105 	}
   110 	return &response, nil
   106 	res.Body.Close() // nolint
       
   107 	return &response, err
   111 }
   108 }
   112 
   109 
   113 // API supports old implementation (deprecated)
   110 // Deprecated: API supports old implementation
   114 func API(request Request) (*Response, error) {
   111 func API(request Request) (*Response, error) {
   115 	return Send(request)
   112 	return Send(request)
   116 }
   113 }
   117 
   114 
   118 // Send uses the DefaultClient to send your request
   115 // Send uses the DefaultClient to send your request
   119 func Send(request Request) (*Response, error) {
   116 func Send(request Request) (*Response, error) {
   120 	return DefaultClient.Send(request)
   117 	return SendWithContext(context.Background(), request)
       
   118 }
       
   119 
       
   120 // SendWithContext uses the DefaultClient to send your request with the provided context.
       
   121 func SendWithContext(ctx context.Context, request Request) (*Response, error) {
       
   122 	return DefaultClient.SendWithContext(ctx, request)
   121 }
   123 }
   122 
   124 
   123 // The following functions enable the ability to define a
   125 // The following functions enable the ability to define a
   124 // custom HTTP Client
   126 // custom HTTP Client
   125 
   127 
   126 // MakeRequest makes the API call.
   128 // MakeRequest makes the API call.
   127 func (c *Client) MakeRequest(req *http.Request) (*http.Response, error) {
   129 func (c *Client) MakeRequest(req *http.Request) (*http.Response, error) {
   128 	return c.HTTPClient.Do(req)
   130 	return c.HTTPClient.Do(req)
   129 }
   131 }
   130 
   132 
   131 // API supports old implementation (deprecated)
   133 // Deprecated: API supports old implementation
   132 func (c *Client) API(request Request) (*Response, error) {
   134 func (c *Client) API(request Request) (*Response, error) {
   133 	return c.Send(request)
   135 	return c.Send(request)
   134 }
   136 }
   135 
   137 
   136 // Send will build your request, make the request, and build your response.
   138 // Send will build your request, make the request, and build your response.
   137 func (c *Client) Send(request Request) (*Response, error) {
   139 func (c *Client) Send(request Request) (*Response, error) {
       
   140 	return c.SendWithContext(context.Background(), request)
       
   141 }
       
   142 
       
   143 // SendWithContext will build your request passing in the provided context, make the request, and build your response.
       
   144 func (c *Client) SendWithContext(ctx context.Context, request Request) (*Response, error) {
   138 	// Build the HTTP request object.
   145 	// Build the HTTP request object.
   139 	req, err := BuildRequestObject(request)
   146 	req, err := BuildRequestObject(request)
   140 	if err != nil {
   147 	if err != nil {
   141 		return nil, err
   148 		return nil, err
   142 	}
   149 	}
       
   150 	// Pass in the user provided context
       
   151 	req = req.WithContext(ctx)
   143 
   152 
   144 	// Build the HTTP client and make the request.
   153 	// Build the HTTP client and make the request.
   145 	res, err := c.MakeRequest(req)
   154 	res, err := c.MakeRequest(req)
   146 	if err != nil {
   155 	if err != nil {
   147 		return nil, err
   156 		return nil, err
   148 	}
   157 	}
   149 
   158 
   150 	// Build Response object.
   159 	// Build Response object.
   151 	response, err := BuildResponse(res)
   160 	return BuildResponse(res)
   152 	if err != nil {
       
   153 		return nil, err
       
   154 	}
       
   155 
       
   156 	return response, nil
       
   157 }
   161 }