You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 1.0 kB

1 year ago
1 year ago
1234567891011121314151617181920212223242526272829303132333435363738
  1. package cdsapi
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "path/filepath"
  7. "strings"
  8. "gitlink.org.cn/cloudream/common/utils/http2"
  9. "gitlink.org.cn/cloudream/common/utils/math2"
  10. "gitlink.org.cn/cloudream/common/utils/serder"
  11. )
  12. func MakeIPFSFilePath(fileHash string) string {
  13. return filepath.Join("ipfs", fileHash)
  14. }
  15. func ParseJSONResponse[TBody any](resp *http.Response) (TBody, error) {
  16. var ret TBody
  17. contType := resp.Header.Get("Content-Type")
  18. if strings.Contains(contType, http2.ContentTypeJSON) {
  19. var err error
  20. if ret, err = serder.JSONToObjectStreamEx[TBody](resp.Body); err != nil {
  21. return ret, fmt.Errorf("parsing response: %w", err)
  22. }
  23. return ret, nil
  24. }
  25. cont, err := io.ReadAll(resp.Body)
  26. if err != nil {
  27. return ret, fmt.Errorf("unknow response content type: %s, status: %d", contType, resp.StatusCode)
  28. }
  29. strCont := string(cont)
  30. return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math2.Min(len(strCont), 200)])
  31. }