Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
But it is not necessary, instance of MongoClient
can be created in global scope and imported into functions in same way like you did with db_conn()
.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
I would also recommend putting USER
, PASS
, MONGO_URI
, PORT
into configuration file, so they can be easily overridden by environment variables on production.
Also Artyom24 Artyom24 pointed out nice improvements regarding API RESTfulness
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
But it is not necessary, instance of MongoClient
can be created in global scope and imported into functions in same way like you did with db_conn()
.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
I would also recommend putting USER
, PASS
, MONGO_URI
, PORT
into configuration file, so they can be easily overridden by environment variables on production.
Also Artyom24 pointed out nice improvements regarding API RESTfulness
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
But it is not necessary, instance of MongoClient
can be created in global scope and imported into functions in same way like you did with db_conn()
.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
I would also recommend putting USER
, PASS
, MONGO_URI
, PORT
into configuration file, so they can be easily overridden by environment variables on production.
Also Artyom24 pointed out nice improvements regarding API RESTfulness
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
But it is not necessary, instance of MongoClient
can be created in global scope and imported into functions in same way like you did with db_conn()
.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
I would also recommend putting USER
, PASS
, MONGO_URI
, PORT
into configuration file , so they can be easily overridden by environment variables on production.
Also Artyom24 pointed out nice improvements regarding API RESTfulness
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
But it is not necessary, instance of MongoClient
can be created in global scope and imported into functions in same way like you did with db_conn()
.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}
I would also recommend putting USER
, PASS
, MONGO_URI
, PORT
into configuration file , so they can be easily overridden by environment variables on production.
Also Artyom24 pointed out nice improvements regarding API RESTfulness
Ad. 1 & 2:
There is no need to open fresh connection for, and close it after request.
MongoClient
has connection-pooling built in, so only thing you need to do is to create instance of MongoClient
with connection parameters and optional pool size.
It will open connection on first usage and in case it gets closed or times out - reopen them when needed.
A good way to do, would be to build a plugin wrapping MongoClient
like they did with SQLite in this example.
Such plugin can be used afterwards as persistent connection - all logic regarding connecting/keeping/reconnecting will happen inside, thus simplifying resource methods.
Ad. 3:
IMHO there is nothing wrong with jsonify
since Flask-RESTful recognises Flasks response objects.
But, as they show in here you don't need to do it and you can return dicts directly:
class Todo3(Resource):
def get(self):
# Set the response code to 201 and return custom headers
return {'task': 'Hello world'}, 201, {'Etag': 'some-opaque-string'}