Implement sms and mail requests
This commit is contained in:
38
api-customer/src/controllers/mail_controller.py
Normal file
38
api-customer/src/controllers/mail_controller.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import json
|
||||
|
||||
from board_mate.mail import Configuration, ApiClient, MailApi
|
||||
from flask import request, jsonify
|
||||
from pydantic import StrictStr
|
||||
|
||||
from src.models.AuthData import AuthData
|
||||
|
||||
|
||||
class MailController:
|
||||
_auth_data: AuthData = None
|
||||
|
||||
def __init__(self, app, auth_data, host):
|
||||
self._register_routes(app)
|
||||
self.config = Configuration(host=host)
|
||||
self.config.verify_ssl = False
|
||||
self._auth_data = auth_data
|
||||
|
||||
def _register_routes(self, app):
|
||||
app.add_url_rule("/sms/send", view_func=self.send, methods=['POST'])
|
||||
|
||||
def send(self):
|
||||
try:
|
||||
with ApiClient(self.config) as api:
|
||||
req = request.get_json()
|
||||
content = req["content"]
|
||||
payload = json.dumps({"content": content})
|
||||
print(payload)
|
||||
|
||||
api.default_headers["Authorization"] = f"Bearer {self._auth_data.get_token()}"
|
||||
mail_api = MailApi(api)
|
||||
|
||||
mail_api.send(StrictStr(payload))
|
||||
return jsonify({"success": True, "message": None}), 200
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"success": False,
|
||||
"message": f"An error occurred : {self._auth_data.get_token()} {self._auth_data.get_client_id()} \n {e}"}), 500
|
||||
@@ -1,12 +1,24 @@
|
||||
import json
|
||||
|
||||
from board_mate.sms import ApiClient
|
||||
from flask import request
|
||||
from board_mate.sms import ApiClient, SmsApi, Configuration
|
||||
from flask import request, jsonify
|
||||
from pydantic import StrictStr
|
||||
|
||||
from src.models.AuthData import AuthData
|
||||
|
||||
|
||||
class SmsController:
|
||||
def __init__(self):
|
||||
pass
|
||||
_auth_data: AuthData = None
|
||||
|
||||
def __init__(self, app, auth_data, host):
|
||||
self._register_routes(app)
|
||||
self.config = Configuration(host=host)
|
||||
self.config.verify_ssl = False
|
||||
self._auth_data = auth_data
|
||||
self.is_logged_in = False
|
||||
|
||||
def _register_routes(self, app):
|
||||
app.add_url_rule("/sms/send", view_func=self.send, methods=['POST'])
|
||||
|
||||
def send(self):
|
||||
try:
|
||||
@@ -17,15 +29,9 @@ class SmsController:
|
||||
print(payload)
|
||||
|
||||
api.default_headers["Authorization"] = f"Bearer {self._auth_data.get_token()}"
|
||||
message_api = SmsApi(api)
|
||||
sms_api = SmsApi(api)
|
||||
|
||||
new_message = MessagePostRequestDto(
|
||||
content=content,
|
||||
timeStamp=timestamp,
|
||||
clientId=StrictStr(self._auth_data.get_client_id())
|
||||
)
|
||||
|
||||
message_api.post_message(new_message)
|
||||
sms_api.example(StrictStr(payload))
|
||||
return jsonify({"success": True, "message": None}), 200
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
Reference in New Issue
Block a user