uplink icon indicating copy to clipboard operation
uplink copied to clipboard

Support dynamic query and header values based on consumer instance properties

Open prkumar opened this issue 5 years ago • 1 comments

Is your feature request related to a problem? Please describe. @tehmufifnman presented the following use case gitter:

Not all methods in the class are static-us, some are dynamic-us

    @returns.json
    @params({"namespace": "static-us"})
    @get("/data/wow/achievement/index")
    def get_achievements_index(self):
        """Returns an index of achievements."""
    @returns.json
    @params({"namespace": "dynamic-us"})
    @get("/data/wow/connected-realm/{connectedRealmId}/auctions")
    def get_auctions(self, connectedRealmId: int):
        """Returns all active auctions for a connected realm."""

so I know which value each method needs based on the options sent into my class - they're always static or dynamic - just the locale prefix changes

There isn't a straightforward way to address this use case as of uplink v0.9.1.

Describe the solution you'd like @tehmufifnman is using a variant of the workaround described in this gist

We can consider whether exposing request_handler is solution. On the other hand, we can provide a more limited solution that only addresses dynamic query and header values based on consumer instance properties.

Additional context N/A

prkumar avatar Apr 19 '20 21:04 prkumar

For Query and Header, we could support this use case by subclassing:

class DynamicLocaleParam(Query):
    def modify_value(self, value, consumer):
        return ‘dynamic-%s’ % (value or consumer.locale.name)

For param and header, we could also support subclassing. Alternatively, we could support the following decorator-based interface:

class ApiClient(Consumer):
    @param
    def dynamic_locale(self):
        return {‘namespace’: ‘dynamic-%s’ % self.locale.name}

    @dynamic_locale
    @get(‘users/{user_id}’)
    def get_users(self, user_id):
        pass

prkumar avatar Apr 20 '20 01:04 prkumar