Skip to content

Commit

Permalink
Merge pull request #213 from aiven/schema-versions
Browse files Browse the repository at this point in the history
Schema versions

#213
  • Loading branch information
Augusto Hack authored May 12, 2021
2 parents fa75f25 + a29b390 commit 90e92a7
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 81 deletions.
38 changes: 38 additions & 0 deletions karapace/schema_registry_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def _add_routes(self):
self.route("/config/<subject:path>", callback=self.config_subject_set, method="PUT", schema_request=True)
self.route("/config", callback=self.config_get, method="GET", schema_request=True)
self.route("/config", callback=self.config_set, method="PUT", schema_request=True)
self.route(
"/schemas/ids/<schema_id:path>/versions", callback=self.schemas_get_versions, method="GET", schema_request=True
)
self.route("/schemas/ids/<schema_id:path>", callback=self.schemas_get, method="GET", schema_request=True)
self.route("/schemas/types", callback=self.schemas_types, method="GET", schema_request=True)
self.route("/subjects", callback=self.subjects_list, method="GET", schema_request=True)
Expand Down Expand Up @@ -336,6 +339,41 @@ async def schemas_get(self, content_type, *, schema_id):
response_body["schemaType"] = schema.schema_type
self.r(response_body, content_type)

async def schemas_get_versions(self, content_type, *, schema_id):
try:
schema_id_int = int(schema_id)
except ValueError:
self.r(
body={
"error_code": SchemaErrorCodes.HTTP_NOT_FOUND.value,
"message": "HTTP 404 Not Found",
},
content_type=content_type,
status=HTTPStatus.NOT_FOUND,
)

subject_versions = []
with self.ksr.id_lock:
for subject, val in self.ksr.subjects.items():
if self.ksr.get_schemas(subject) and "schemas" in val:
schemas = val["schemas"]
for version, schema in schemas.items():
if int(schema["id"]) == schema_id_int and not schema["deleted"]:
subject_versions.append({"subject": subject, "version": int(version)})

if not subject_versions:
self.r(
body={
"error_code": SchemaErrorCodes.HTTP_NOT_FOUND.value,
"message": "HTTP 404 Not Found",
},
content_type=content_type,
status=HTTPStatus.NOT_FOUND,
)

subject_versions = sorted(subject_versions, key=lambda s: (s["subject"], s["version"]))
self.r(subject_versions, content_type)

async def schemas_types(self, content_type):
self.r(["JSON", "AVRO"], content_type)

Expand Down
Loading

0 comments on commit 90e92a7

Please sign in to comment.