Skip to content

Commit

Permalink
Add dynamic payload construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireye04 committed May 23, 2024
1 parent 8674248 commit 8e78ab0
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion src/sasquatchbackpack/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import timedelta

import click
Expand All @@ -10,6 +11,8 @@

DEFAULT_MAGNITUDE_BOUNDS = (2, 10)

DEFAULT_TEST = False


def check_duration(
ctx: click.Context, param: dict, value: tuple[int, int]
Expand Down Expand Up @@ -155,11 +158,20 @@ def main() -> None:
show_default=True,
callback=check_magnitude_bounds,
)
@click.option(
"-t",
"--test",
help="set to True to echo API results without sending them",
default=DEFAULT_TEST,
type=bool,
show_default=True,
)
def usgs_earthquake_data(
duration: tuple[int, int],
radius: int,
coords: tuple[float, float],
magnitude_bounds: tuple[int, int],
test: bool,
) -> None:
"""Seaches USGS databases for relevant earthquake data and prints it
to console
Expand All @@ -180,10 +192,74 @@ def usgs_earthquake_data(
click.echo(result)
click.echo("------")
else:
click.secho("SUCCESS", fg="orange")
click.secho("SUCCESS! (kinda)", fg="yellow")
click.echo("------")
click.echo("No results found for the provided criteria :(")
click.echo("------")
return

if not test:
click.echo("Sending data...")

# sasquatch_rest_proxy_url = (
# "https://data-int.lsst.cloud/sasquatch-rest-proxy"
# )

namespace = "lsst.example"
topic_name = "usgs-earthquake-data"

value_schema = json.dumps(
{
"namespace": f"{namespace}",
"type": "record",
"name": f"{topic_name}",
"description": "Collection of earthquakes near the "
+ "summit",
"fields": [
{"name": "timestamp", "type": "long"},
{"name": "id", "type": "str"},
{"name": "latitude", "type": "float"},
{"name": "longitude", "type": "float"},
{"name": "depth", "type": "float", "units": "Km"},
{"name": "magnitude", "type": "float"},
],
}
)

# url = f"{sasquatch_rest_proxy_url}/topics/{namespace}.{topic_name}"

# headers = {
# "Content-Type": "application/vnd.kafka.avro.v2+json",
# "Accept": "application/vnd.kafka.v2+json",
# }

records = []

for result in results:
records.append(
{
"value": {
"timestamp": result.time.strftime("%s"),
"id": result.id,
"latitude": result.latitude,
"longitude": result.longitude,
"depth": float(result.depth),
"magnitude": float(result.magnitude),
}
}
)

payload = {"value_schema": value_schema, "records": records}

print(payload)

# response = requests.request("POST",
# url,
# json=payload,
# headers=headers
# )

# print(response.text)


if __name__ == "__main__":
Expand Down

0 comments on commit 8e78ab0

Please sign in to comment.