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.1KB

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