Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a try to run the validate in the produce method #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions gcn_kafka/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import certifi
import confluent_kafka
import confluent_kafka.admin
from jsonschema import validate, exceptions
import json
import requests

from .oidc import set_oauth_cb

Expand Down Expand Up @@ -91,6 +94,25 @@ def __init__(
self.poll(0)


def produce_json_notice(self, topic:str, data:dict):
try:
request = requests.get(data['$schema'])
schema_model = json.loads(request.content.decode())
validate(data, schema_model)
except exceptions.ValidationError:
print("The instance of the data provided is invalid against the schema")
return
except exceptions.SchemaError:
print("The provided schema is invalid")
return

notice_id = str(uuid4())
data['notice_id'] = notice_id

super().produce(topic, json.dumps(data))
print(f'Successfully posted topic with notice id: {notice_id}')


class Consumer(confluent_kafka.Consumer):
def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ dependencies = [
"certifi",
"confluent-kafka >= 1.6.1",
"requests",
"typing-extensions; python_version<='3.7'"
"typing-extensions; python_version<='3.7'",
"jsonschema"
]
requires-python = ">=3.7"
dynamic = [ "version" ]
Expand Down