Skip to content

Commit

Permalink
Add functional redis push and pull!
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireye04 committed Nov 20, 2024
1 parent e791d3b commit 8f1b9db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 11 additions & 14 deletions src/sasquatchbackpack/commands/usgs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""USGS CLI."""

import asyncio
from datetime import timedelta

import click
Expand Down Expand Up @@ -214,19 +213,17 @@ def test_redis() -> None:

# for record in records:
# Using earthquake id as redis key

record = records[0]
asyncio.run(
erm.store(
record["value"]["id"],
schemas.EarthquakeSchema(
timestamp=record["value"]["timestamp"],
id=record["value"]["id"],
latitude=record["value"]["latitude"],
longitude=record["value"]["longitude"],
depth=record["value"]["depth"],
magnitude=record["value"]["depth"],
),
erm.store(
record["value"]["id"],
schemas.EarthquakeSchema(
timestamp=record["value"]["timestamp"],
id=record["value"]["id"],
latitude=record["value"]["latitude"],
longitude=record["value"]["longitude"],
depth=record["value"]["depth"],
magnitude=record["value"]["depth"],
),
debug=True,
)

erm.get(record["value"]["id"])
12 changes: 7 additions & 5 deletions src/sasquatchbackpack/schemas/usgs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""USGS Schemas."""

import asyncio

import redis.asyncio as redis
from dataclasses_avroschema.pydantic import AvroBaseModel
from pydantic import Field
Expand Down Expand Up @@ -32,14 +34,14 @@ def __init__(self, address: str) -> None:
self.model = PydanticRedisStorage(
datatype=EarthquakeSchema, redis=connection
)
self.loop = asyncio.new_event_loop()

async def store(self, key: str, item: EarthquakeSchema) -> bool:
def store(self, key: str, item: EarthquakeSchema) -> None:
if self.model is None:
raise RuntimeError("Model is undefined.")
await self.model.store(key, item)
return True
self.loop.run_until_complete(self.model.store(key, item))

async def get(self, key: str) -> EarthquakeSchema:
def get(self, key: str) -> EarthquakeSchema:
if self.model is None:
raise RuntimeError("Model is undefined.")
return await self.model.get(key)
return self.loop.run_until_complete(self.model.get(key))

0 comments on commit 8f1b9db

Please sign in to comment.