Skip to content

Commit 0873366

Browse files
committed
add user service
1 parent 09f0bc3 commit 0873366

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

.env.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DATABASE_URL=
1313

1414
# External services
1515
PLANT_SERVICE_URL=
16+
USERS_SERVICE_URL=
1617

1718
# RabbitMQ
1819
CLOUDAMQP_URL=

app/schemas/user.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pydantic import BaseModel, Field
2+
3+
4+
class UserSchema(BaseModel):
5+
id: int = Field(...)
6+
name: str = Field(...)
7+
device_token: str = Field(...)

app/service/rabbitmq/consumer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from ..users import UsersService
23
from exceptions.logger_messages import LoggerMessages
34
import pydantic
45
import logging
@@ -34,6 +35,7 @@ def __init__(self, queue_name):
3435
self.__queue_name = queue_name
3536
self.__middleware = Middleware()
3637
self.__sqlAlchemyClient = SQLAlchemyClient()
38+
self.__users = UsersService()
3739

3840
def run(self):
3941
self.__middleware.create_queue(self.__queue_name)
@@ -50,6 +52,9 @@ def obtain_device_plant(self, measurement_from_rabbit):
5052
logger.error(f"{err} - {type(err)}")
5153
raise RowNotFoundError(measurement_from_rabbit.id_device, "DEVICE_PLANT")
5254

55+
async def obtain_user(self, user_id):
56+
return await self.__users.get_user(user_id)
57+
5358
def check_package(self, measurement):
5459
empty_values = []
5560
if measurement.temperature is None:
@@ -102,11 +107,14 @@ def generate_notification_body(self, error):
102107
return f"Los siguientes parámetros están altos: {high_msg}."
103108

104109
def send_notification(self, id_user, measurement, error, details):
110+
device_plant = self.obtain_device_plant(measurement)
111+
112+
user = self.obtain_user(device_plant.id_user)
105113

106114
notification_body = self.generate_notification_body(error)
107115

108116
try:
109-
if measurement.device_token is not None:
117+
if user.device_token is not None:
110118
message = messaging.Message(
111119
notification=messaging.Notification(title="Estado de tu planta",
112120
body=notification_body),

app/service/users.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import logging
2+
from httpx import (
3+
AsyncClient,
4+
codes,
5+
HTTPStatusError
6+
)
7+
from os import environ
8+
from fastapi import HTTPException
9+
from typing import Optional
10+
from schemas.user import UserSchema
11+
12+
logger = logging.getLogger("app")
13+
logger.setLevel("DEBUG")
14+
15+
USERS_SERVICE_URL = environ["USERS_SERVICE_URL"]
16+
17+
18+
class UsersService():
19+
@staticmethod
20+
async def get_user(user_id: int) -> Optional[UserSchema]:
21+
try:
22+
async with AsyncClient() as client:
23+
response = await client.get(
24+
USERS_SERVICE_URL + f"/users/{user_id}"
25+
)
26+
if response.status_code == codes.OK:
27+
return UserSchema(**response.json())
28+
elif response.status_code == codes.NOT_FOUND:
29+
return None
30+
else:
31+
return response.raise_for_status().json()
32+
33+
except HTTPStatusError as e:
34+
logger.error(
35+
"Users service cannot be accessed because: " + str(e)
36+
)
37+
raise HTTPException(
38+
status_code=e.response.status_code,
39+
detail=e.response.content.decode(),
40+
)

0 commit comments

Comments
 (0)