request.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Optionally, it performs GZIP compression.
  33. func (r *Request) SetBody(body interface{}, gzipCompress bool) error {
  34. switch b := body.(type) {
  35. case string:
  36. if gzipCompress {
  37. return r.setBodyGzip(b)
  38. } else {
  39. return r.setBodyString(b)
  40. }
  41. default:
  42. if gzipCompress {
  43. return r.setBodyGzip(body)
  44. } else {
  45. return r.setBodyJson(body)
  46. }
  47. }
  48. }
  49. // setBodyJson encodes the body as a struct to be marshaled via json.Marshal.
  50. func (r *Request) setBodyJson(data interface{}) error {
  51. body, err := json.Marshal(data)
  52. if err != nil {
  53. return err
  54. }
  55. r.Header.Set("Content-Type", "application/json")
  56. r.setBodyReader(bytes.NewReader(body))
  57. return nil
  58. }
  59. // setBodyString encodes the body as a string.
  60. func (r *Request) setBodyString(body string) error {
  61. return r.setBodyReader(strings.NewReader(body))
  62. }
  63. // setBodyGzip gzip's the body. It accepts both strings and structs as body.
  64. // The latter will be encoded via json.Marshal.
  65. func (r *Request) setBodyGzip(body interface{}) error {
  66. switch b := body.(type) {
  67. case string:
  68. buf := new(bytes.Buffer)
  69. w := gzip.NewWriter(buf)
  70. if _, err := w.Write([]byte(b)); err != nil {
  71. return err
  72. }
  73. if err := w.Close(); err != nil {
  74. return err
  75. }
  76. r.Header.Add("Content-Encoding", "gzip")
  77. r.Header.Add("Vary", "Accept-Encoding")
  78. return r.setBodyReader(bytes.NewReader(buf.Bytes()))
  79. default:
  80. data, err := json.Marshal(b)
  81. if err != nil {
  82. return err
  83. }
  84. buf := new(bytes.Buffer)
  85. w := gzip.NewWriter(buf)
  86. if _, err := w.Write(data); err != nil {
  87. return err
  88. }
  89. if err := w.Close(); err != nil {
  90. return err
  91. }
  92. r.Header.Add("Content-Encoding", "gzip")
  93. r.Header.Add("Vary", "Accept-Encoding")
  94. r.Header.Set("Content-Type", "application/json")
  95. return r.setBodyReader(bytes.NewReader(buf.Bytes()))
  96. }
  97. }
  98. // setBodyReader writes the body from an io.Reader.
  99. func (r *Request) setBodyReader(body io.Reader) error {
  100. rc, ok := body.(io.ReadCloser)
  101. if !ok && body != nil {
  102. rc = ioutil.NopCloser(body)
  103. }
  104. r.Body = rc
  105. if body != nil {
  106. switch v := body.(type) {
  107. case *strings.Reader:
  108. r.ContentLength = int64(v.Len())
  109. case *bytes.Buffer:
  110. r.ContentLength = int64(v.Len())
  111. }
  112. }
  113. return nil
  114. }