You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "bytes"
  7. "compress/gzip"
  8. "encoding/json"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. )
  14. // Elasticsearch-specific HTTP request
  15. type Request http.Request
  16. // NewRequest is a http.Request and adds features such as encoding the body.
  17. func NewRequest(method, url string) (*Request, error) {
  18. req, err := http.NewRequest(method, url, nil)
  19. if err != nil {
  20. return nil, err
  21. }
  22. req.Header.Add("Accept", "application/json")
  23. req.Header.Set("Content-Type", "application/json")
  24. return (*Request)(req), nil
  25. }
  26. // SetBasicAuth wraps http.Request's SetBasicAuth.
  27. func (r *Request) SetBasicAuth(username, password string) {
  28. ((*http.Request)(r)).SetBasicAuth(username, password)
  29. }
  30. // SetBody encodes the body in the request. You may pass a flag to
  31. // compress the request via gzip.
  32. func (r *Request) SetBody(body interface{}, gzipCompress bool) error {
  33. switch b := body.(type) {
  34. case string:
  35. if gzipCompress {
  36. return r.setBodyGzip(b)
  37. }
  38. return r.setBodyString(b)
  39. default:
  40. if gzipCompress {
  41. return r.setBodyGzip(body)
  42. }
  43. return r.setBodyJson(body)
  44. }
  45. }
  46. // setBodyJson encodes the body as a struct to be marshaled via json.Marshal.
  47. func (r *Request) setBodyJson(data interface{}) error {
  48. body, err := json.Marshal(data)
  49. if err != nil {
  50. return err
  51. }
  52. r.Header.Set("Content-Type", "application/json")
  53. r.setBodyReader(bytes.NewReader(body))
  54. return nil
  55. }
  56. // setBodyString encodes the body as a string.
  57. func (r *Request) setBodyString(body string) error {
  58. return r.setBodyReader(strings.NewReader(body))
  59. }
  60. // setBodyGzip gzip's the body. It accepts both strings and structs as body.
  61. // The latter will be encoded via json.Marshal.
  62. func (r *Request) setBodyGzip(body interface{}) error {
  63. switch b := body.(type) {
  64. case string:
  65. buf := new(bytes.Buffer)
  66. w := gzip.NewWriter(buf)
  67. if _, err := w.Write([]byte(b)); err != nil {
  68. return err
  69. }
  70. if err := w.Close(); err != nil {
  71. return err
  72. }
  73. r.Header.Add("Content-Encoding", "gzip")
  74. r.Header.Add("Vary", "Accept-Encoding")
  75. return r.setBodyReader(bytes.NewReader(buf.Bytes()))
  76. default:
  77. data, err := json.Marshal(b)
  78. if err != nil {
  79. return err
  80. }
  81. buf := new(bytes.Buffer)
  82. w := gzip.NewWriter(buf)
  83. if _, err := w.Write(data); err != nil {
  84. return err
  85. }
  86. if err := w.Close(); err != nil {
  87. return err
  88. }
  89. r.Header.Add("Content-Encoding", "gzip")
  90. r.Header.Add("Vary", "Accept-Encoding")
  91. r.Header.Set("Content-Type", "application/json")
  92. return r.setBodyReader(bytes.NewReader(buf.Bytes()))
  93. }
  94. }
  95. // setBodyReader writes the body from an io.Reader.
  96. func (r *Request) setBodyReader(body io.Reader) error {
  97. rc, ok := body.(io.ReadCloser)
  98. if !ok && body != nil {
  99. rc = ioutil.NopCloser(body)
  100. }
  101. r.Body = rc
  102. if body != nil {
  103. switch v := body.(type) {
  104. case *strings.Reader:
  105. r.ContentLength = int64(v.Len())
  106. case *bytes.Buffer:
  107. r.ContentLength = int64(v.Len())
  108. }
  109. }
  110. return nil
  111. }