http_warpper.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. json_decoder := json.NewDecoder(resp.Body)
  17. return json_decoder.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. read_timeout := int64(60000)
  31. for _, option := range options {
  32. if option.Type == "read_timeout" {
  33. read_timeout = option.Value.(int64)
  34. break
  35. }
  36. }
  37. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), 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.StreamResponse[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. error_text, _ := io.ReadAll(resp.Body)
  70. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, error_text)
  71. }
  72. ch := stream.NewStreamResponse[T](1024)
  73. // get read timeout
  74. read_timeout := int64(60000)
  75. for _, option := range options {
  76. if option.Type == "read_timeout" {
  77. read_timeout = option.Value.(int64)
  78. break
  79. }
  80. }
  81. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  82. // close the response body if timeout
  83. resp.Body.Close()
  84. })
  85. routine.Submit(func() {
  86. scanner := bufio.NewScanner(resp.Body)
  87. defer resp.Body.Close()
  88. for scanner.Scan() {
  89. data := scanner.Bytes()
  90. if len(data) == 0 {
  91. continue
  92. }
  93. if bytes.HasPrefix(data, []byte("data: ")) {
  94. // split
  95. data = data[6:]
  96. }
  97. // unmarshal
  98. t, err := parser.UnmarshalJsonBytes[T](data)
  99. if err != nil {
  100. continue
  101. }
  102. ch.Write(t)
  103. }
  104. ch.Close()
  105. })
  106. return ch, nil
  107. }
  108. // TODO: improve this, deduplicate code
  109. func RequestAndParseStreamMap(client *http.Client, url string, method string, options ...HttpOptions) (*stream.StreamResponse[map[string]any], error) {
  110. resp, err := Request(client, url, method, options...)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if resp.StatusCode != http.StatusOK {
  115. defer resp.Body.Close()
  116. error_text, _ := io.ReadAll(resp.Body)
  117. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, error_text)
  118. }
  119. ch := stream.NewStreamResponse[map[string]any](1024)
  120. // get read timeout
  121. read_timeout := int64(60000)
  122. for _, option := range options {
  123. if option.Type == "read_timeout" {
  124. read_timeout = option.Value.(int64)
  125. break
  126. }
  127. }
  128. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  129. // close the response body if timeout
  130. resp.Body.Close()
  131. })
  132. routine.Submit(func() {
  133. scanner := bufio.NewScanner(resp.Body)
  134. defer resp.Body.Close()
  135. for scanner.Scan() {
  136. data := scanner.Bytes()
  137. if len(data) == 0 {
  138. continue
  139. }
  140. if bytes.HasPrefix(data, []byte("data: ")) {
  141. // split
  142. data = data[6:]
  143. }
  144. // unmarshal
  145. t, err := parser.UnmarshalJsonBytes2Map(data)
  146. if err != nil {
  147. continue
  148. }
  149. ch.Write(t)
  150. }
  151. ch.Close()
  152. })
  153. return ch, nil
  154. }
  155. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  156. return RequestAndParseStream[T](client, url, "GET", options...)
  157. }
  158. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  159. return RequestAndParseStream[T](client, url, "POST", options...)
  160. }
  161. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  162. return RequestAndParseStream[T](client, url, "PUT", options...)
  163. }