from typing import Optional from datetime import datetime from typing import Dict, Any from flask import abort import hashlib def get_amount_from_string(string: str) -> Optional[float]: """ Returns the price amount from a string. :param string: string to parse out the data """ try: string_split = string.split('.') if len(string_split) == 1: return float(string_split[0]) if len(string_split) != 2: raise Exception() return float(f"{string_split[0]}.{string_split[1]}") except: return None def get_date_from_string(string: str) -> Optional[datetime]: """ Returns a type datetime with format YEAR-MONTH-DAY from a string. :param string: string to parse out the date. """ try: return datetime.strptime(string, '%Y-%m-%d') except: return None def get_string_from_date(date: datetime) -> str: """ Returns a string of the date with format YEAR-MONTH-DAY. :param date: date that we would like to parse. """ return f"{date.year}-{date.month}-{date.day}" def create_encrypted_string(string: str) -> str: """ Returns an encrypted string with a provided string. :param string: string that would need to be encrypted """ hash_object = hashlib.sha256() hash_object.update(string.encode()) hash_string = hash_object.hexdigest() return hash_string def json_success_response( data: Dict[str,Any]={}, status_code: int = 200 ) -> Dict[str, Any]: """ Returns a SUCCESS json response. :param data: dictionary that holds the data for the response. (default: {}) :param status_code: status code for the response (default: 200) """ return { 'status': 'SUCCESS', 'status_code': status_code, "data": data, } def json_error_response( string: str, status_code: int= 500 ) -> Dict[str, Any]: """ Returns an ERROR json response. :param string: error message :param status_code: status code for the response (default: 500) """ abort(status_code, string)