http_warpper.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. resp, err := Request(client, url, method, options...)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // get read timeout
  26. read_timeout := int64(60000)
  27. for _, option := range options {
  28. if option.Type == "read_timeout" {
  29. read_timeout = option.Value.(int64)
  30. break
  31. }
  32. }
  33. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  34. // close the response body if timeout
  35. resp.Body.Close()
  36. })
  37. err = parseJsonBody(resp, &ret)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &ret, nil
  42. }
  43. func GetAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  44. return RequestAndParse[T](client, url, "GET", options...)
  45. }
  46. func PostAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  47. return RequestAndParse[T](client, url, "POST", options...)
  48. }
  49. func PutAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  50. return RequestAndParse[T](client, url, "PUT", options...)
  51. }
  52. func DeleteAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  53. return RequestAndParse[T](client, url, "DELETE", options...)
  54. }
  55. func PatchAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  56. return RequestAndParse[T](client, url, "PATCH", options...)
  57. }
  58. func RequestAndParseStream[T any](client *http.Client, url string, method string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  59. resp, err := Request(client, url, method, options...)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if resp.StatusCode != http.StatusOK {
  64. defer resp.Body.Close()
  65. error_text, _ := io.ReadAll(resp.Body)
  66. return nil, fmt.Errorf("request failed with status code: %d and respond with: %s", resp.StatusCode, error_text)
  67. }
  68. ch := stream.NewStreamResponse[T](1024)
  69. // get read timeout
  70. read_timeout := int64(60000)
  71. for _, option := range options {
  72. if option.Type == "read_timeout" {
  73. read_timeout = option.Value.(int64)
  74. break
  75. }
  76. }
  77. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  78. // close the response body if timeout
  79. resp.Body.Close()
  80. })
  81. routine.Submit(func() {
  82. scanner := bufio.NewScanner(resp.Body)
  83. defer resp.Body.Close()
  84. for scanner.Scan() {
  85. data := scanner.Bytes()
  86. if len(data) == 0 {
  87. continue
  88. }
  89. if bytes.HasPrefix(data, []byte("data: ")) {
  90. // split
  91. data = data[6:]
  92. }
  93. // unmarshal
  94. t, err := parser.UnmarshalJsonBytes[T](data)
  95. if err != nil {
  96. ch.WriteError(err)
  97. break
  98. }
  99. ch.Write(t)
  100. }
  101. ch.Close()
  102. })
  103. return ch, nil
  104. }
  105. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  106. return RequestAndParseStream[T](client, url, "GET", options...)
  107. }
  108. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  109. return RequestAndParseStream[T](client, url, "POST", options...)
  110. }
  111. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  112. return RequestAndParseStream[T](client, url, "PUT", options...)
  113. }