Skip to content

Commit 6e9b436

Browse files
committed
Fixes disqus#38: Allow for nested methods to support conduit calls such as diffusion.repository.edit
1 parent 838398b commit 6e9b436

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

phabricator/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,31 @@ def __repr__(self):
217217

218218

219219
class Resource(object):
220-
def __init__(self, api, interface=None, endpoint=None, method=None):
220+
def __init__(self, api, interface=None, endpoint=None, method=None, nested=False):
221221
self.api = api
222222
self.interface = interface or copy.deepcopy(parse_interfaces(INTERFACES))
223223
self.endpoint = endpoint
224224
self.method = method
225+
self.nested = nested
225226

226227
def __getattr__(self, attr):
227228
if attr in getattr(self, '__dict__'):
228229
return getattr(self, attr)
229230
interface = self.interface
230-
if attr not in interface:
231+
if self.nested:
232+
attr = "%s.%s" % (self.endpoint, attr)
233+
submethod_exists = False
234+
submethod_match = attr + '.'
235+
for key in interface.keys():
236+
if key.startswith(submethod_match):
237+
submethod_exists = True
238+
break
239+
if attr not in interface and submethod_exists:
240+
return Resource(self.api, interface, attr, self.endpoint, nested=True)
241+
elif attr not in interface:
231242
interface[attr] = {}
243+
if self.nested:
244+
return Resource(self.api, interface[attr], attr, self.method)
232245
return Resource(self.api, interface[attr], attr, self.endpoint)
233246

234247
def __call__(self, **kwargs):

phabricator/interfaces.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

phabricator/tests/test_phabricator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ def test_user_whoami(self, mock_connection):
8383

8484
self.assertEqual(api.user.whoami()['userName'], 'testaccount')
8585

86+
def test_classic_resources(self):
87+
api = phabricator.Phabricator(
88+
username='test',
89+
certificate='test',
90+
host='http://localhost'
91+
)
92+
93+
self.assertEqual(api.user.whoami.method, 'user')
94+
self.assertEqual(api.user.whoami.endpoint, 'whoami')
95+
96+
def test_nested_resources(self):
97+
api = phabricator.Phabricator(
98+
username='test',
99+
certificate='test',
100+
host='http://localhost'
101+
)
102+
103+
self.assertEqual(api.diffusion.repository.edit.method, 'diffusion')
104+
self.assertEqual(api.diffusion.repository.edit.endpoint, 'repository.edit')
105+
86106
@mock.patch('phabricator.httplib.HTTPConnection')
87107
def test_bad_status(self, mock_connection):
88108
mock_obj = mock_connection.return_value = mock.Mock()

0 commit comments

Comments
 (0)