Skip to content

Commit

Permalink
Deduplicate GalaxyInstance and ToolShedInstance __init__() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoranzo committed Jun 23, 2015
1 parent 7090e7f commit 69ca1b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
12 changes: 1 addition & 11 deletions bioblend/galaxy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
A base representation of an instance of Galaxy
"""
from six.moves.urllib.parse import urljoin, urlparse

from bioblend.galaxy.client import Client
from bioblend.galaxy import (libraries, histories, workflows, datasets, users,
genomes, tools, toolshed, config, visual, quotas,
Expand Down Expand Up @@ -49,15 +47,7 @@ def __init__(self, url, key=None, email=None, password=None):
e-mail address. Ignored if key is supplied directly.
"""
# Make sure the url scheme is defined (otherwise requests will not work)
if not urlparse(url).scheme:
url = "http://" + url
# All of Galaxy's API's are rooted at <url>/api so make that the base url
self.base_url = url
self.url = urljoin(url, 'api')
self._init_auth(key, email, password)
self.json_headers = {'Content-Type': 'application/json'}
self.verify = True # Should SSL verification be done
super(GalaxyInstance, self).__init__(url, key, email, password)
self.libraries = libraries.LibraryClient(self)
self.histories = histories.HistoryClient(self)
self.workflows = workflows.WorkflowClient(self)
Expand Down
29 changes: 19 additions & 10 deletions bioblend/galaxyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@
import requests
from requests_toolbelt import MultipartEncoder
import six
from six.moves.urllib.parse import urljoin, urlparse

from .galaxy.client import ConnectionError


class GalaxyClient(object):

def __init__(self, url, key=None, email=None, password=None):
# Make sure the url scheme is defined (otherwise requests will not work)
if not urlparse(url).scheme:
url = "http://" + url
# All of Galaxy's and ToolShed's API's are rooted at <url>/api so make that the url
self.base_url = url
self.url = urljoin(url, 'api')
# If key has been supplied, use it; otherwise just set email and
# password and grab user's key before first request.
if key:
self._key = key
else:
self._key = None
self.email = email
self.password = password
self.json_headers = {'Content-Type': 'application/json'}
self.verify = True # Should SSL verification be done

def _make_url(self, module, module_id=None, deleted=False, contents=False):
"""
Compose a URL based on the provided arguments.
Expand Down Expand Up @@ -165,16 +184,6 @@ def key(self):
self._key = response["api_key"]
return self._key

def _init_auth(self, key, email, password):
# If key supplied use it, otherwise just set email and password and
# grab users key before first request.
if key:
self._key = key
else:
self._key = None
self.email = email
self.password = password

@property
def default_params(self):
return {'key': self.key}
12 changes: 1 addition & 11 deletions bioblend/toolshed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
A base representation of an instance of Tool Shed
"""
from six.moves.urllib.parse import urljoin, urlparse

from bioblend.toolshed import (repositories)
from bioblend.galaxyclient import GalaxyClient

Expand Down Expand Up @@ -33,13 +31,5 @@ def __init__(self, url, key='', email=None, password=None):
:param key: If required, user's API key for the given instance of ToolShed,
obtained from the user preferences.
"""
# Make sure the url scheme is defined (otherwise requests will not work)
if not urlparse(url).scheme:
url = "http://" + url
self.base_url = url
# All of ToolShed's API's are rooted at <url>/api so make that the url
self.url = urljoin(url, 'api')
self._init_auth(key, email, password)
self.json_headers = {'Content-Type': 'application/json'}
self.verify = True # Should SSL verification be done
super(ToolShedInstance, self).__init__(url, key, email, password)
self.repositories = repositories.ToolShedClient(self)

0 comments on commit 69ca1b5

Please sign in to comment.