Skip to content

Commit

Permalink
that work arround won't work...add social service
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed Apr 13, 2024
1 parent 509b87e commit a08b823
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ LOGGING_LEVEL=
USERS_HOST=
MEASUREMENTS_HOST=
PLANTS_HOST=
SOCIAL_HOST=
JWT_SECRET=
HASH_ALGORITHM=
57 changes: 57 additions & 0 deletions src/apps/social.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import logging

import requests
from flask import make_response


def get_query_params(queryParam) -> str:
if not queryParam:
return ""
return f"?{str(queryParam, 'utf-8')}"


class Social:
def __init__(self):
self.host = os.getenv("SOCIAL_HOST")

def getResponseJson(self, response):
if response.status_code == 503 or not response.text:
return {"message": "social service is currently unavailable,"
" please try again later", "status": 503}
return response.json()

def get(self, url, body, headers, query_params):
url = f"{self.host}{url}{get_query_params(query_params)}"
logging.info(f"SOCIAL | GET | {url}")
response = requests.get(url, json=body, headers=headers)
return make_response(self.getResponseJson(response),
response.status_code)

def post(self, url, body, headers, query_params):
response = requests.post(f"{self.host}{url}"
f"{get_query_params(query_params)}",
json=body,
headers=headers)
logging.info(f"SOCIAL | POST | {url}")
logging.debug(f"BODY: {body}")
return make_response(self.getResponseJson(response),
response.status_code)

def patch(self, url, body, headers, query_params):
response = requests.patch(f"{self.host}{url}"
f"{get_query_params(query_params)}",
json=body,
headers=headers)
logging.info(f"SOCIAL | PATCH | {url}")
logging.debug(f"BODY: {body}")
return make_response(self.getResponseJson(response),
response.status_code)

def delete(self, url, body, headers, query_params):
response = requests.delete(f"{self.host}{url}"
f"{get_query_params(query_params)}",
headers=headers)
logging.info(f"SOCIAL | DELETE | {url}")
return make_response(self.getResponseJson(response),
response.status_code)
3 changes: 2 additions & 1 deletion src/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src.apps.users import Users
from src.apps.measurements import Measurements
from src.apps.plants import Plants
from src.apps.social import Social


def getExtraData():
Expand All @@ -29,7 +30,7 @@ def getExtraData():
"logs": Plants(),
"login": Users(),
# TODO: Add the new service
"socal": Measurements()
"social": Social()
}


Expand Down

0 comments on commit a08b823

Please sign in to comment.