http_warpper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.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. 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. if resp.StatusCode != http.StatusOK {
  73. defer resp.Body.Close()
  74. error_text, _ := io.ReadAll(resp.Body)
  75. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, error_text)
  76. }
  77. ch := stream.NewStream[T](1024)
  78. // get read timeout
  79. read_timeout := int64(60000)
  80. raise_error_when_stream_data_not_match := false
  81. for _, option := range options {
  82. if option.Type == "read_timeout" {
  83. read_timeout = option.Value.(int64)
  84. break
  85. } else if option.Type == "raiseErrorWhenStreamDataNotMatch" {
  86. raise_error_when_stream_data_not_match = option.Value.(bool)
  87. }
  88. }
  89. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  90. // close the response body if timeout
  91. resp.Body.Close()
  92. })
  93. routine.Submit(func() {
  94. scanner := bufio.NewScanner(resp.Body)
  95. defer resp.Body.Close()
  96. for scanner.Scan() {
  97. data := scanner.Bytes()
  98. if len(data) == 0 {
  99. continue
  100. }
  101. if bytes.HasPrefix(data, []byte("data: ")) {
  102. // split
  103. data = data[6:]
  104. }
  105. // unmarshal
  106. t, err := parser.UnmarshalJsonBytes[T](data)
  107. if err != nil {
  108. if raise_error_when_stream_data_not_match {
  109. ch.WriteError(err)
  110. break
  111. }
  112. continue
  113. }
  114. ch.Write(t)
  115. }
  116. ch.Close()
  117. })
  118. return ch, nil
  119. }
  120. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  121. return RequestAndParseStream[T](client, url, "GET", options...)
  122. }
  123. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  124. return RequestAndParseStream[T](client, url, "POST", options...)
  125. }
  126. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  127. return RequestAndParseStream[T](client, url, "PUT", options...)
  128. }