-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add events API requests (#677)
* feat: add events API requests see /~https://github.com/openfoodfacts/openfoodfacts-events
- Loading branch information
Showing
9 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from multiprocessing import Process, SimpleQueue | ||
|
||
import requests | ||
|
||
from robotoff import settings | ||
from robotoff.utils import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
class EventProcessor: | ||
"""Send events in an outside process""" | ||
|
||
# the process and queue to send events | ||
process = None | ||
queue = None | ||
|
||
def get(self): | ||
"""Start a process to handle events, but only when needed, | ||
and return communication pipe | ||
""" | ||
if self.process is None: | ||
self.queue = SimpleQueue() | ||
# Create a daemonic process | ||
self.process = Process(target=send_events, args=(self.queue,), daemon=True) | ||
self.process.start() | ||
return self.queue | ||
|
||
def send_async(self, *args, **kwargs): | ||
if settings.EVENTS_API_URL: | ||
queue = self.get() | ||
queue.put((settings.EVENTS_API_URL, args, kwargs)) | ||
|
||
|
||
# a singleton for event processor | ||
event_processor = EventProcessor() | ||
|
||
|
||
def send_events(queue): | ||
"""Loop to send events in a specific process""" | ||
while True: | ||
api_url, args, kwargs = queue.get() | ||
send_event(api_url, *args, **kwargs) | ||
|
||
|
||
def send_event( | ||
api_url: str, | ||
event_type: str, | ||
user_id: str, | ||
device_id: str, | ||
barcode: str = None, | ||
): | ||
event = { | ||
"event_type": event_type, | ||
"user_id": user_id, | ||
"device_id": device_id, | ||
"barcode": barcode, | ||
} | ||
logger.debug(f"Event: {event}") | ||
response = requests.post(api_url, json=event) | ||
logger.debug(f"Event API response: {response}") | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters