Ejemplo n.º 1
0
 def __init__(self, registry_url="http://www.intermine.org/registry"):
     self.registry_url = registry_url
     opener = InterMineURLOpener()
     data = opener.open(registry_url + Registry.MINES_PATH).read()
     mine_data = json.loads(data)
     self.__mine_dict = dict(
         ((mine["name"], mine) for mine in mine_data["mines"]))
     self.__synonyms = dict(
         ((name.lower(), name) for name in self.__mine_dict.keys()))
     self.__mine_cache = {}
Ejemplo n.º 2
0
    def __init__(self,
                 root,
                 username=None,
                 password=None,
                 token=None,
                 prefetch_depth=1,
                 prefetch_id_only=False):
        """
        Constructor
        ===========

        Construct a connection to a webservice::

            url = "http://www.flymine.org/query/service"

            # An unauthenticated connection - access to all public data
            service = Service(url)

            # An authenticated connection - access to private and public data
            service = Service(url, token="ABC123456")


        @param root: the root url of the webservice (required)
        @param username: your login name (optional)
        @param password: your password (required if a username is given)
        @param token: your API access token(optional - used in preference to username and password)

        @raise ServiceError: if the version cannot be fetched and parsed
        @raise ValueError:   if a username is supplied, but no password

        There are two alternative authentication systems supported by InterMine
        webservices. The first is username and password authentication, which
        is supported by all webservices. Newer webservices (version 6+)
        also support API access token authentication, which is the recommended
        system to use. Token access is more secure as you will never have
        to transmit your username or password, and the token can be easily changed
        or disabled without changing your webapp login details.

        """
        o = urlparse(root)
        if not o.scheme: root = "http://" + root
        if not root.endswith("/service"): root = root + "/service"

        self.root = root
        self.prefetch_depth = prefetch_depth
        self.prefetch_id_only = prefetch_id_only
        # Initialize empty cached data.
        self._templates = None
        self._model = None
        self._version = None
        self._release = None
        self._widgets = None
        self._list_manager = ListManager(self)
        self.__missing_method_name = None
        if token:
            self.opener = InterMineURLOpener(token=token)
        elif username:
            if token:
                raise ValueError(
                    "Both username and token credentials supplied")

            if not password:
                raise ValueError("Username given, but no password supplied")

            self.opener = InterMineURLOpener((username, password))
        else:
            self.opener = InterMineURLOpener()

        try:
            self.version
        except WebserviceError as e:
            raise ServiceError(
                "Could not validate service - is the root url (%s) correct? %s"
                % (root, e))

        if token and self.version < 6:
            raise ServiceError(
                "This service does not support API access token authentication"
            )

        # Set up sugary aliases
        self.query = self.new_query