uplink icon indicating copy to clipboard operation
uplink copied to clipboard

Make consumer available to converters (including @loads decorated functions)

Open prkumar opened this issue 6 years ago • 0 comments

Is your feature request related to a problem? Please describe. This issue relates to a question by @liiight on Gitter:

hey @prkumar, is it possible to pass the consumer class when using loads?

it'd be nice if would be possible, in a similar fashion to how its used in response handler

my use case is that i want to pass the consumer class to every model im returning, allowing me to create nifty connection behind the scenes masked by nice api

Describe the solution you'd like From an API perspective, we could add the argument requires_consumer to the constructors of @loads and @dumps:

@loads(ModelBase, requires_consumer=True)
def load_model(consumer, model_cls, data):
    ...

From an implementation perspective, we need to modify RequestDefinition.make_converter_registry to accept the consumer instance when constructing the ConverterFactoryRegistry:

class RequestDefinition(interfaces.RequestDefinition):
    ...
    def make_converter_registry(self, converters_, consumer):
        return converters.ConverterFactoryRegistry(converters_, self, consumer)

Then, we'll need to pass the consumer when constructing the request builder in RequestPreparer.create_request_builder:

class RequestPreparer(object):
    ...
    def create_request_builder(self, definition):
        registry = definition.make_converter_registry(self._converters, self._consumer)
        req = helpers.RequestBuilder(self._client, registry, self._base_url)
        if self._session_chain:
            self._session_chain.audit_request(self._consumer, req)
        return req

The remaining changes should be focused in uplink.models module, where @loads and @dumps is defined.

prkumar avatar Apr 20 '19 01:04 prkumar