Add query params option and tidy up parameter organization

This commit is contained in:
Paul Makepeace 2011-06-10 14:38:16 +00:00
parent d3163dd693
commit 9ab8ab3695
1 changed files with 17 additions and 6 deletions

View File

@ -53,17 +53,27 @@ class RefineServer(object):
server=self.url() server=self.url()
self.server = server[:-1] if server.endswith('/') else server self.server = server[:-1] if server.endswith('/') else server
def urlopen(self, command, data=None, project_id=None): def urlopen(self, command, data=None, params=None, project_id=None):
"""Open a Refine URL and optionally POST data.""" """Open a Refine URL and with optional query params and POST data.
data: POST data dict
param: query params dict
project_id: project ID as string
Returns urllib2.urlopen iterable."""
url = self.server + '/command/core/' + command url = self.server + '/command/core/' + command
if data is None: if data is None:
data = {} data = {}
if params is None:
params = {}
if project_id: if project_id:
# XXX haven't figured out pattern on qs v body # XXX haven't figured out pattern on qs v body
if 'delete' in command: if 'delete' in command or data:
data['project'] = project_id data['project'] = project_id
else: else:
url += '?project=' + project_id params['project'] = project_id
if params:
url += '?' + urllib.urlencode(params)
req = urllib2.Request(url) req = urllib2.Request(url)
if data: if data:
req.add_data(data) # data = urllib.urlencode(data) req.add_data(data) # data = urllib.urlencode(data)
@ -282,7 +292,8 @@ class RefineProject:
def do_raw(self, command, data): def do_raw(self, command, data):
"""Issue a command to the server & return a response object.""" """Issue a command to the server & return a response object."""
return self.server.urlopen(command, data, self.project_id) return self.server.urlopen(command, project_id=self.project_id,
data=data)
def do_json(self, command, data=None, include_engine=True): def do_json(self, command, data=None, include_engine=True):
"""Issue a command to the server, parse & return decoded JSON.""" """Issue a command to the server, parse & return decoded JSON."""
@ -346,7 +357,7 @@ class RefineProject:
"""Return a fileobject of a project's data.""" """Return a fileobject of a project's data."""
url = ('export-rows/' + urllib.quote(self.project_name()) + '.' + url = ('export-rows/' + urllib.quote(self.project_name()) + '.' +
export_format) export_format)
return self.do_raw(url, {'format': export_format}) return self.do_raw(url, data={'format': export_format})
def export_rows(self, **kwargs): def export_rows(self, **kwargs):
"""Return an iterable of parsed rows of a project's data.""" """Return an iterable of parsed rows of a project's data."""