Example #1
0
 def test_schema(self):
     api = Api(
         host="http://example.com/",
         path='/api/v1/',
         version='1'
     )
     api.schema(
         'User',
         properties={
             "name": String(required=True),
             "logoutUrl": String(),
         },
     )
     schema = api.schemas()
     self.assertEqual(
         "http://example.com/api/v1/json-schemas#",
         schema['id']
     )
     self.assertEqual(
         "http://json-schema.org/draft-04/schema#",
         schema['$schema']
     )
     self.assertEqual(
         {
             "id": "User",
             "type": "object",
             'additionalProperties': False,
             "properties": {
                 "name": {"type": "string"},
                 "logoutUrl": {"type": "string"}
             },
             "required": ["name"]
         },
         schema['User']
     )
Example #2
0
    def test_validator(self):
        self.maxDiff = None
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        api.schema(
            'Student',
            properties={
                "name": String(required=True),
                "id": Int(required=True)
            },
        )
        api.schema(
            'StudentList',
            properties={
                'students': Array(api.ref('Student'), required=True),
            }
        )

        self.assertRaises(ValidationError, api.validate, 'Student', {})

        try:
            api.validate(
                'Student',
                {
                    "name": "alice",
                    "id": 1
                }
            )
        except ValidationError:
            self.fail("Validation was suppose to pass. It failed instead")
Example #3
0
    def test_schema_with_ref(self):
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        api.schema(
            'Student',
            properties={
                "name": String(required=True),
                "id": Int(required=True)
            },
        )
        api.schema(
            'StudentList',
            properties={
                "students": Array(items=api.ref('Student'), required=True)
            },
        )

        schema = api.schemas()


        self.assertEqual(
            "http://example.com/api/v1/json-schemas#",
            schema['id']
        )
        self.assertEqual(
            "http://json-schema.org/draft-04/schema#",
            schema['$schema']
        )
        self.assertEqual(
            {
                "id": "Student",
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "id": {"type": "integer"}
                },
                "required": ["name", "id"],
                'additionalProperties': False
            },
            schema['Student']
        )
        self.assertEqual({
                "students": {
                    "type": "array",
                    "items" : {
                        "$ref":
                            "http://example.com/api/v1/json-schemas#/Student"
                    }
                },
            },
            schema['StudentList']['properties']
        )
Example #4
0
    def test_define_routes(self):
        self.maxDiff = None
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        api.schema(
            'Student',
            properties={
                "name": String(required=True),
                "id": Int(required=True)
            },
        )
        api.schema(
            'StudentList',
            properties={
                'students': Array(api.ref('Student'), required=True),
            }
        )
        students = api.resource(
            path="/students", desc="Operations about students"
        )
        path = students.endpoint(r"/students/")
        path.operation(
            type_="StudentList",
            alias="getStudends",
            responses=[
                Message(200, "Ok"),
                Message(401, "Unauthorized"),
                Message(403, "Forbidden"),
            ]
        )(Handler.get)
        path.bind(Handler)

        routes = api.routes()
        self.assertEqual('/api/v1', routes.prefix)
        self.assertEqual(
            [
                ("/api/v1/api-docs", api.api_doc_handler,),
                ("/api/v1/api-docs/<path:.+>", api.apis_handler,),
                ("/api/v1/json-schemas", api.schema_handler,),
                ("/api/v1/students/", Handler),
            ],
            [(r.template, r.handler) for r in routes.routes]
        )
Example #5
0
    def test_define_ressources(self):
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        self.assertEqual('http://example.com', api.host)
        self.assertEqual('/api/v1', api.path)
        self.assertEqual(
            'http://example.com/api/v1',
            api.base_path
        )
        self.assertEqual('1', api.version)
        self.assertEqual({
                "apiVersion": "1",
                "swaggerVersion": "1.2",
                "apis": []
            },
            api.api_doc()
        )

        api.resource(path="/res", desc="Some resource")
        self.assertEqual({
                "apiVersion": "1",
                "swaggerVersion": "1.2",
                "apis": [
                    {
                        "path": "/res",
                        "description": "Some resource"
                    }
                ]
            },
            api.api_doc()
        )
