M
M
Matvey Ustinov2016-10-19 10:57:57
API
Matvey Ustinov, 2016-10-19 10:57:57

Flask API and Chat. How to implement, where to dig?

Hello guys.
I am doing a test.
Need help on the steps and methods of chat implementation, where the REST API implemented in Flask acts as a backend.
I somehow dealt with api on DRF (Django Rest Framwork), but I worked purely with the backend and api. It was somehow simpler there - a request came from the client, the server sent a response. And how the front-end client works there didn’t really bother me. And here by me, according to the instructions, the front-end client should also be implemented on Flask.
I started to implement it, but somehow I got confused how this whole thing should turn.
Now I have several views. But the problem is that they use templates to render pages on the client. That is, everything is mixed up in a heap and there is no separation of the client and backend api.

For example, let's take the stage with registration: there is an address 127.0.0.1/register
When you enter this page, a form with fields is displayed. Further, after the user clicks on the send button, the view performs a simple validation, enters the data into the database, sets the session cookie for the client, and redirects to the main chat page via a redirect.
And everything would be fine, but according to the instructions, everything should be a little different. In theory, the client should have sent json with a request to 127.0.0.1/api/register where the server should have sent something in response. And then something has to happen.
This is exactly how I would like to implement this task, but I don’t understand at all how to implement such a front work with an apish.

At the moment, the view code looks like this pastebin.com/UGt8qPhU
I would be very grateful for tips and advice.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2016-10-19
@matlex

In order for the server to issue json, and not render templates, it is enough just to return jsonify() via flask.
For example, I have this

from flask import jsonify

# Begin AJAX requests
@cart.route("/api/total")
def total():
    count = 0
    if "cart" in session:
        cart = session.get("cart")
        l = [int(i) for i in redis.hvals(getCartKey(cart))]
        count = sum(l)
        logger.debug('cart %s : %d', cart, count)
    data = calculateItems()
    data.pop("products")
    return jsonify(dict(count=count, **data))

Well, the description of jsonify from the flask code.
"""Creates a :class:`~flask.Response` with the JSON representation of
the given arguments with an `application/json` mimetype. The arguments
to this function are the same as to the :class:`dict` constructor.
Example usage::
from flask import jsonify
@app.route('/_get_current_user')
def get_current_user():
return jsonify(username=g.user.username,
email=g.user.email,
id=g.user.id)
This will send a JSON response like this to the browser::
{
"username": "admin",
"email": "[email protected]",
"id": 42
}
For security reasons only objects are supported toplevel. For more
information about this, have a look at :ref:`json-security`.
This function's response will be pretty printed if it was not requested
with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless
the ``JSONIFY_PRETTYPRINT_REGULAR`` config parameter is set to false.
..versionadded::0.2
"""

Regarding messages and waiting for them on the server side, there is a technique called long pooling , or use a websocket (but the same applies to it!"), and to implement it, you either need to run flask in threading mode app.run(threading=True) , or use a gevent-based WSGI wrapper,
as an example - https://bitbucket.org/jeunice/flask-ws-example/src...
and
https://github.com/sigilioso/long_polling_example

P
planc, 2016-10-19
@planc

https://classroom.udacity.com/courses/ud388/
https://blog.miguelgrinberg.com/post/designing-ar...
shop.oreilly.com/product/0636920034803.do - there is a root tracker

K
KIN1991, 2016-10-19
@KIN1991

Try this one https://pypi.python.org/pypi/json-rpc/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question