From 914e8f9eaa68db7c49fe85c5db20f8bfb6291599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 18 Mar 2016 10:59:52 +0100 Subject: [PATCH] Moved _ensure_projections(...) up from Find to Resource It's now also used in List --- pillarsdk/resource.py | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/pillarsdk/resource.py b/pillarsdk/resource.py index b0fcf47..5cd3b19 100644 --- a/pillarsdk/resource.py +++ b/pillarsdk/resource.py @@ -10,6 +10,7 @@ class Resource(object): """Base class for all REST services """ convert_resources = {} + ensure_query_projections = {} def __init__(self, attributes=None): attributes = attributes or {} @@ -22,6 +23,28 @@ class Resource(object): super(Resource, self).__setattr__('request_id', None) self.merge(attributes) + @staticmethod + def _ensure_projections(params, extra_projections): + """Ensures that if projections are given in the params, they contain the given ones. + + Only works when `params['projection']` exists and is a dict. + + @param params: URL parameters + @type params: dict + @param extra_projections: extra projections to add + @type extra_projections: dict + """ + + if not extra_projections: + return + + try: + if isinstance(params['projection'], dict): + params['projection'].update(extra_projections) + except (TypeError, KeyError): + # Either params is None or params['projection'] doesn't exist. + pass + def generate_request_id(self): """Generate unique request id """ @@ -106,29 +129,6 @@ class Resource(object): class Find(Resource): - ensure_query_projections = {} - - @classmethod - def _ensure_projections(cls, params, extra_projections): - """Ensures that if projections are given in the params, they contain the given ones. - - Only works when `params['projection']` exists and is a dict. - - @param params: URL parameters - @type params: dict - @param extra_projections: extra projections to add - @type extra_projections: dict - """ - - if not extra_projections: - return - - try: - if isinstance(params['projection'], dict): - params['projection'].update(extra_projections) - except (TypeError, KeyError): - # Either params is None or params['projection'] doesn't exist. - pass @classmethod def find(cls, resource_id, params=None, api=None): @@ -215,6 +215,7 @@ class List(Resource): if params is None: url = cls.path else: + cls._ensure_projections(params, cls.ensure_query_projections) url = utils.join_url_params(cls.path, params) try: