From 13b51f8bad36809491c62cbd59fbbbf6baf5956d Mon Sep 17 00:00:00 2001 From: Alek Storm Date: Mon, 31 Oct 2011 14:33:16 -0400 Subject: [PATCH] Add support for selecting specific fields of Etsy resources, and adding associations --- etsy/__init__.py | 2 +- etsy/_core.py | 10 ++++++++-- etsy/_v2.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/etsy/__init__.py b/etsy/__init__.py index 32e5cf1..dffae3f 100644 --- a/etsy/__init__.py +++ b/etsy/__init__.py @@ -1,4 +1,4 @@ -from _v2 import EtsyV2 as Etsy +from _v2 import Association, EtsyV2 as Etsy from etsy_env import EtsyEnvSandbox, EtsyEnvProduction diff --git a/etsy/_core.py b/etsy/_core.py index b28b588..dadf7a0 100644 --- a/etsy/_core.py +++ b/etsy/_core.py @@ -141,10 +141,16 @@ def invoke(self, *args, **kwargs): ps[p] = kwargs[p] del kwargs[p] - self.type_checker(self.spec, **kwargs) - return self.api._get(self.spec['http_method'], self.uri_format % ps, **kwargs) + fields = kwargs.pop('fields', None) + includes = kwargs.pop('includes', None) + self.type_checker(self.spec, **kwargs) + if fields: + kwargs['fields'] = ','.join(fields) + if includes: + kwargs['includes'] = ','.join([str(include) for include in includes]) + return self.api._get(self.spec['http_method'], self.uri_format % ps, **kwargs) class MethodTableCache(object): diff --git a/etsy/_v2.py b/etsy/_v2.py index 6e1d1e8..93d0f27 100644 --- a/etsy/_v2.py +++ b/etsy/_v2.py @@ -24,3 +24,31 @@ def _get_url(self, url, http_method, content_type, body): if self.etsy_oauth_client is not None: return self.etsy_oauth_client.do_oauth_request(url, http_method, content_type, body) return API._get_url(self, url, http_method, content_type, body) + + +class Association(object): + class Bounds(object): + def __init__(self, limit, offset=None): + self.limit = limit + self.offset = offset + + def __init__(self, name, fields=None, scope=None, bounds=None, child=None): + self.name = name + self.fields = fields + self.scope = scope + self.bounds = bounds + self.child = child + + def __str__(self): + elems = [self.name] + if self.fields is not None: + elems.extend(['(', ','.join(self.fields), ')']) + if self.scope is not None: + elems.extend([':', self.scope]) + if self.bounds is not None: + elems.extend([':', str(self.bounds.limit)]) + if self.bounds.offset is not None: + elems.extend([':', str(self.bounds.offset)]) + if self.child is not None: + elems.extend(['/', str(self.child)]) + return ''.join(elems)