async def get(self): with self.make_session() as session: user_objects = await as_future(session.query(User).all) # transforming into JSON-serializable objects if len(user_objects) != 0: schema = UserSchema(many=True) users = schema.dump(user_objects) self.respond(users.data, "Success", 200) else: self.respond(msg="No Users!")
async def get(self, uid): with self.make_session() as session: user_object = await as_future( (session.query(User).filter(User.id == uid).first)) # transforming into JSON-serializable objects if user_object is not None: schema = UserSchema(only=("id", "username", "email", "gravatar")) user = schema.dump(user_object) self.respond(user.data, "Success", 200) else: self.respond(msg="No User with that id!")
def getUsers(): #pra manusear os argumentos do flask try: if int(request.args['per_page']): per_page = int(request.args['per_page']) except: per_page = 3 try: if int(request.args['page']): page = int(request.args['page']) except: page = 1 #pegando os dados da API web req = urllib.request.Request(API_URL.format(page, per_page), headers=headers) with urllib.request.urlopen(req) as response: data = json.loads(response.read().decode()) api_users = data['data'] api_total = int(data['total']) #pegando os dados do TINYDB tinydb_users = db.table('users').all() # transofrmando em json schema = UserSchema(many=True) dados = schema.dump(api_users) #montando a resposta final total = api_total + len(db.table('users')) data = api_users #juntando as duas API caso a URL não for suficiente if len(api_users) != per_page: if (len(api_users) != 0): datamin = 0 else: datamin = ceil(((page * per_page) - api_total) / per_page) - 1 datamax = datamin + per_page dadostotal = api_users + tinydb_users data = dadostotal[datamin:datamax] res = { "page": page, 'per_page': per_page, "total": total, 'total_pages': ceil(total / per_page), "data": data } return jsonify(res)
def getUser(id): per_page = 1 page = id #pegando os dados da API web req = urllib.request.Request(API_URL.format(page, per_page), headers=headers) with urllib.request.urlopen(req) as response: data = json.loads(response.read().decode()) api_users = data['data'] api_total = int(data['total']) #pegando os dados do TINYDB tinydb_users = db.table('users').all() # transofrmando em json schema = UserSchema(many=True) dados = schema.dump(api_users) #montando a resposta final total = api_total + len(db.table('users')) data = api_users #juntando as duas API caso a URL não for suficiente if len(api_users) != per_page: if (len(api_users) != 0): datamin = 0 else: datamin = ceil(((page * per_page) - api_total) / per_page) - 1 datamax = datamin + per_page dadostotal = api_users + tinydb_users data = dadostotal[datamin:datamax] res = data if (len(data) == 0): abort(404) else: return jsonify(res)