def test_will_prettyprint_json_in_debug_mode(self):
        self.app.config['DEBUG'] = True
        api = Api(self.app)

        class Foo1(Resource):
            def get(self):
                return {'foo': 'bar', 'baz': 'asdf'}

        api.add_resource(Foo1, '/foo', endpoint='bar')

        with self.app.test_client() as client:
            foo = client.get('/foo')

            # Python's dictionaries have random order (as of "new" Pythons,
            # anyway), so we can't verify the actual output here.  We just
            # assert that they're properly prettyprinted.
            lines = foo.data.splitlines()
            lines = [line.decode() for line in lines]
            self.assertEquals("{", lines[0])
            self.assertTrue(lines[1].startswith('    '))
            self.assertTrue(lines[2].startswith('    '))
            self.assertEquals("}", lines[3])

            # Assert our trailing newline.
            self.assertTrue(foo.data.endswith(b'\n'))
    def test_json_float_marshalled(self):
        api = Api(self.app)

        class FooResource(Resource):
            fields = {'foo': fields.Float}

            def get(self):
                return marshal({"foo": 3.0}, self.fields)

        api.add_resource(FooResource, '/api')

        app = self.app.test_client()
        resp = app.get('/api')
        self.assertEquals(resp.status_code, 200)
        self.assertEquals(resp.data.decode('utf-8'), '{"foo": 3.0}\n')
#!bin/python

from flask import Flask, Blueprint
from flask.ext.restplus import Api, Resource, fields, apidoc

app = Flask(__name__)
# PREFIX van de api -> http://SERVERNAME/<URL_PREFIX>
blueprint = Blueprint('api', __name__, url_prefix='/apiv1')
# CUSTOM CREATE van Api object ipv api = Api(blueprint, ui=False)
api = Api()
# manuele add van endpoint specs met path naar swagger.json, anders auto op /apiv1/swagger.json
# AANPASSEN PATH->swagger.json naar /<NAMESPACE>/swagger.json
api.add_resource(api.swagger_view(), '/sensoren/swagger.json', endpoint='specs', doc=False)
# !!! Blueprint is een soort app. Aangezien de routes gekoppeld zijn ad blueprint, moet deze geregistreerd worden, dus niet api.init_app(app, add_specs=False, ui=False)
# init Api
api.init_app(blueprint, add_specs=False, ui=False)

# NAMESPACE: AANPASSEN! Komt overeen met http://<SERVERNAME>/URL_PREFIX/<NAMESPACE>
ns = api.namespace('sensoren', description='Temperatuursensoren Koelcel & Diepvriescel')

# PATH NAAR SWAGGER (/<NAMESPACE>/swagger)
@blueprint.route('/sensoren/swagger/', endpoint='swagger')
def swagger_ui():
    return apidoc.ui_for(api)

# EFFECTIEVE API GEDEELTE
# => http://<SERVERNAME>/URL_PREFIX/<NAMESPACE>/
@ns.route('/', endpoint='sensoren')
@api.doc(description='test')
class MyResource(Resource):
    def get(self):
Esempio n. 4
0

@api.doc(responses={200: 'Successfully logged in '})
class ProtectedResource(Resource):
    """
    This endpoint is protected by basic auth and is only used for testing
    """
    decorators = [requires_auth]

    def get(self):
        """Testing: Authentication"""
        return {"message": "Hello"}, 200


# testing endpoints
api.add_resource(HelloWorld, '/hello')
api.add_resource(ProtectedResource, '/protected-resource')

api.add_resource(ImOpen, '/im-open/<int:lock_id>')
api.add_resource(ImClosed, '/im-closed/<int:lock_id>')
api.add_resource(FriendLocks, '/friend-lock')

api.add_resource(UserList, '/user')
api.add_resource(Me, '/me')
api.add_resource(UserDetail, '/user/<int:user_id>')
api.add_resource(LockList, '/lock')
api.add_resource(LockDetail, '/lock/<int:lock_id>')
api.add_resource(OpenLock, '/open/<int:lock_id>')
api.add_resource(CloseLock, '/close/<int:lock_id>')
api.add_resource(Status, '/status/<int:lock_id>')
api.add_resource(FriendList, '/friend')
Esempio n. 5
0
            return results, 200


class ProjectBugSearch(Resource):
    @api.doc(
        description='Will search title description without project specified ',
        responses={200: 'Bug'},
        parser=parser)
    def get(self, project, query):
        args = parser.parse_args()
        if not args['field']:
            results = search_clients[project].search_fields(
                ['title', 'description'], query, index=project)
        else:
            results = search_clients[project].search_field(args['field'],
                                                           query,
                                                           index=project)

        if len(results) == 0:
            return 'Not Found', 404
        else:
            return results, 200


bug_search_module_api.add_resource(GeneralBug, '/<string:bug_id>')
bug_search_module_api.add_resource(ProjectBug,
                                   '/<string:project>/<string:bug_id>')
bug_search_module_api.add_resource(GeneralBugSearch, '/search/<string:query>')
bug_search_module_api.add_resource(ProjectBugSearch,
                                   '/search/<string:project>/<string:query>')