4747
4848from pydantic import BaseModel , Field , model_validator , ValidationInfo
4949
50+ from snet .sdk .registry .models import FileURI
5051from snet .sdk .utils .utils import is_valid_endpoint
5152
5253
@@ -67,46 +68,76 @@ def is_single_value(asset_type):
6768 return True
6869
6970
71+ class FileType (Enum ):
72+ IMAGE = "image"
73+ VIDEO = "video"
74+ ARCHIVE = "archive"
75+
76+
77+ # class AssetType(Enum):
78+ # HERO_IMAGE = "hero_image"
79+ # PROTO_FILE = "proto_file"
80+ # DEMO_COMPONENT = "demo_component"
81+
82+
83+ class ServiceType (Enum ):
84+ GRPC = "grpc"
85+ HTTP = "http"
86+ JSONRPC = "jsonrpc"
87+
88+
7089def generate_group_id () -> str :
7190 return base64 .b64encode (secrets .token_bytes (32 )).decode ()
7291
7392
7493class Pricing (BaseModel ):
7594 price_model : Literal ["fixed_price" , "method_price" ] = Field (default = "fixed_price" )
76- price_in_cogs : int = Field (ge = 1 )
95+ price_in_cogs : int = Field (ge = 1 , default = 1 )
7796 default : bool = Field (default = True )
7897
7998
8099class Group (BaseModel ):
81100 group_name : str = Field (min_length = 1 , default = "default_group" )
82101 group_id : str = Field (default_factory = generate_group_id , init = False )
83- free_calls : int = Field (ge = 1 )
84- free_call_signer_address : str
85- daemon_addresses : list [str ]
102+ free_calls : int = Field (ge = 1 , default = 3 )
103+ free_call_signer_address : str = Field (default = "" )
104+ daemon_addresses : list [str ] = Field (default = [])
105+ endpoints : list [str ] = Field (default = [])
106+ pricing : list [Pricing ] = Field (default = [])
86107
87108
88- class ServiceDescription (BaseModel ): ...
109+ class ServiceDescription (BaseModel ):
110+ url : str = Field (default = "" )
111+ short_description : str = Field (default = "" )
112+ description : str = Field (default = "" )
89113
90114
91- class Media (BaseModel ): ...
115+ class Media (BaseModel ):
116+ order : int = Field (ge = 1 , default = 1 )
117+ url : str = Field (min_length = 1 )
118+ file_type : FileType
119+ alt_text : str = Field (default = "" )
120+ asset_type : AssetType
92121
93122
94- class Contributor (BaseModel ): ...
123+ class Contributor (BaseModel ):
124+ name : str = Field (min_length = 1 )
125+ email_id : str = Field (default = "" )
95126
96127
97128class ServiceMetadata (BaseModel ):
98129 version : int = Field (ge = 1 , default = 1 )
99- display_name : str
130+ display_name : str = Field ( min_length = 1 )
100131 encoding : Literal ["proto" , "json" ] = Field (default = "proto" )
101132 service_type : Literal ["grpc" , "http" , "jsonrpc" ]
102- service_api_source : Optional [str ] = Field (min_length = 1 , default = None , init = False )
133+ service_api_source : Optional [str ] = Field (default = None , init = False )
103134 model_ipfs_hash : Optional [str ] = Field (min_length = 1 , default = None , init = False , deprecated = True )
104- mpe_address : str
105- groups : list [Group ]
106- service_description : ServiceDescription
107- media : list [Media ]
108- contributors : list [Contributor ]
109- tags : list [str ]
135+ mpe_address : Optional [ str ] = Field ( default = None , init = False )
136+ groups : list [Group ] = Field ( default = [])
137+ service_description : Optional [ ServiceDescription ] = Field ( default = None )
138+ media : list [Media ] = Field ( default = [])
139+ contributors : list [Contributor ] = Field ( default = [])
140+ tags : list [str ] = Field ( default = [])
110141
111142 @model_validator (mode = "before" )
112143 @classmethod
@@ -117,7 +148,6 @@ def restrict_deprecated_fields(cls, data: Any, info: ValidationInfo) -> Any:
117148 is_fetching = info .context and info .context .get ("from_storage" ) is True
118149
119150 if not is_fetching and data .get ("model_ipfs_hash" ):
120- # TODO: configure exceptions
121151 raise ValueError (
122152 "The 'model_ipfs_hash' field is deprecated and cannot be used "
123153 "to create new metadata. Please use 'service_api_source' instead."
@@ -128,10 +158,22 @@ def restrict_deprecated_fields(cls, data: Any, info: ValidationInfo) -> Any:
128158 def generate_final_json (self ):
129159 if self .service_api_source is None :
130160 if self .model_ipfs_hash is None :
131- # TODO: configure exceptions
132161 raise ValueError ("The 'service_api_source' field is missing!" )
133162 else :
134- self .service_api_source , self .model_ipfs_hash = self .model_ipfs_hash , None
163+ self .service_api_source = FileURI .normalize_string_uri (self .model_ipfs_hash )
164+ self .model_ipfs_hash = None
165+
166+ if not self .mpe_address :
167+ raise ValueError ("The 'mpe_address' field is missing!" )
168+
169+ if len (self .groups ) == 0 :
170+ raise ValueError ("There must be one item in 'groups' field at least!" )
171+
172+ if len (self .contributors ) == 0 :
173+ raise ValueError ("There must be one item in 'contributors' field at least!" )
174+
175+ if not self .service_description :
176+ raise ValueError ("The 'mpe_address' field is missing!" )
135177
136178 return self .model_dump_json (indent = 2 , exclude_none = True )
137179
0 commit comments