http_warpper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package http_requests
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "time"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  13. )
  14. func parseJsonBody(resp *http.Response, ret interface{}) error {
  15. defer resp.Body.Close()
  16. jsonDecoder := json.NewDecoder(resp.Body)
  17. return jsonDecoder.Decode(ret)
  18. }
  19. func RequestAndParse[T any](client *http.Client, url string, method string, options ...HttpOptions) (*T, error) {
  20. var ret T
  21. // check if ret is a map, if so, create a new map
  22. if _, ok := any(ret).(map[string]any); ok {
  23. ret = *new(T)
  24. }
  25. resp, err := Request(client, url, method, options...)
  26. if err != nil {
  27. return nil, err
  28. }
  29. // get read timeout
  30. readTimeout := int64(60000)
  31. for _, option := range options {
  32. if option.Type == "read_timeout" {
  33. readTimeout = option.Value.(int64)
  34. break
  35. }
  36. }
  37. time.AfterFunc(time.Millisecond*time.Duration(readTimeout), func() {
  38. // close the response body if timeout
  39. resp.Body.Close()
  40. })
  41. err = parseJsonBody(resp, &ret)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &ret, nil
  46. }
  47. func GetAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  48. return RequestAndParse[T](client, url, "GET", options...)
  49. }
  50. func PostAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  51. return RequestAndParse[T](client, url, "POST", options...)
  52. }
  53. func PutAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  54. return RequestAndParse[T](client, url, "PUT", options...)
  55. }
  56. func DeleteAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  57. return RequestAndParse[T](client, url, "DELETE", options...)
  58. }
  59. func PatchAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  60. return RequestAndParse[T](client, url, "PATCH", options...)
  61. }
  62. func RequestAndParseStream[T any](client *http.Client, url string, method string, options ...HttpOptions) (*stream.Stream[T], error) {
  63. resp, err := Request(client, url, method, options...)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if resp.StatusCode != http.StatusOK {
  68. defer resp.Body.Close()
  69. errorText, _ := io.ReadAll(resp.Body)
  70. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, errorText)
  71. }
  72. if resp.StatusCode != http.StatusOK {
  73. defer resp.Body.Close()
  74. errorText, _ := io.ReadAll(resp.Body)
  75. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, errorText)
  76. }
  77. ch := stream.NewStream[T](1024)
  78. // get read timeout
  79. readTimeout := int64(60000)
  80. raiseErrorWhenStreamDataNotMatch := false
  81. for _, option := range options {
  82. if option.Type == "read_timeout" {
  83. readTimeout = option.Value.(int64)
  84. break
  85. } else if option.Type == "raiseErrorWhenStreamDataNotMatch" {
  86. raiseErrorWhenStreamDataNotMatch = option.Value.(bool)
  87. }
  88. }
  89. time.AfterFunc(time.Millisecond*time.Duration(readTimeout), func() {
  90. // close the response body if timeout
  91. resp.Body.Close()
  92. })
  93. routine.Submit(map[string]string{
  94. "module": "http_requests",
  95. "function": "RequestAndParseStream",
  96. }, func() {
  97. scanner := bufio.NewScanner(resp.Body)
  98. defer resp.Body.Close()
  99. for scanner.Scan() {
  100. data := scanner.Bytes()
  101. if len(data) == 0 {
  102. continue
  103. }
  104. if bytes.HasPrefix(data, []byte("data: ")) {
  105. // split
  106. data = data[6:]
  107. }
  108. // unmarshal
  109. t, err := parser.UnmarshalJsonBytes[T](data)
  110. if err != nil {
  111. if raiseErrorWhenStreamDataNotMatch {
  112. ch.WriteError(err)
  113. break
  114. }
  115. continue
  116. }
  117. ch.Write(t)
  118. }
  119. ch.Close()
  120. })
  121. return ch, nil
  122. }
  123. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  124. return RequestAndParseStream[T](client, url, "GET", options...)
  125. }
  126. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  127. return RequestAndParseStream[T](client, url, "POST", options...)
  128. }
  129. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  130. return RequestAndParseStream[T](client, url, "PUT", options...)
  131. }