github.com/subosito/twilio@v0.0.1/common.go (about)

     1  package twilio
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"time"
     8  )
     9  
    10  type Parameter interface {
    11  	urlValues() url.Values
    12  }
    13  
    14  // Exception holds information about error response returned by Twilio API
    15  type Exception struct {
    16  	Status   int    `json:"status"`
    17  	Message  string `json:"message"`
    18  	Code     int    `json:"code"`
    19  	MoreInfo string `json:"more_info"`
    20  }
    21  
    22  // Exception implements Error interface
    23  func (e *Exception) Error() string {
    24  	return fmt.Sprintf("%d: %s", e.Code, e.Message)
    25  }
    26  
    27  type Pagination struct {
    28  	Start           int    `json:"start"`
    29  	Total           int    `json:"total"`
    30  	NumPages        int    `json:"num_pages"`
    31  	Page            int    `json:"page"`
    32  	PageSize        int    `json:"page_size"`
    33  	End             int    `json:"end"`
    34  	Uri             string `json:"uri"`
    35  	FirstPageUri    string `json:"first_page_uri"`
    36  	LastPageUri     string `json:"last_page_uri"`
    37  	NextPageUri     string `json:"next_page_uri"`
    38  	PreviousPageUri string `json:"previous_page_uri"`
    39  }
    40  
    41  func unquote(b []byte) string {
    42  	switch b[0] {
    43  	case '"':
    44  		return string(b[1 : len(b)-1])
    45  	default:
    46  		return string(b)
    47  	}
    48  }
    49  
    50  type Price float32
    51  
    52  func (p *Price) UnmarshalJSON(b []byte) error {
    53  	return json.Unmarshal([]byte(unquote(b)), (*float32)(p))
    54  }
    55  
    56  type Timestamp time.Time
    57  
    58  func (m *Timestamp) UnmarshalJSON(b []byte) error {
    59  	s := unquote(b)
    60  
    61  	if s == "null" {
    62  		*m = Timestamp(time.Time{})
    63  		return nil
    64  	}
    65  
    66  	t, err := time.Parse(time.RFC1123Z, s)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	*m = Timestamp(t)
    72  	return nil
    73  }