Example #6
0
"""JSON/RESTful API that will support a variety of Angularjs apps to
support the education market in Singapore.

"""

from google.appengine.api import app_identity
from google.appengine.api import lib_config
from webapp2ext.swagger import Api

__all__ = ['api']


class _ConfigDefaults(object):
    HOST = ("http://%s/" % app_identity.get_default_version_hostname()
            or '0.0.0.0:8080')
    PATH = '/api/v1/'
    VERSION = '1-dev'


_config = lib_config.register('education', _ConfigDefaults.__dict__)

# Put those settings in appengine_config.py
api = Api(host=_config.HOST, path=_config.PATH, version=_config.VERSION)

import education.core.schemas
import education.dashboard.schemas

import education.core.controllers
import education.dashboard.controllers
Example #7
0
    def test_define_operation_models(self):
        self.maxDiff = None
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        api.schema(
            'Student',
            properties={
                "name": String(required=True),
                "id": Int(required=True)
            },
        )
        api.schema(
            'StudentList',
            properties={
                'students': Array(api.ref('Student'), required=True),
            }
        )
        students = api.resource(
            path="/students", desc="Operations about students"
        )
        path = students.endpoint(r"/students/")
        path.operation(
            type_="StudentList",
            alias="getStudends",
            responses=[
                Message(200, "Ok"),
                Message(401, "Unauthorized"),
                Message(403, "Forbidden"),
            ]
        )(Handler.get)

        self.assertEqual(
            {
                u"apiVersion": u"1",
                u"swaggerVersion": u"1.2",
                u"basePath": u"http://example.com/api/v1",
                u"resourcePath": u"/students",
                u"apis": [
                    {
                        u"path": u"/students/",
                        u"operations": [
                            {
                                u"method": u"GET",
                                u"summary": u"List resource",
                                u"type": u"StudentList",
                                u"nickname": u"getStudends",
                                u"parameters": [],
                                u"responseMessages": [
                                    {
                                        u"code": 200,
                                        u"message": u"Ok"
                                    },
                                    {
                                        u"code": 401,
                                        u"message": u"Unauthorized"
                                    },
                                    {
                                        u"code": 403,
                                        u"message": u"Forbidden"
                                    },
                                ]

                            }
                        ],
                    },
                ],
                u"models": {
                    u"Student": {
                        u"id": u"Student",
                        u"type": u"object",
                        u'additionalProperties': False,
                        u"properties": {
                            u"id": {
                                u"type": u"integer"
                            },
                            u"name": {
                                u"type": u"string"
                            }
                        },
                        u"required": [u"name", u"id"]
                    },
                    u"StudentList": {
                        u"id": u"StudentList",
                        u"type": u"object",
                        u'additionalProperties': False,
                        u"properties": {
                            u"students": {
                                u"type": "array",
                                u"items": {
                                    u"$ref": u"Student"
                                }
                            },
                        },
                        u"required": [u"students"]
                    }

                }
            },
            students.api_doc()
        )
