33
33
from confluent_kafka .schema_registry .error import SchemaRegistryError , OAuthTokenError
34
34
from confluent_kafka .schema_registry .common .schema_registry_client import (
35
35
RegisteredSchema ,
36
+ SchemaVersion ,
36
37
ServerConfig ,
37
38
is_success ,
38
39
is_retriable ,
@@ -663,17 +664,19 @@ async def register_schema_full_response(
663
664
return registered_schema
664
665
665
666
async def get_schema (
666
- self , schema_id : int , subject_name : Optional [str ] = None , fmt : Optional [str ] = None
667
+ self , schema_id : int , subject_name : Optional [str ] = None ,
668
+ fmt : Optional [str ] = None , reference_format : Optional [str ] = None ,
667
669
) -> 'Schema' :
668
670
"""
669
671
Fetches the schema associated with ``schema_id`` from the
670
672
Schema Registry. The result is cached so subsequent attempts will not
671
673
require an additional round-trip to the Schema Registry.
672
674
673
675
Args:
674
- schema_id (int): Schema id
675
- subject_name (str): Subject name the schema is registered under
676
- fmt (str): Format of the schema
676
+ schema_id (int): Schema id.
677
+ subject_name (str): Subject name the schema is registered under.
678
+ fmt (str): Desired output format, dependent on schema type.
679
+ reference_format (str): Desired output format for references.
677
680
678
681
Returns:
679
682
Schema: Schema instance identified by the ``schema_id``
@@ -682,19 +685,21 @@ async def get_schema(
682
685
SchemaRegistryError: If schema can't be found.
683
686
684
687
See Also:
685
- `GET Schema API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-ids-int-%20id>`_
688
+ `GET Schema API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-ids-int-%20id>`_
686
689
""" # noqa: E501
687
690
688
691
result = self ._cache .get_schema_by_id (subject_name , schema_id )
689
692
if result is not None :
690
693
return result [1 ]
691
694
692
- query = {'subject' : subject_name } if subject_name is not None else None
695
+ query = {}
696
+ if subject_name is not None :
697
+ query ['subject' ] = subject_name
693
698
if fmt is not None :
694
- if query is not None :
695
- query [ 'format' ] = fmt
696
- else :
697
- query = { 'format' : fmt }
699
+ query [ 'format' ] = fmt
700
+ if reference_format is not None :
701
+ query [ 'reference_format' ] = reference_format
702
+
698
703
response = await self ._rest_client .get ('schemas/ids/{}' .format (schema_id ), query )
699
704
700
705
registered_schema = RegisteredSchema .from_dict (response )
@@ -741,24 +746,102 @@ async def get_schema_by_guid(
741
746
742
747
return registered_schema .schema
743
748
749
+ async def get_schema_types (self ) -> List [str ]:
750
+ """
751
+ Lists all supported schema types in the Schema Registry.
752
+
753
+ Returns:
754
+ list(str): List of supported schema types (e.g., ['AVRO', 'JSON', 'PROTOBUF'])
755
+
756
+ Raises:
757
+ SchemaRegistryError: if schema types can't be retrieved
758
+
759
+ See Also:
760
+ `GET Schema Types API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-types>`_
761
+ """ # noqa: E501
762
+
763
+ return await self ._rest_client .get ('schemas/types' )
764
+
765
+ async def get_subjects_by_schema_id (
766
+ self , schema_id : int , subject_name : Optional [str ] = None , deleted : bool = False ,
767
+ offset : int = 0 , limit : int = - 1
768
+ ) -> List [str ]:
769
+ """
770
+ Retrieves all the subjects associated with ``schema_id``.
771
+
772
+ Args:
773
+ schema_id (int): Schema ID.
774
+ subject_name (str): Subject name that results can be filtered by.
775
+ deleted (bool): Whether to include subjects where the schema was deleted.
776
+ offset (int): Pagination offset for results.
777
+ limit (int): Pagination size for results. Ignored if negative.
778
+
779
+ Returns:
780
+ list(str): List of subjects matching the specified parameters.
781
+
782
+ Raises:
783
+ SchemaRegistryError: if subjects can't be found
784
+ """
785
+ query = {'offset' : offset , 'limit' : limit }
786
+ if subject_name is not None :
787
+ query ['subject' ] = subject_name
788
+ if deleted :
789
+ query ['deleted' ] = deleted
790
+ return await self ._rest_client .get ('schemas/ids/{}/subjects' .format (schema_id ), query )
791
+
792
+ async def get_schema_versions (
793
+ self , schema_id : int , subject_name : Optional [str ] = None , deleted : bool = False ,
794
+ offset : int = 0 , limit : int = - 1
795
+ ) -> List [SchemaVersion ]:
796
+ """
797
+ Gets all subject-version pairs of a schema by its ID.
798
+
799
+ Args:
800
+ schema_id (int): Schema ID.
801
+ subject_name (str): Subject name that results can be filtered by.
802
+ deleted (bool): Whether to include subject versions where the schema was deleted.
803
+ offset (int): Pagination offset for results.
804
+ limit (int): Pagination size for results. Ignored if negative.
805
+
806
+ Returns:
807
+ list(SchemaVersion): List of subject-version pairs. Each pair contains:
808
+ - subject (str): Subject name.
809
+ - version (int): Version number.
810
+
811
+ Raises:
812
+ SchemaRegistryError: if schema versions can't be found.
813
+
814
+ See Also:
815
+ `GET Schema Versions API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-ids-int-%20id-versions>`_
816
+ """ # noqa: E501
817
+
818
+ query = {'offset' : offset , 'limit' : limit }
819
+ if subject_name is not None :
820
+ query ['subject' ] = subject_name
821
+ if deleted :
822
+ query ['deleted' ] = deleted
823
+ response = await self ._rest_client .get ('schemas/ids/{}/versions' .format (schema_id ), query )
824
+ return [SchemaVersion .from_dict (item ) for item in response ]
825
+
744
826
async def lookup_schema (
745
827
self , subject_name : str , schema : 'Schema' ,
746
- normalize_schemas : bool = False , deleted : bool = False
828
+ normalize_schemas : bool = False , fmt : Optional [ str ] = None , deleted : bool = False
747
829
) -> 'RegisteredSchema' :
748
830
"""
749
831
Returns ``schema`` registration information for ``subject``.
750
832
751
833
Args:
752
- subject_name (str): Subject name the schema is registered under
834
+ subject_name (str): Subject name the schema is registered under.
753
835
schema (Schema): Schema instance.
754
- normalize_schemas (bool): Normalize schema before registering
836
+ normalize_schemas (bool): Normalize schema before registering.
837
+ fmt (str): Desired output format, dependent on schema type.
755
838
deleted (bool): Whether to include deleted schemas.
756
839
757
840
Returns:
758
841
RegisteredSchema: Subject registration information for this schema.
759
842
760
843
Raises:
761
- SchemaRegistryError: If schema or subject can't be found
844
+ SchemaRegistryError: If schema or subject can't be found.
762
845
763
846
See Also:
764
847
`POST Subject API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#post--subjects-(string-%20subject)>`_
@@ -770,9 +853,17 @@ async def lookup_schema(
770
853
771
854
request = schema .to_dict ()
772
855
856
+ query_params = {
857
+ 'normalize' : normalize_schemas ,
858
+ 'deleted' : deleted
859
+ }
860
+ if fmt is not None :
861
+ query_params ['format' ] = fmt
862
+
863
+ query_string = '&' .join (f"{ key } ={ value } " for key , value in query_params .items ())
864
+
773
865
response = await self ._rest_client .post (
774
- 'subjects/{}?normalize={}&deleted={}' .format (
775
- _urlencode (subject_name ), normalize_schemas , deleted ),
866
+ 'subjects/{}?{}' .format (_urlencode (subject_name ), query_string ),
776
867
body = request
777
868
)
778
869
@@ -791,9 +882,19 @@ async def lookup_schema(
791
882
792
883
return registered_schema
793
884
794
- async def get_subjects (self ) -> List [str ]:
885
+ async def get_subjects (
886
+ self , subject_prefix : Optional [str ] = None , deleted : bool = False , deleted_only : bool = False ,
887
+ offset : int = 0 , limit : int = - 1
888
+ ) -> List [str ]:
795
889
"""
796
- Lists all subjects registered with the Schema Registry
890
+ Lists all subjects registered with the Schema Registry.
891
+
892
+ Args:
893
+ subject_prefix (str): Subject name prefix that results can be filtered by.
894
+ deleted (bool): Whether to include deleted subjects.
895
+ deleted_only (bool): Whether to return deleted subjects only. If both deleted and deleted_only are True, deleted_only takes precedence.
896
+ offset (int): Pagination offset for results.
897
+ limit (int): Pagination size for results. Ignored if negative.
797
898
798
899
Returns:
799
900
list(str): Registered subject names
@@ -805,7 +906,10 @@ async def get_subjects(self) -> List[str]:
805
906
`GET subjects API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects>`_
806
907
""" # noqa: E501
807
908
808
- return await self ._rest_client .get ('subjects' )
909
+ query = {'deleted' : deleted , 'deleted_only' : deleted_only , 'offset' : offset , 'limit' : limit }
910
+ if subject_prefix is not None :
911
+ query ['subject' ] = subject_prefix
912
+ return await self ._rest_client .get ('subjects' , query )
809
913
810
914
async def delete_subject (self , subject_name : str , permanent : bool = False ) -> List [int ]:
811
915
"""
@@ -899,7 +1003,9 @@ async def get_latest_with_metadata(
899
1003
if registered_schema is not None :
900
1004
return registered_schema
901
1005
902
- query = {'deleted' : deleted , 'format' : fmt } if fmt is not None else {'deleted' : deleted }
1006
+ query = {'deleted' : deleted }
1007
+ if fmt is not None :
1008
+ query ['format' ] = fmt
903
1009
keys = metadata .keys ()
904
1010
if keys :
905
1011
query ['key' ] = [_urlencode (key ) for key in keys ]
@@ -920,13 +1026,13 @@ async def get_version(
920
1026
deleted : bool = False , fmt : Optional [str ] = None
921
1027
) -> 'RegisteredSchema' :
922
1028
"""
923
- Retrieves a specific schema registered under `` subject_name``.
1029
+ Retrieves a specific schema registered under `subject_name` and `version `.
924
1030
925
1031
Args:
926
1032
subject_name (str): Subject name.
927
- version (int): version number . Defaults to latest version.
1033
+ version (Union[ int, str] ): Version of the schema or string "latest" . Defaults to latest version.
928
1034
deleted (bool): Whether to include deleted schemas.
929
- fmt (str): Format of the schema
1035
+ fmt (str): Format of the schema.
930
1036
931
1037
Returns:
932
1038
RegisteredSchema: Registration information for this version.
@@ -935,7 +1041,7 @@ async def get_version(
935
1041
SchemaRegistryError: if the version can't be found or is invalid.
936
1042
937
1043
See Also:
938
- `GET Subject Versions API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions>`_
1044
+ `GET Subject Versions API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions-(versionId-%20version) >`_
939
1045
""" # noqa: E501
940
1046
941
1047
registered_schema = self ._cache .get_registered_by_subject_version (subject_name , version )
@@ -953,12 +1059,44 @@ async def get_version(
953
1059
954
1060
return registered_schema
955
1061
956
- async def get_versions (self , subject_name : str ) -> List [int ]:
1062
+ async def get_referenced_by (
1063
+ self , subject_name : str , version : Union [int , str ] = "latest" , offset : int = 0 , limit : int = - 1
1064
+ ) -> List [int ]:
1065
+ """
1066
+ Get a list of IDs of schemas that reference the schema with the given `subject_name` and `version`.
1067
+
1068
+ Args:
1069
+ subject_name (str): Subject name
1070
+ version (int or str): Version number or "latest"
1071
+ offset (int): Pagination offset for results.
1072
+ limit (int): Pagination size for results. Ignored if negative.
1073
+
1074
+ Returns:
1075
+ list(int): List of schema IDs that reference the specified schema.
1076
+
1077
+ Raises:
1078
+ SchemaRegistryError: if the schema version can't be found or referenced schemas can't be retrieved
1079
+
1080
+ See Also:
1081
+ `GET Subject Versions API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions-versionId-%20version-referencedby>`_
1082
+ """ # noqa: E501
1083
+
1084
+ query = {'offset' : offset , 'limit' : limit }
1085
+ return await self ._rest_client .get ('subjects/{}/versions/{}/referencedby' .format (
1086
+ _urlencode (subject_name ), version ), query )
1087
+
1088
+ async def get_versions (
1089
+ self , subject_name : str , deleted : bool = False , deleted_only : bool = False , offset : int = 0 , limit : int = - 1
1090
+ ) -> List [int ]:
957
1091
"""
958
1092
Get a list of all versions registered with this subject.
959
1093
960
1094
Args:
961
1095
subject_name (str): Subject name.
1096
+ deleted (bool): Whether to include deleted schemas.
1097
+ deleted_only (bool): Whether to return deleted versions only. If both deleted and deleted_only are True, deleted_only takes precedence.
1098
+ offset (int): Pagination offset for results.
1099
+ limit (int): Pagination size for results. Ignored if negative.
962
1100
963
1101
Returns:
964
1102
list(int): Registered versions
@@ -970,7 +1108,8 @@ async def get_versions(self, subject_name: str) -> List[int]:
970
1108
`GET Subject Versions API Reference <https://docs.confluent.io/current/schema-registry/develop/api.html#post--subjects-(string-%20subject)-versions>`_
971
1109
""" # noqa: E501
972
1110
973
- return await self ._rest_client .get ('subjects/{}/versions' .format (_urlencode (subject_name )))
1111
+ query = {'deleted' : deleted , 'deleted_only' : deleted_only , 'offset' : offset , 'limit' : limit }
1112
+ return await self ._rest_client .get ('subjects/{}/versions' .format (_urlencode (subject_name )), query )
974
1113
975
1114
async def delete_version (self , subject_name : str , version : int , permanent : bool = False ) -> int :
976
1115
"""
0 commit comments