request.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package aws
  2. import (
  3. "bytes"
  4. "github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
  5. "github.com/ks3sdklib/aws-sdk-go/internal/crc"
  6. "hash"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "reflect"
  11. "strings"
  12. "time"
  13. )
  14. // A Request is the service request to be made.
  15. type Request struct {
  16. *Service
  17. Handlers Handlers
  18. Time time.Time
  19. ExpireTime int64
  20. Operation *Operation
  21. HTTPRequest *http.Request
  22. HTTPResponse *http.Response
  23. Body io.ReadSeeker
  24. bodyStart int64 // offset from beginning of Body that the request body starts
  25. Params interface{}
  26. Error error
  27. Data interface{}
  28. RequestID string
  29. RetryCount uint
  30. Retryable SettableBool
  31. RetryDelay time.Duration
  32. built bool
  33. context Context
  34. Crc64 hash.Hash64
  35. ProgressFn ProgressFunc
  36. ContentType string
  37. RequestType string
  38. }
  39. // An Operation is the service API operation to be made.
  40. type Operation struct {
  41. Name string
  42. HTTPMethod string
  43. HTTPPath string
  44. *Paginator
  45. }
  46. // Paginator keeps track of pagination configuration for an API operation.
  47. type Paginator struct {
  48. InputTokens []string
  49. OutputTokens []string
  50. LimitToken string
  51. TruncationToken string
  52. }
  53. // NewRequest returns a new Request pointer for the service API
  54. // operation and parameters.
  55. //
  56. // Params is any value of input parameters to be the request payload.
  57. // Data is pointer value to an object which the request's response
  58. // payload will be deserialized to.
  59. func NewRequest(service *Service, operation *Operation, params interface{}, data interface{}) *Request {
  60. method := operation.HTTPMethod
  61. if method == "" {
  62. method = "POST"
  63. }
  64. p := operation.HTTPPath
  65. if p == "" {
  66. p = "/"
  67. }
  68. httpReq, _ := http.NewRequest(method, "", nil)
  69. httpReq.URL, _ = url.Parse(service.Endpoint + p)
  70. r := &Request{
  71. Service: service,
  72. Handlers: service.Handlers.copy(),
  73. Time: time.Now(),
  74. ExpireTime: 0,
  75. Operation: operation,
  76. HTTPRequest: httpReq,
  77. Body: nil,
  78. Params: params,
  79. Error: nil,
  80. Data: data,
  81. }
  82. r.SetBufferBody([]byte{})
  83. return r
  84. }
  85. func NewRequestV2(service *Service, method string, params interface{}) *Request {
  86. httpReq, _ := http.NewRequest(method, "", nil)
  87. httpReq.URL, _ = url.Parse(service.Endpoint)
  88. r := &Request{
  89. Service: service,
  90. ExpireTime: 0,
  91. HTTPRequest: httpReq,
  92. Params: params,
  93. Error: nil,
  94. }
  95. return r
  96. }
  97. // WillRetry returns if the request's can be retried.
  98. func (r *Request) WillRetry() bool {
  99. return r.Error != nil && r.Retryable.Get() && int(r.RetryCount) < r.Service.MaxRetries
  100. }
  101. // ParamsFilled returns if the request's parameters have been populated
  102. // and the parameters are valid. False is returned if no parameters are
  103. // provided or invalid.
  104. func (r *Request) ParamsFilled() bool {
  105. return r.Params != nil && reflect.ValueOf(r.Params).Elem().IsValid()
  106. }
  107. // DataFilled returns true if the request's data for response deserialization
  108. // target has been set and is a valid. False is returned if data is not
  109. // set, or is invalid.
  110. func (r *Request) DataFilled() bool {
  111. return r.Data != nil && reflect.ValueOf(r.Data).Elem().IsValid()
  112. }
  113. // SetBufferBody will set the request's body bytes that will be sent to
  114. // the service API.
  115. func (r *Request) SetBufferBody(buf []byte) {
  116. r.SetReaderBody(bytes.NewReader(buf))
  117. }
  118. // SetStringBody sets the body of the request to be backed by a string.
  119. func (r *Request) SetStringBody(s string) {
  120. r.SetReaderBody(strings.NewReader(s))
  121. }
  122. // SetReaderBody will set the request's body reader.
  123. func (r *Request) SetReaderBody(reader io.ReadSeeker) {
  124. if r.Config.CrcCheckEnabled {
  125. r.Crc64 = crc.NewCRC(crc.CrcTable(), 0)
  126. }
  127. if r.Config.CrcCheckEnabled || r.ProgressFn != nil {
  128. r.HTTPRequest.Body = TeeReader(reader, r.Crc64, GetReaderLen(reader), r.ProgressFn)
  129. } else {
  130. r.HTTPRequest.Body = io.NopCloser(reader)
  131. }
  132. r.Body = reader
  133. }
  134. // Build will build the request's object, so it can be signed and sent
  135. // to the service. Build will also validate all the request's parameters.
  136. // Anny additional build Handlers set on this request will be run
  137. // in the order they were set.
  138. //
  139. // The request will only be built once. Multiple calls to build will have
  140. // no effect.
  141. //
  142. // If any Validate or Build errors occur the build will stop and the error
  143. // which occurred will be returned.
  144. func (r *Request) Build() error {
  145. if !r.built {
  146. r.Error = nil
  147. r.Handlers.Validate.Run(r)
  148. if r.Error != nil {
  149. return r.Error
  150. }
  151. r.Handlers.Build.Run(r)
  152. r.built = true
  153. }
  154. return r.Error
  155. }
  156. // Sign will sign the request retuning error if errors are encountered.
  157. //
  158. // Send will build the request prior to signing. All Sign Handlers will
  159. // be executed in the order they were set.
  160. func (r *Request) Sign() error {
  161. r.Build()
  162. if r.Error != nil {
  163. return r.Error
  164. }
  165. r.Handlers.Sign.Run(r)
  166. return r.Error
  167. }
  168. // Send will send the request returning error if errors are encountered.
  169. //
  170. // Send will sign the request prior to sending. All Send Handlers will
  171. // be executed in the order they were set.
  172. func (r *Request) Send() error {
  173. for {
  174. r.Sign()
  175. if r.Error != nil {
  176. return r.Error
  177. }
  178. if r.Retryable.Get() {
  179. // Re-seek the body back to the original point in for a retry so that
  180. // send will send the body's contents again in the upcoming request.
  181. r.Body.Seek(r.bodyStart, 0)
  182. }
  183. r.Retryable.Reset()
  184. r.Handlers.Send.Run(r)
  185. if r.Error != nil {
  186. r.Handlers.Retry.Run(r)
  187. r.Handlers.AfterRetry.Run(r)
  188. if r.Error != nil {
  189. return r.Error
  190. }
  191. continue
  192. }
  193. r.Handlers.UnmarshalMeta.Run(r)
  194. r.Handlers.ValidateResponse.Run(r)
  195. if r.Error != nil {
  196. r.Handlers.UnmarshalError.Run(r)
  197. r.Handlers.Retry.Run(r)
  198. r.Handlers.AfterRetry.Run(r)
  199. if r.Error != nil {
  200. return r.Error
  201. }
  202. continue
  203. }
  204. r.Handlers.Unmarshal.Run(r)
  205. r.Handlers.CheckCrc64.Run(r)
  206. if r.Error != nil {
  207. r.Handlers.Retry.Run(r)
  208. r.Handlers.AfterRetry.Run(r)
  209. if r.Error != nil {
  210. return r.Error
  211. }
  212. continue
  213. }
  214. break
  215. }
  216. return nil
  217. }
  218. // HasNextPage returns true if this request has more pages of data available.
  219. func (r *Request) HasNextPage() bool {
  220. return r.nextPageTokens() != nil
  221. }
  222. // nextPageTokens returns the tokens to use when asking for the next page of
  223. // data.
  224. func (r *Request) nextPageTokens() []interface{} {
  225. if r.Operation.Paginator == nil {
  226. return nil
  227. }
  228. if r.Operation.TruncationToken != "" {
  229. tr := awsutil.ValuesAtAnyPath(r.Data, r.Operation.TruncationToken)
  230. if tr == nil || len(tr) == 0 {
  231. return nil
  232. }
  233. switch v := tr[0].(type) {
  234. case bool:
  235. if v == false {
  236. return nil
  237. }
  238. }
  239. }
  240. found := false
  241. tokens := make([]interface{}, len(r.Operation.OutputTokens))
  242. for i, outtok := range r.Operation.OutputTokens {
  243. v := awsutil.ValuesAtAnyPath(r.Data, outtok)
  244. if v != nil && len(v) > 0 {
  245. found = true
  246. tokens[i] = v[0]
  247. }
  248. }
  249. if found {
  250. return tokens
  251. }
  252. return nil
  253. }
  254. // NextPage returns a new Request that can be executed to return the next
  255. // page of result data. Call .Send() on this request to execute it.
  256. func (r *Request) NextPage() *Request {
  257. tokens := r.nextPageTokens()
  258. if tokens == nil {
  259. return nil
  260. }
  261. data := reflect.New(reflect.TypeOf(r.Data).Elem()).Interface()
  262. nr := NewRequest(r.Service, r.Operation, awsutil.CopyOf(r.Params), data)
  263. for i, intok := range nr.Operation.InputTokens {
  264. awsutil.SetValueAtAnyPath(nr.Params, intok, tokens[i])
  265. }
  266. return nr
  267. }
  268. // EachPage iterates over each page of a paginated request object. The fn
  269. // parameter should be a function with the following sample signature:
  270. //
  271. // func(page *T, lastPage bool) bool {
  272. // return true // return false to stop iterating
  273. // }
  274. //
  275. // Where "T" is the structure type matching the output structure of the given
  276. // operation. For example, a request object generated by
  277. // DynamoDB.ListTablesRequest() would expect to see dynamodb.ListTablesOutput
  278. // as the structure "T". The lastPage value represents whether the page is
  279. // the last page of data or not. The return value of this function should
  280. // return true to keep iterating or false to stop.
  281. func (r *Request) EachPage(fn func(data interface{}, isLastPage bool) (shouldContinue bool)) error {
  282. for page := r; page != nil; page = page.NextPage() {
  283. page.Send()
  284. shouldContinue := fn(page.Data, !page.HasNextPage())
  285. if page.Error != nil || !shouldContinue {
  286. return page.Error
  287. }
  288. }
  289. return nil
  290. }