Skip to content

Commit ae74230

Browse files
committed
connection init
1 parent 6729588 commit ae74230

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/sasquatchbackpack/sasquatch.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
__all__ = ["BackpackDispatcher", "DataSource", "DispatcherConfig"]
44

55
import asyncio
6+
import json
67
import os
78
from abc import ABC, abstractmethod
89
from dataclasses import dataclass, field
910
from string import Template
1011

1112
import redis.asyncio as redis
1213
import requests
14+
from faststream.kafka import KafkaBroker
15+
from safir.kafka import KafkaConnectionSettings, SecurityProtocol
1316

1417
# Code yoinked from https://github.com/lsst-sqre/
1518
# sasquatch/blob/main/examples/RestProxyAPIExample.ipynb
@@ -215,6 +218,25 @@ def _get_source_records(self) -> list[dict] | None:
215218
if self.redis.get(self.source.get_redis_key(record)) is None
216219
]
217220

221+
async def direct_connect(self) -> None:
222+
"""Assemble a schema and payload from the given source,
223+
and route data directly to kafka.
224+
"""
225+
config = KafkaConnectionSettings(
226+
bootstrap_servers=os.getenv("KAFKA_BOOTSTRAP_SERVERS", ""),
227+
security_protocol=SecurityProtocol.SSL,
228+
)
229+
kafka_broker = KafkaBroker(**config.to_faststream_params())
230+
231+
records = self._get_source_records()
232+
if records is None:
233+
return
234+
if len(records) == 0:
235+
return
236+
237+
payload = json.dumps({"value_schema": self.schema, "records": records})
238+
await kafka_broker.publish(payload, self.source.topic_name)
239+
218240
def post(self) -> tuple[str, list]:
219241
"""Assemble schema and payload from the given source, then
220242
makes a POST request to kafka.

src/sasquatchbackpack/sources/usgs/commands.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""USGS CLI."""
22

3-
import os
3+
import asyncio
44
from datetime import UTC, datetime, timedelta
55

66
import click
@@ -199,12 +199,11 @@ def usgs_earthquake_data(
199199

200200
click.echo("Post mode enabled: Sending data...")
201201
click.echo(f"Querying redis at {backpack_dispatcher.redis.address}")
202-
click.echo(
203-
os.getenv(
204-
"SASQUATCH_REST_PROXY_URL",
205-
"no env var :(",
206-
)
207-
)
202+
loop = asyncio.new_event_loop()
203+
loop.run_until_complete(backpack_dispatcher.direct_connect())
204+
205+
click.echo("complete")
206+
208207
result, records = backpack_dispatcher.post()
209208

210209
if "Error" in result:

0 commit comments

Comments
 (0)