Skip to content

Commit

Permalink
reading-list-updater: Process update requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Oct 6, 2024
1 parent 9e22603 commit eee7250
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions services/reading-list-updater/hive/reading_list_updater/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging

from dataclasses import dataclass
from functools import cached_property
from typing import Callable, Optional

from pika import BasicProperties
from pika.spec import Basic

from hive.mediawiki import HiveWiki
from hive.messaging import Channel, blocking_connection

from .entry import ReadingListEntry
Expand All @@ -17,9 +19,27 @@
@dataclass
class Service:
email_queue: str = "readinglist.emails.received"
updates_queue: str = "readinglist.update.requests"
append_request_queue: str = "readinglist.append.requests"
on_channel_open: Optional[Callable[[Channel], None]] = None

@cached_property
def wiki(self):
return HiveWiki()

def on_append_request(
self,
channel: Channel,
method: Basic.Deliver,
properties: BasicProperties,
body: bytes,
):
content_type = properties.content_type
if content_type != "application/json":
raise ValueError(content_type)
entry = ReadingListEntry.from_json_bytes(body)
wikitext = entry.as_wikitext()
self.wiki.page("Reading list").append(f"* {wikitext}")

def on_email_received(
self,
channel: Channel,
Expand All @@ -33,7 +53,7 @@ def on_email_received(
entry = ReadingListEntry.from_email_bytes(body)
channel.publish_request(
message=entry.as_dict(),
routing_key=self.updates_queue,
routing_key=self.append_request_queue,
)

def run(self):
Expand All @@ -44,4 +64,9 @@ def run(self):
on_message_callback=self.on_email_received,
dead_letter=True,
)
channel.consume_requests(
queue=self.append_request_queue,
on_message_callback=self.on_append_request,
dead_letter=True,
)
channel.start_consuming()
1 change: 1 addition & 0 deletions services/reading-list-updater/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r ../../requirements.txt
-e ../../libs/common
-e ../../libs/config
-e ../../libs/mediawiki
-e ../../libs/messaging
-e ../../libs/service

0 comments on commit eee7250

Please sign in to comment.