http_warpper.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ch := stream.NewStream[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. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  109. return RequestAndParseStream[T](client, url, "GET", options...)
  110. }
  111. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  112. return RequestAndParseStream[T](client, url, "POST", options...)
  113. }
  114. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.Stream[T], error) {
  115. return RequestAndParseStream[T](client, url, "PUT", options...)
  116. }