Example #8
0
    def test_define_operation(self):
        self.maxDiff = None
        api = Api(
            host="http://example.com/",
            path='/api/v1/',
            version='1'
        )
        api.schema(
            'Student',
            properties={
                "name": String(required=True),
                "id": Int(required=True)
            },
        )
        api.schema(
            'StudentList',
            properties={
                'students': Array(api.ref('Student'), required=True),
            }
        )
        students = api.resource(path="/students", desc="Operations about students")
        path = students.endpoint(r"/students/")
        path.operation(
            type_="StudentList",
            alias="getStudends",
            responses=[
                Message(200, "Ok"),
                Message(401, "Unauthorized"),
                Message(403, "Forbidden"),
            ]
        )(Handler.get)
        path.operation(
            type_="Student",
            alias="addStudend",
            parameters=[
                Param(
                    name="body",
                    description="Student to add",
                    type_="Student",
                    param_type="body",
                    required=True
                )
            ],
            responses=[
                Message(200, "Ok"),
                Message(400, "Invalid student"),
                Message(401, "Unauthorized"),
                Message(403, "Forbidden"),
            ]
        )(Handler.post)


        path = students.endpoint(r"/students/<studenId:\d+>")
        path.operation(
            type_="Student",
            alias="getStudendById",
            parameters=[
                Int(
                    name="studenId",
                    description="student id",
                    required=True,
                    param_type="path",
                    minimum=1
                )
            ],
            responses=[
                Message(200, "Ok"),
                Message(401, "Unauthorized"),
                Message(403, "Forbidden"),
                Message(404, "Not Found"),
            ]
        )(Handler.get) # should be a different method, but it

        self.assertIn(r"/students/<studenId:\d+>", students.apis)
        self.assertEqual(
            {
                u"apiVersion": u"1",
                u"swaggerVersion": u"1.2",
                u"basePath": u"http://example.com/api/v1",
                u"resourcePath": u"/students",
                u"apis": [
                    {
                        u"path": u"/students/",
                        u"operations": [
                            {
                                u"method": u"GET",
                                u"summary": u"List resource",
                                u"type": u"StudentList",
                                u"nickname": u"getStudends",
                                u"parameters": [],
                                u"responseMessages": [
                                    {
                                        u"code": 200,
                                        u"message": u"Ok"
                                    },
                                    {
                                        u"code": 401,
                                        u"message": u"Unauthorized"
                                    },
                                    {
                                        u"code": 403,
                                        u"message": u"Forbidden"
                                    },
                                ]

                            },
                            {
                                u"method": u"POST",
                                u"summary": u"Add resource",
                                u"type": u"Student",
                                u"nickname": u"addStudend",
                                u"parameters": [
                                    {
                                        "name": "body",
                                        "description": "Student to add",
                                        "required": True,
                                        "type": "Student",
                                        "paramType": "body"
                                    }
                                ],
                                u"responseMessages": [
                                    {
                                        u"code": 200,
                                        u"message": u"Ok"
                                    },
                                    {
                                        u"code": 400,
                                        u"message": u"Invalid student"
                                    },
                                    {
                                        u"code": 401,
                                        u"message": u"Unauthorized"
                                    },
                                    {
                                        u"code": 403,
                                        u"message": u"Forbidden"
                                    },
                                ]

                            }
                        ],
                    },
                    {
                        u"path": u"/students/{studenId}",
                        u"operations": [
                            {
                                u"method": u"GET",
                                u"summary": u"List resource",
                                u"type": u"Student",
                                u"nickname": u"getStudendById",
                                u"parameters": [
                                    {
                                        u"name": u"studenId",
                                        u"description": u"student id",
                                        u"required": True,
                                        u"type": u"integer",
                                        u"paramType": u"path",
                                        u"minimum": 1
                                    }
                                ],
                                u"responseMessages": [
                                    {
                                        u"code": 200,
                                        u"message": u"Ok"
                                    },
                                    {
                                        u"code": 401,
                                        u"message": u"Unauthorized"
                                    },
                                    {
                                        u"code": 403,
                                        u"message": u"Forbidden"
                                    },
                                    {
                                        u"code": 404,
                                        u"message": u"Not Found"
                                    },
                                ]
                            }
                        ]
                    },
                ],
                u"models": {
                    u"Student": {
                        u"id": u"Student",
                        u"type": u"object",
                        u'additionalProperties': False,
                        u"properties": {
                            u"id": {
                                u"type": u"integer"
                            },
                            u"name": {
                                u"type": u"string"
                            }
                        },
                        u"required": [u"name", u"id"]
                    },
                    u"StudentList": {
                        u"id": u"StudentList",
                        u"type": u"object",
                        u'additionalProperties': False,
                        u"properties": {
                            u"students": {
                                u"type": "array",
                                u"items": {
                                    u"$ref": u"Student"
                                }
                            },
                        },
                        u"required": [u"students"]
                    }

                }
            },
            students.api_doc()
        )