import datetime import os from flakon import SwaggerBlueprint from flask import jsonify, request, abort from sqlalchemy import or_, and_ from UsersService.database import db, User, Follower YML = os.path.join(os.path.dirname(__file__), '..', 'static', 'API_users.yaml') users = SwaggerBlueprint('users', '__name__', swagger_spec=YML) # Return the list of all users @users.operation('getUsers') def _users(): usrs = db.session.query(User) return jsonify([user.serialize() for user in usrs]) # Create a new user @users.operation('createUser') def _create_user(): try: user_data = request.get_json(request) print(user_data) # check user existence q = db.session.query(User).filter(User.email == user_data['email']) user = q.first() if user is not None:
import os from flakon import SwaggerBlueprint from flask import request, jsonify from beepbeep.dataservice.database import db, Objective HERE = os.path.dirname(__file__) YML = os.path.join(HERE, '..', 'static', 'api-swagger.yaml') objectives_api = SwaggerBlueprint('objectives', __name__, swagger_spec=YML) def json_to_objective(db_objective, json_objective, runner_id): db_objective.user_id = runner_id db_objective.distance = json_objective['distance'] return db_objective @objectives_api.operation('getObjectives') def get_objectives(id): objectives = db.session.query(Objective).filter(Objective.user_id == id) return jsonify([objective.to_json() for objective in objectives]) @objectives_api.operation('setObjective') def add_objective(id): runner_id = id objective = request.get_json() db_objective = json_to_objective(Objective(), objective, runner_id)
import os from datetime import datetime, timedelta from flakon import SwaggerBlueprint from flakon.request_utils import get_request_retry, runs_endpoint, users_endpoint from flask import request, jsonify, abort import requests from decimal import Decimal from beepbeep.trainingobjectiveservice.database import db, Training_Objective, Last_Run import json HERE = os.path.dirname(__file__) YML = os.path.join(HERE, '..', 'static', 'api.yaml') api = SwaggerBlueprint('API', __name__, swagger_spec=YML) def check_runner_id(runner_id, send_get=True): if int(runner_id) <= 0: abort(400, 'Invalid runner_id') if send_get: if db.session.query(Last_Run).filter( Last_Run.runner_id == runner_id).count() != 0: return try: response = get_request_retry(users_endpoint(runner_id)) status_code = response.status_code except requests.exceptions.RequestException as ex: abort(503, str(ex))
import requests from flakon import SwaggerBlueprint from flask import render_template, redirect, url_for from flask_login import current_user, login_required from APIGateway.urls import * usersapi = SwaggerBlueprint('users', '__name__', swagger_spec=os.path.join(YML_PATH, 'users-api.yaml')) # Renders a page (users.html) with a list of all the users @usersapi.operation('getAll') def _get_all_users(): try: x = requests.get(USER_URL + '/users') except requests.exceptions.ConnectionError: return service_not_up() users = [] if check_service_up(x): users = x.json() return render_template("users.html", users=users, home_url=GATEWAY_URL) # Renders a page (wall.html) with the wall of a specified user @usersapi.operation('getUser') def _get_user(id_user):
import datetime import os import string from random import randint import requests from flakon import SwaggerBlueprint from flask import request, jsonify, abort from sqlalchemy import func, desc, and_ from StoriesService.database import db, Story YML = os.path.join(os.path.dirname(__file__), '.', 'stories-service-api.yaml') stories = SwaggerBlueprint('stories', '__name__', swagger_spec=YML) NEW_REACTIONS_URL = "http://127.0.0.1:5004/new" DELETE_REACTIONS_URL = "http://127.0.0.1:5004/delete" # Check validity of a text def check_validity(text, figures): message = None if len(text) > 1000: message = 'Story is too long' else: dice_figures = figures.split('#')[1:-1] trans = str.maketrans(string.punctuation, ' ' * len(string.punctuation)) new_s = text.translate(trans).lower() story_words = new_s.split() for w in story_words:
import os from flakon import SwaggerBlueprint from flask import request, jsonify from beepbeep.dataservice.database import db, Challenge, User HERE = os.path.dirname(__file__) YML = os.path.join(HERE, '..', 'static', 'api-swagger.yaml') challenges_api = SwaggerBlueprint('challenges', __name__, swagger_spec=YML) def is_valid_user_id(user_id): return db.session.query(User).filter( User.id == user_id).first() is not None def json_to_challenge(db_challenge, json_challenge, runner_id_): db_challenge.runner_id = runner_id_ db_challenge.run_id = json_challenge['run_id'] db_challenge.latest_run_id = json_challenge['latest_run_id'] return db_challenge @challenges_api.operation('getChallenges') def get_challenges(id): challenges = db.session.query(Challenge).filter(Challenge.runner_id == id) return jsonify([challenge.to_json() for challenge in challenges]) @challenges_api.operation('addChallenge')
from flakon import SwaggerBlueprint from flask import request, jsonify, abort from flask import current_app as app from reactions.database import db, Like, Dislike from sqlalchemy.sql.expression import func import requests from jsonschema import validate, ValidationError reactions = SwaggerBlueprint('reactions', __name__, swagger_spec='./reactions/react-specs.yaml') """ The route can be used by a logged in user to like a published story. """ @reactions.operation('like') def _like(): if general_validator('like', request): json_data = request.get_json() story_id = json_data['story_id'] current_user_id = json_data['user_id'] try: r = app.request.get_story(story_id, current_user_id) if r.status_code == 404: abort(404) q = Like.query.filter_by(liker_id=current_user_id, story_id=story_id) if q.first() is None: new_like = Like()
import requests from flakon import SwaggerBlueprint from flask import render_template, request, redirect, url_for, session from flask_login import login_required, current_user from APIGateway.forms import StoryForm from APIGateway.tasks import reaction_task from APIGateway.urls import * storiesapi = SwaggerBlueprint('stories', '__name__', swagger_spec=os.path.join( YML_PATH, 'stories-api.yaml')) # Renders the Stories page (stories.html), where ALL the published stories are seen @storiesapi.operation('getAll') def _get_all_stories(): try: x = requests.get(STORY_URL + '/stories') except requests.exceptions.ConnectionError: return service_not_up() stories = [] if check_service_up(x): stories = x.json() return render_template("stories.html", stories=stories, home_url=GATEWAY_URL)
import os from datetime import datetime from flakon import SwaggerBlueprint from flask import request, jsonify from beepbeep.dataservice.database import db, Run from sqlalchemy import func import json from json import loads HERE = os.path.dirname(__file__) YML = os.path.join(HERE, '..', 'static', 'api-swagger.yaml') runs_api = SwaggerBlueprint('runs', __name__, swagger_spec=YML) def json_to_run(db_run, json_run, runner_id): db_run.strava_id = json_run['strava_id'] db_run.distance = json_run['distance'] db_run.start_date = datetime.fromtimestamp(json_run['start_date']) db_run.elapsed_time = json_run['elapsed_time'] db_run.average_speed = json_run['average_speed'] db_run.average_heartrate = json_run['average_heartrate'] db_run.total_elevation_gain = json_run['total_elevation_gain'] db_run.runner_id = runner_id db_run.title = json_run['title'] db_run.description = json_run['description'] return db_run
import os from flakon import SwaggerBlueprint from flask import request, jsonify from beepbeep.dataservice.database import db, User, Run, Challenge, Objective import requests HERE = os.path.dirname(__file__) YML = os.path.join(HERE, '..', 'static', 'api-swagger.yaml') users_api = SwaggerBlueprint('users', __name__, swagger_spec=YML) def fill(source, target): for prop in source: setattr(target, prop, source[prop]) @users_api.operation('getUsers') def get_users(): users = db.session.query(User) return jsonify([user.to_json() for user in users]) @users_api.operation('createUser') def create_user(): raw_user = request.get_json() if raw_user is None: return "Invalid user body", 400 user = User() fill(raw_user, user)
import requests from flakon import SwaggerBlueprint from flask import url_for, redirect, render_template, request, session from flask_login import login_required from werkzeug.exceptions import BadRequestKeyError from APIGateway.urls import * diceapi = SwaggerBlueprint('dice', '__name__', swagger_spec=os.path.join(YML_PATH, 'dice-api.yaml')) # Renders the Setting page (settings.html), where the set and number of dice can be chosen. @diceapi.operation('getSettingsPage') @login_required def _get_settings_page(): try: x = requests.get(DICE_URL + '/sets') except requests.exceptions.ConnectionError: return service_not_up() if check_service_up(x): # No dice sets are loaded into the dice microservice if x.status_code == 204: flash("No dice set found. Please contact Jacopo Massa") redirect(url_for('gateway._home')) else: # status_code < 300 ? sets = x.json() return render_template("settings.html", sets=sets, home_url=GATEWAY_URL) else: return redirect(url_for('gateway._home'))
import json import requests from flakon import SwaggerBlueprint from flask import render_template, request from flask_login import current_user, logout_user, login_required, login_user from APIGateway.classes.User import User from APIGateway.forms import UserForm, LoginForm from APIGateway.urls import * authapi = SwaggerBlueprint('gateway', '__name__', swagger_spec=os.path.join(YML_PATH, 'auth-api.yaml')) # Renders the Home page (index.html). # It renders different data, depending whether an user is logged or not. @authapi.operation('home') def _home(): # Stories is an empty list, so it can be iterated in the HTML stories = [] # If there's a logged user, we get his stories if current_user is not None and hasattr(current_user, 'id'): s = requests.get(STORY_URL + '/stories/users/{}'.format(current_user.id)) if check_service_up(s): if s.status_code < 300: