http_warpper.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package requests
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "net/http"
  7. "time"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  11. )
  12. func parseJsonBody(resp *http.Response, ret interface{}) error {
  13. defer resp.Body.Close()
  14. json_decoder := json.NewDecoder(resp.Body)
  15. return json_decoder.Decode(ret)
  16. }
  17. func RequestAndParse[T any](client *http.Client, url string, method string, options ...HttpOptions) (*T, error) {
  18. var ret T
  19. resp, err := Request(client, url, method, options...)
  20. if err != nil {
  21. return nil, err
  22. }
  23. // get read timeout
  24. read_timeout := int64(60000)
  25. for _, option := range options {
  26. if option.Type == "read_timeout" {
  27. read_timeout = option.Value.(int64)
  28. break
  29. }
  30. }
  31. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  32. // close the response body if timeout
  33. resp.Body.Close()
  34. })
  35. err = parseJsonBody(resp, &ret)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &ret, nil
  40. }
  41. func GetAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  42. return RequestAndParse[T](client, url, "GET", options...)
  43. }
  44. func PostAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  45. return RequestAndParse[T](client, url, "POST", options...)
  46. }
  47. func PutAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  48. return RequestAndParse[T](client, url, "PUT", options...)
  49. }
  50. func DeleteAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  51. return RequestAndParse[T](client, url, "DELETE", options...)
  52. }
  53. func PatchAndParse[T any](client *http.Client, url string, options ...HttpOptions) (*T, error) {
  54. return RequestAndParse[T](client, url, "PATCH", options...)
  55. }
  56. func RequestAndParseStream[T any](client *http.Client, url string, method string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  57. resp, err := Request(client, url, method, options...)
  58. if err != nil {
  59. return nil, err
  60. }
  61. ch := stream.NewStreamResponse[T](1024)
  62. // get read timeout
  63. read_timeout := int64(60000)
  64. for _, option := range options {
  65. if option.Type == "read_timeout" {
  66. read_timeout = option.Value.(int64)
  67. break
  68. }
  69. }
  70. time.AfterFunc(time.Millisecond*time.Duration(read_timeout), func() {
  71. // close the response body if timeout
  72. resp.Body.Close()
  73. })
  74. routine.Submit(func() {
  75. scanner := bufio.NewScanner(resp.Body)
  76. for scanner.Scan() {
  77. data := scanner.Bytes()
  78. if bytes.HasPrefix(data, []byte("data: ")) {
  79. // split
  80. data = data[6:]
  81. // unmarshal
  82. t, err := parser.UnmarshalJsonBytes[T](data)
  83. if err != nil {
  84. continue
  85. }
  86. ch.Write(t)
  87. }
  88. }
  89. ch.Close()
  90. })
  91. return ch, nil
  92. }
  93. func GetAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  94. return RequestAndParseStream[T](client, url, "GET", options...)
  95. }
  96. func PostAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  97. return RequestAndParseStream[T](client, url, "POST", options...)
  98. }
  99. func PutAndParseStream[T any](client *http.Client, url string, options ...HttpOptions) (*stream.StreamResponse[T], error) {
  100. return RequestAndParseStream[T](client, url, "PUT", options...)
  101. }