示例#1
0
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)

        self.ns = Namespace(subject="file")
        self.relation_ns = Namespace(subject=Person, object_="file")

        self.controller = FileController()

        UPLOAD_MAPPINGS = {
            Operation.Upload: EndpointDefinition(
                func=self.controller.upload,
                request_schema=FileExtraSchema(),
            ),
        }

        UPLOAD_FOR_MAPPINGS = {
            Operation.UploadFor: EndpointDefinition(
                func=self.controller.upload_for_person,
                request_schema=FileExtraSchema(),
                response_schema=FileResponseSchema(),
            ),
        }

        configure_upload(self.graph, self.ns, UPLOAD_MAPPINGS)
        configure_upload(self.graph, self.relation_ns, UPLOAD_FOR_MAPPINGS)
        configure_swagger(self.graph)

        self.client = self.graph.flask.test_client()
示例#2
0
def configure_follower_relationship_routes(graph):
    controller = graph.follower_relationship_controller
    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewFollowerRelationshipSchema(),
            response_schema=FollowerRelationshipSchema(),
        ),
        Operation.Delete:
        EndpointDefinition(func=transactional(controller.delete), ),
        Operation.Retrieve:
        EndpointDefinition(
            func=controller.retrieve,
            response_schema=FollowerRelationshipSchema(),
        ),
        Operation.Search:
        EndpointDefinition(
            func=controller.search,
            request_schema=SearchFollowerRelationshipSchema(),
            response_schema=FollowerRelationshipSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#3
0
def configure_user_routes(graph):
    controller = graph.user_controller
    mappings = {
        Operation.Create: EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewUserSchema(),
            response_schema=UserSchema(),
        ),
        Operation.Delete: EndpointDefinition(
            func=transactional(controller.delete),
        ),
        Operation.Retrieve: EndpointDefinition(
            func=controller.retrieve,
            response_schema=UserSchema(),
        ),
        Operation.Search: EndpointDefinition(
            func=controller.search,
            request_schema=SearchUserSchema(),
            response_schema=UserSchema(),
        ),
        Operation.Update: EndpointDefinition(
            func=transactional(controller.update),
            request_schema=UpdateUserSchema(),
            response_schema=UserSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#4
0
def configure_account_routes(graph):
    controller = graph.account_controller
    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewAccountSchema(),
            response_schema=AccountSchema(),
        ),
        Operation.Delete:
        EndpointDefinition(func=transactional(controller.delete), ),
        Operation.Retrieve:
        EndpointDefinition(
            func=auth_group("app-users-group")(controller.retrieve),
            response_schema=AccountSchema(),
        ),
        Operation.Search:
        EndpointDefinition(
            func=auth_group("app-admins-group")(controller.search),
            request_schema=SearchAccountSchema(),
            response_schema=AccountSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#5
0
def configure_topping_routes(graph):
    controller = graph.topping_controller
    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewToppingSchema(),
            response_schema=ToppingSchema(),
        ),
        Operation.Delete:
        EndpointDefinition(func=transactional(controller.delete), ),
        Operation.Replace:
        EndpointDefinition(
            func=transactional(controller.replace),
            request_schema=NewToppingSchema(),
            response_schema=ToppingSchema(),
        ),
        Operation.Retrieve:
        EndpointDefinition(
            func=controller.retrieve,
            response_schema=ToppingSchema(),
        ),
        Operation.Search:
        EndpointDefinition(
            func=controller.search,
            request_schema=SearchToppingSchema(),
            response_schema=ToppingSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#6
0
def configure_order_routes(graph):
    controller = graph.order_controller
    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewOrderSchema(),
            response_schema=OrderSchema(),
        ),
        Operation.Delete:
        EndpointDefinition(func=transactional(controller.delete), ),
        Operation.Replace:
        EndpointDefinition(
            func=transactional(controller.replace),
            request_schema=NewOrderSchema(),
            response_schema=OrderSchema(),
        ),
        Operation.Retrieve:
        EndpointDefinition(
            func=controller.retrieve,
            response_schema=OrderSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
def configure_event_crud(
    graph,
    controller,
    event_schema,
    new_event_schema,
    search_event_schema,
    use_deferred_batch=False,
):
    if use_deferred_batch:
        create_func = transactional(
            deferred_batch(controller)(controller.create))
    else:
        create_func = transactional(controller.create)

    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=create_func,
            request_schema=new_event_schema,
            response_schema=event_schema,
        ),
        Operation.Delete:
        EndpointDefinition(func=transactional(controller.delete), ),
        Operation.Replace:
        EndpointDefinition(
            func=transactional(controller.replace),
            request_schema=new_event_schema,
            response_schema=event_schema,
        ),
        Operation.Retrieve:
        EndpointDefinition(
            func=controller.retrieve,
            response_schema=event_schema,
        ),
        Operation.Search:
        EndpointDefinition(
            func=controller.search,
            request_schema=search_event_schema,
            response_schema=event_schema,
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
    def test_register_both_create_and_create_collection(self):
        mappings = {
            Operation.Create:
            EndpointDefinition(
                func=lambda x: x,
                request_schema=Schema(),
                response_schema=Schema(),
            ),
            Operation.CreateCollection:
            EndpointDefinition(
                func=lambda x: x,
                request_schema=Schema(),
                response_schema=Schema(),
            ),
        }

        assert_that(
            calling(configure_crud).with_args(self.graph, self.ns, mappings),
            raises(RouteAlreadyRegisteredException),
        )
示例#9
0
def configure_invocations_route(graph):
    controller = graph.invocations_controller

    return configure_invocations(
        graph,
        EndpointDefinition(
            func=controller.search,
            request_schema=NewPredictionSchema(),
            response_schema=ClassificationResultSchema(),
        ),
    )
示例#10
0
def add_crud(graph):
    controller = graph.encryptable_controller

    mappings = {
        Operation.Update: EndpointDefinition(
            func=controller.update_and_reencrypt,
            request_schema=UpdateEncryptableSchema(),
            response_schema=EncryptableSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#11
0
def configure_logging_level(graph):
    ns = Namespace(
        subject="logging_level",
    )

    convention = LoggingLevelConvention(graph)
    convention.configure(
        ns,
        retrieve=EndpointDefinition(
            response_schema=LoggerSchema(),
        ),
    )
    return ns
示例#12
0
def configure_order_event_routes(graph):
    controller = graph.order_event_controller
    mappings = {
        Operation.Create:
        EndpointDefinition(
            func=transactional(controller.create),
            request_schema=NewOrderEventSchema(),
            response_schema=OrderEventSchema(),
        ),
        Operation.Retrieve:
        EndpointDefinition(
            func=controller.retrieve,
            response_schema=OrderEventSchema(),
        ),
        Operation.Search:
        EndpointDefinition(
            func=controller.search,
            request_schema=SearchOrderEventSchema(),
            response_schema=OrderEventSchema(),
        ),
    }
    configure_crud(graph, controller.ns, mappings)
    return controller.ns
示例#13
0
def configure_search_followers_by_user_routes(graph):
    """
    Endpoint for list of users following the given user aka followers.

    """
    controller = graph.user_controller

    mappings = {
        Operation.SearchFor: EndpointDefinition(
            func=controller.search_followers_by_user,
            request_schema=PageSchema(),
            response_schema=UserSchema(),
        ),
    }

    configure_relation(graph, controller.followers_ns, mappings)
示例#14
0
def configure_search_feed_by_user_routes(graph):
    """
    Endpoint for list of tweets posted by users the given user is following.

    """
    controller = graph.user_controller

    mappings = {
        Operation.SearchFor: EndpointDefinition(
            func=controller.search_feed_by_user,
            request_schema=PageSchema(),
            response_schema=TweetSchema(),
        ),
    }

    configure_relation(graph, controller.feed_ns, mappings)
示例#15
0
def configure_publish_info(graph):
    ns = Namespace(
        subject="introspection/publish_info",
    )

    def search(**kwargs):
        publish_info = list(graph.sns_producer.get_publish_info())
        return publish_info, len(publish_info)

    mappings = {
        Operation.Search: EndpointDefinition(
            func=search,
            request_schema=Schema(),
            response_schema=PublishInfoSchema(),
        ),
    }

    configure_crud(graph, ns, mappings)
    return ns
def configure_status_convention(graph):
    store = IndexStatusStore(graph)

    ns = Namespace(subject="index_status", )

    def search(**kwargs):
        status = store.get_status()
        return status, len(status)

    mappings = {
        Operation.Search:
        EndpointDefinition(
            func=search,
            request_schema=Schema(),
            response_schema=IndexStatusSchema(),
        ),
    }

    configure_crud(graph, ns, mappings)
    return ns
示例#17
0

PERSON = Person(id=1, first_name="First", last_name="Last")


def find_person_by_name(person_name):
    return PERSON


def find_person(person_id):
    return PERSON


PERSON_MAPPINGS = {
    Operation.Alias: EndpointDefinition(
        func=find_person_by_name,
    ),
    Operation.Retrieve: EndpointDefinition(
        func=find_person,
        response_schema=PersonSchema(),
    ),
}


class TestAlias:

    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)

        self.ns = Namespace(
            subject=Person,
示例#18
0
from microcosm_flask.tests.conventions.fixtures import (
    Person, )

PERSON = Person(id=1, first_name="First", last_name="Last")


def find_person_by_name(person_name):
    return PERSON


def find_person(person_id):
    return PERSON


PERSON_MAPPINGS = {
    Operation.Alias: EndpointDefinition(func=find_person_by_name, ),
    Operation.Retrieve: EndpointDefinition(func=find_person, ),
}


class TestAlias(object):
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)

        self.ns = Namespace(subject=Person)
        configure_crud(self.graph, self.ns, PERSON_MAPPINGS)
        configure_alias(self.graph, self.ns, PERSON_MAPPINGS)

        self.client = self.graph.flask.test_client()

    def test_url_for(self):
示例#19
0
        return self.value


class SearchAddressPageSchema(OffsetLimitPageSchema):
    list_param = QueryStringList(String())
    enum_param = EnumField(TestEnum)


def add_request_id(headers):
    headers["X-Request-Id"] = "request-id"


PERSON_MAPPINGS = {
    Operation.Search: EndpointDefinition(
        func=person_search,
        request_schema=OffsetLimitPageSchema(),
        response_schema=PersonCSVSchema(),
        response_formats=[ResponseFormats.JSON, ResponseFormats.CSV],
    ),
}


ADDRESS_MAPPINGS = {
    Operation.Search: EndpointDefinition(
        func=address_search,
        request_schema=SearchAddressPageSchema(),
        response_schema=AddressCSVSchema(),
        response_formats=[ResponseFormats.JSON, ResponseFormats.CSV],
    ),
}

示例#20
0
def upload_file_for(files, person_id):
    return dict(id=person_id, )


class FileExtraSchema(Schema):
    extra = fields.String(missing="something")


class FileResponseSchema(Schema):
    id = fields.UUID(required=True)


UPLOAD_MAPPINGS = {
    Operation.Upload:
    EndpointDefinition(
        func=upload_file,
        request_schema=FileExtraSchema(),
    ),
}

UPLOAD_FOR_MAPPINGS = {
    Operation.UploadFor:
    EndpointDefinition(
        func=upload_file_for,
        response_schema=FileResponseSchema(),
    ),
}


class TestUpload(object):
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)
示例#21
0

PERSON = Person(id=1, first_name="First", last_name="Last")


def find_person_by_name(person_name):
    return PERSON


def find_person(person_id):
    return PERSON


PERSON_MAPPINGS = {
    Operation.Alias:
    EndpointDefinition(func=find_person_by_name, ),
    Operation.Retrieve:
    EndpointDefinition(
        func=find_person,
        response_schema=PersonSchema(),
    ),
}


class TestAlias:
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)

        self.ns = Namespace(subject=Person, )
        configure_crud(self.graph, self.ns, PERSON_MAPPINGS)
        configure_alias(self.graph, self.ns, PERSON_MAPPINGS)
PEOPLE = [
    PERSON_1,
    PERSON_2,
]


def retrieve_person(person_id):
    return next(person for person in PEOPLE
                if ContentBasedAddressConverter.make_hash(person) == person_id)


PERSON_MAPPINGS = {
    Operation.Retrieve:
    EndpointDefinition(
        func=retrieve_person,
        response_schema=PersonSchema(),
    ),
}


class TestIdentifierType(object):
    def setup(self):
        loader = load_from_dict(route=dict(converters=["cba"], ), )
        self.graph = create_object_graph(name="example",
                                         testing=True,
                                         loader=loader)
        assert_that(self.graph.config.route.converters,
                    contains_inanyorder("uuid", "cba"))
        self.person_ns = Namespace(
            subject=Person,
            # use custom identifier type
示例#23
0
    return dict(text=text), 1


class FooRequestSchema(Schema):
    text = fields.String(required=True)


class FooResponseSchema(Schema):
    text = fields.String(request=True)


FOO_MAPPINGS = {
    Operation.CreateCollection:
    EndpointDefinition(
        func=create_collection,
        request_schema=FooRequestSchema(),
        response_schema=FooResponseSchema(),
    ),
}


class TestCreateCollection:
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True)

        self.ns = Namespace(subject="foo")

        configure_crud(self.graph, self.ns, FOO_MAPPINGS)
        configure_swagger(self.graph)

        self.client = self.graph.flask.test_client()