def test_inject_client():
    class SubSubConf(Config):
        field = Field(int)

    class SubConf(Config):
        foo = Field(SubSubConf)
        bar = ListField(SubSubConf)
        baz = Field(int)

    class Conf(Config):
        one = Field(SubConf)
        two = ListField(SubConf)
        three = Field(int)

    client = object()
    subsubconf = dict(field=1)
    subconf = dict(foo=subsubconf, bar=[subsubconf, subsubconf], baz=1)
    conf = util.inject_client(
        client,
        Conf(one=subconf,
             two=[subconf, subconf],
             three=1))

    assert conf._client is client

    assert conf.one._client is client
    assert conf.one.foo._client is client
    assert all(item._client is client for item in conf.one.bar)

    assert all(item._client is client for item in conf.two)
    assert all(item.foo._client is client for item in conf.two)
    assert all(all(subitem._client is client for subitem in item.bar)
               for item in conf.two)
def test_inject_client():
    class SubSubConf(Config):
        field = Field(int)

    class SubConf(Config):
        foo = Field(SubSubConf)
        bar = ListField(SubSubConf)
        baz = Field(int)

    class Conf(Config):
        one = Field(SubConf)
        two = ListField(SubConf)
        three = Field(int)

    client = object()
    subsubconf = dict(field=1)
    subconf = dict(foo=subsubconf, bar=[subsubconf, subsubconf], baz=1)
    conf = util.inject_client(
        client, Conf(one=subconf, two=[subconf, subconf], three=1))

    assert conf._client is client

    assert conf.one._client is client
    assert conf.one.foo._client is client
    assert all(item._client is client for item in conf.one.bar)

    assert all(item._client is client for item in conf.two)
    assert all(item.foo._client is client for item in conf.two)
    assert all(
        all(subitem._client is client for subitem in item.bar)
        for item in conf.two)
    def _parse_response(self, data, response_class, wrapper=None):
        """
        Parse json data using the response class, returning
        response_class(data).  If wrapper is not None, return the attribute in
        wrapper instead of the object itself.

        After parsing the data, the client object is injected into any Config
        objects. This allows Config objects to potentially make further API
        queries.
        """
        if wrapper is not None and not hasattr(response_class, wrapper):
            raise AttributeError('{0} does not have attribute {1}'.format(
                response_class.__name__, wrapper))

        try:
            response = inject_client(self._client, response_class(data))
            return response if wrapper is None else response.get(wrapper)
        except (figgis.PropertyError, figgis.ValidationError) as exc:
            msg = 'Invalid response: {0}'.format(exc)
            LOG.critical(msg, exc_info=exc)
            raise error.ApiError(msg)
Example #4
0
    def _parse_response(self, data, response_class, wrapper=None):
        """
        Parse json data using the response class, returning
        response_class(data).  If wrapper is not None, return the attribute in
        wrapper instead of the object itself.

        After parsing the data, the client object is injected into any Config
        objects. This allows Config objects to potentially make further API
        queries.
        """
        if wrapper is not None and not hasattr(response_class, wrapper):
            raise AttributeError('{0} does not have attribute {1}'.format(
                response_class.__name__, wrapper))

        try:
            response = inject_client(self._client, response_class(data))
            return response if wrapper is None else response.get(wrapper)
        except (figgis.PropertyError, figgis.ValidationError) as exc:
            msg = 'Invalid response: {0}'.format(exc)
            LOG.critical(msg, exc_info=exc)
            raise error.ApiError(msg)