model.go raw

   1  package base
   2  
   3  import (
   4  	"fmt"
   5  	"io"
   6  	"net/http"
   7  	"net/textproto"
   8  	"net/url"
   9  	"strings"
  10  	"time"
  11  )
  12  
  13  const (
  14  	RegionCnNorth1    = "cn-north-1"
  15  	RegionUsEast1     = "us-east-1"
  16  	RegionApSingapore = "ap-singapore-1"
  17  
  18  	RegionApSouthEast1 = "ap-southeast-1"
  19  
  20  	timeFormatV4 = "20060102T150405Z"
  21  )
  22  
  23  type ServiceInfo struct {
  24  	Timeout     time.Duration
  25  	Scheme      string
  26  	Host        string
  27  	Header      http.Header
  28  	Credentials Credentials
  29  	Retry       RetrySettings
  30  }
  31  
  32  type ApiInfo struct {
  33  	Method  string
  34  	Path    string
  35  	Query   url.Values
  36  	Form    url.Values
  37  	Timeout time.Duration
  38  	Header  http.Header
  39  	Retry   RetrySettings
  40  }
  41  
  42  type Credentials struct {
  43  	AccessKeyID     string
  44  	SecretAccessKey string
  45  	Service         string
  46  	Region          string
  47  	SessionToken    string
  48  }
  49  
  50  type metadata struct {
  51  	algorithm       string
  52  	credentialScope string
  53  	signedHeaders   string
  54  	date            string
  55  	region          string
  56  	service         string
  57  }
  58  
  59  // Unified JSON return results
  60  type CommonResponse struct {
  61  	ResponseMetadata ResponseMetadata
  62  	Result           interface{} `json:"Result,omitempty"`
  63  }
  64  
  65  type BaseResp struct {
  66  	Status      string
  67  	CreatedTime int64
  68  	UpdatedTime int64
  69  }
  70  
  71  type ErrorObj struct {
  72  	CodeN   int
  73  	Code    string
  74  	Message string
  75  }
  76  
  77  type ResponseMetadata struct {
  78  	RequestId string
  79  	Service   string    `json:",omitempty"`
  80  	Region    string    `json:",omitempty"`
  81  	Action    string    `json:",omitempty"`
  82  	Version   string    `json:",omitempty"`
  83  	Error     *ErrorObj `json:",omitempty"`
  84  }
  85  
  86  type Policy struct {
  87  	Statement []*Statement
  88  }
  89  
  90  const (
  91  	StatementEffectAllow = "Allow"
  92  	StatementEffectDeny  = "Deny"
  93  )
  94  
  95  type Statement struct {
  96  	Effect    string
  97  	Action    []string
  98  	Resource  []string
  99  	Condition string `json:",omitempty"`
 100  }
 101  
 102  type SecurityToken2 struct {
 103  	AccessKeyID     string
 104  	SecretAccessKey string
 105  	SessionToken    string
 106  	ExpiredTime     string
 107  	CurrentTime     string
 108  }
 109  
 110  type InnerToken struct {
 111  	LTAccessKeyId         string
 112  	AccessKeyId           string
 113  	SignedSecretAccessKey string
 114  	ExpiredTime           int64
 115  	PolicyString          string
 116  	Signature             string
 117  }
 118  
 119  type RetrySettings struct {
 120  	AutoRetry     bool
 121  	RetryTimes    *uint64
 122  	RetryInterval *time.Duration
 123  }
 124  
 125  type RequestParam struct {
 126  	IsSignUrl bool
 127  	Body      []byte
 128  	Method    string
 129  	Date      time.Time
 130  	Path      string
 131  	Host      string
 132  	QueryList url.Values
 133  	Headers   http.Header
 134  }
 135  
 136  type SignRequest struct {
 137  	XDate          string
 138  	XNotSignBody   string
 139  	XCredential    string
 140  	XAlgorithm     string
 141  	XSignedHeaders string
 142  	XSignedQueries string
 143  	XSignature     string
 144  	XSecurityToken string
 145  
 146  	Host           string
 147  	ContentType    string
 148  	XContentSha256 string
 149  	Authorization  string
 150  }
 151  
 152  var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
 153  
 154  func escapeQuotes(s string) string {
 155  	return quoteEscaper.Replace(s)
 156  }
 157  
 158  type MultiPartItem struct {
 159  	header textproto.MIMEHeader
 160  	data   io.Reader
 161  }
 162  
 163  func CreateMultiPartItem(header textproto.MIMEHeader, data io.Reader) *MultiPartItem {
 164  	return &MultiPartItem{
 165  		header: header,
 166  		data:   data,
 167  	}
 168  }
 169  
 170  func CreateMultiPartItemFormFile(fieldname, filename string, data io.Reader) *MultiPartItem {
 171  	h := make(textproto.MIMEHeader)
 172  	h.Set("Content-Disposition",
 173  		fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
 174  			escapeQuotes(fieldname), escapeQuotes(filename)))
 175  	h.Set("Content-Type", "application/octet-stream")
 176  	return CreateMultiPartItem(h, data)
 177  }
 178  
 179  func CreateMultiPartItemFormField(fieldname string, data string) *MultiPartItem {
 180  	h := make(textproto.MIMEHeader)
 181  	h.Set("Content-Disposition",
 182  		fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldname)))
 183  	return CreateMultiPartItem(h, strings.NewReader(data))
 184  }
 185