Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

108 rader
2.8 KiB

  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "sort"
  6. "strings"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/model"
  9. )
  10. const (
  11. AssetErrorInvalidRequest = "invalid_request_error"
  12. AssetErrorChannelNotFound = "asset_channel_not_found"
  13. AssetErrorBindingInvalid = "asset_channel_binding_invalid"
  14. AssetErrorOperationNotSupported = "asset_operation_not_supported"
  15. AssetErrorNotFound = "asset_not_found"
  16. AssetErrorUpstream = "upstream_error"
  17. AssetErrorServer = "server_error"
  18. )
  19. type AssetError struct {
  20. Type string
  21. Message string
  22. HTTPStatus int
  23. }
  24. func newAssetError(errType string, message string, status int) *AssetError {
  25. if status == 0 {
  26. status = http.StatusBadGateway
  27. }
  28. return &AssetError{Type: errType, Message: message, HTTPStatus: status}
  29. }
  30. type AssetRequest struct {
  31. Action AssetActionSpec
  32. Version string
  33. Body map[string]any
  34. RawBody []byte
  35. }
  36. type AssetUpstreamResponse struct {
  37. StatusCode int
  38. Header http.Header
  39. Body []byte
  40. }
  41. type AssetAdapter interface {
  42. Name() string
  43. Supports(operation AssetOperation) bool
  44. DoAssetRequest(ctx context.Context, channel *model.Channel, req AssetRequest) (*AssetUpstreamResponse, *AssetError)
  45. }
  46. var assetAdapters = map[int]AssetAdapter{}
  47. func init() {
  48. registerDefaultAssetAdapters()
  49. }
  50. func registerDefaultAssetAdapters() {
  51. assetAdapters[constant.ChannelTypeChinaMobileSeedance] = NewChinaMobileAssetAdapter()
  52. assetAdapters[constant.ChannelTypeDoubaoVideoCompatibleAiping] = NewCompatibleAssetAdapter("aiping_asset", []AssetOperation{
  53. AssetOperationAssetCreate,
  54. AssetOperationAssetList,
  55. AssetOperationAssetGet,
  56. AssetOperationAssetUpdate,
  57. AssetOperationAssetDelete,
  58. })
  59. assetAdapters[constant.ChannelTypeDoubaoVideoCompatibleTianyiYun] = NewCompatibleAssetAdapter("tianyiyun_asset", []AssetOperation{
  60. AssetOperationAssetCreate,
  61. AssetOperationAssetList,
  62. AssetOperationAssetGet,
  63. AssetOperationAssetUpdate,
  64. AssetOperationAssetDelete,
  65. })
  66. }
  67. func GetAssetAdapter(channelType int) (AssetAdapter, bool) {
  68. adapter, ok := assetAdapters[channelType]
  69. return adapter, ok
  70. }
  71. func OverrideAssetAdapterForTest(channelType int, adapter AssetAdapter) func() {
  72. oldAdapter, hadOldAdapter := assetAdapters[channelType]
  73. assetAdapters[channelType] = adapter
  74. return func() {
  75. if hadOldAdapter {
  76. assetAdapters[channelType] = oldAdapter
  77. } else {
  78. delete(assetAdapters, channelType)
  79. }
  80. }
  81. }
  82. func RegisteredAssetChannelTypes() []int {
  83. types := make([]int, 0, len(assetAdapters))
  84. for channelType := range assetAdapters {
  85. types = append(types, channelType)
  86. }
  87. sort.Ints(types)
  88. return types
  89. }
  90. func assetChannelHasKey(channel *model.Channel) bool {
  91. return channel != nil && strings.TrimSpace(channel.Key) != "" && len(channel.GetKeys()) > 0
  92. }