from requests.exceptions import ConnectionError, InvalidURL from requests.utils import prepend_scheme_if_needed from lib.common import ( UNEXPECTED_EXCEPTION, chunks, format_query_fields, get_categories_table, get_next_id, get_recipes_table, root_logger, verify_parameters, ) from lib.types import CategoryEntry, RecipeEntry, Response, ResponseData logging = root_logger.getChild("recipes") editable_recipe_fields = { "name": str, "categories": list, "desc": str, "ingredients": list, "instructions": list, "imgSrc": str, "cookTime": str, "yield": str, "adaptedFrom": str, "url": str, } IMAGE_PREFIX = f"https://{os.environ.get('images_bucket_name', '')}.s3-us-west-2.amazonaws.com/"
from typing import Iterable, List, Mapping, MutableMapping, Optional, Union from boto3.dynamodb.conditions import Attr, Key from botocore.exceptions import ClientError from lib.common import ( UNEXPECTED_EXCEPTION, format_query_fields, get_categories_table, get_next_id, root_logger, verify_parameters, ) from lib.types import CategoryEntry, Response, ResponseData logging = root_logger.getChild("categories") editable_category_fields = {"name": str} def get_categories(user_id: str) -> Response: """ GET all categories in the database. :param user_id: ID of the user :return: (Response specifics, status code) """ logging.info(f"Getting all categories for user with ID {user_id}.") kwargs = format_query_fields( ["categoryId", "name", "updateTime", "createTime"])
import re from typing import List, Union from flask import request from flask_restx import Namespace, Resource from recipe_scrapers import NoSchemaFoundInWildMode, scrape_me from requests.exceptions import ConnectionError, InvalidURL from requests.utils import prepend_scheme_if_needed from lib.common import pformat_ as pformat, root_logger from lib.types import ResponseData from models import invalid_input_model, register_scrape_response_models, scrape_response_model logging = root_logger.getChild("scrape") api = Namespace("scrape", description="Scraping related operations.") register_scrape_response_models(api) @api.route("") @api.response(400, "Invalid Input", invalid_input_model) class Scrape(Resource): @api.param("url", "URL to scrape.") @api.marshal_with(scrape_response_model, skip_none=True) def get(self): """Scrape a url for recipe info.""" def _normalize_list(list_: Union[str, List[str]]) -> List[str]: """Normalize a list or string with possible leading markers to just a list.""" return ( [re.sub(r"^\d+[.:]? ?", "", entry) for entry in list_.split("\n")]
import os from datetime import datetime from typing import Final, Mapping import boto3 from boto3_type_annotations.cognito_idp import Client as CognitoClient from botocore.exceptions import ParamValidationError from lib.common import UNEXPECTED_EXCEPTION, get_meta_table, root_logger, verify_parameters from lib.types import Response, ResponseData logging = root_logger.getChild("auth") CLIENT_ID: Final[str] = os.environ.get("client_id") USER_POOL_ID: Final[str] = os.environ.get("user_pool_id") def sign_up(body: Mapping[str, str]) -> Response: """ Sign up using email and password. :param body: Mapping containing email and password keys :return: (Response specifics, status code) """ logging.info("Starting sign up flow.") email, password = verify_parameters(body, "email", "password") client: CognitoClient = boto3.client("cognito-idp") try: res = client.sign_up(ClientId=CLIENT_ID, Username=email,
from typing import Iterable from boto3.dynamodb.conditions import Attr from lib.common import format_query_fields, get_meta_table, root_logger, verify_parameters from lib.types import Response, ResponseData logging = root_logger.getChild("shopping_list") def get_shopping_list(user_id: str) -> Response: """ GET all shopping list items in the database. :param user_id: ID of the user :return: (Response specifics, status code) """ logging.info(f"Getting shopping list for user with ID {user_id}.") kwargs = format_query_fields(["shoppingList"]) meta_table, _ = get_meta_table() shopping_list = ( meta_table.get_item(Key={"userId": user_id}, **kwargs) .get("Item", {}) .get("shoppingList", []) ) return ResponseData(data={"shoppingList": shopping_list}), 200 def patch_shopping_list(user_id: str, body: Iterable[str]) -> Response: