Example #1
0
    def addTagTo(self, entity, tag):
        """
        Attach an existing tag to a Labstep entity.
        (See 'tag' for simplified tagging).

        Parameters
        ----------
        entity (obj)
            The Labstep entity to tag. Can be Resource,
            Experiment, or Protocol. Must have 'id'.
        tag (obj)
            The tag to attach. Must have an 'id' property.

        Returns
        -------
        entity
            An object representing the tagged entity.
        """
        entityName = entity.__entityName__

        headers = getHeaders(entity.__user__)
        url = url_join(
            configService.getHost(),
            "api/generic/",
            entityName,
            str(entity.id),
            tag.__entityName__,
            str(tag.id),
        )
        response = requestService.put(url, headers=headers)
        return json.loads(response.content)
Example #2
0
 def transferOwnership(self, entity, workspace_id):
     entityName = entity.__entityName__
     headers = getHeaders(entity.__user__)
     url = url_join(configService.getHost(), "api/generic/", entityName,
                    str(entity.id), "transfer-ownership")
     params = {"group_id": workspace_id}
     requestService.post(url, headers=headers, json=params)
Example #3
0
    def sendEmails(self, emails, message=None):
        """
        Send sharelinks to collaborators via email.

        Parameters
        ----------
        emails (list)
            A list of the emails to send the invite to.
        message (str)
            A message to send with the invite.


        Example
        -------
        ::

            sharelink.sendEmails(emails=['*****@*****.**','*****@*****.**'],
                message='Hi, please collaborate with me on Labstep!')
        """
        headers = getHeaders(self.__user__)

        url = url_join(configService.getHost(), "api/generic/share-link/email")
        fields = {"emails": emails, "message": message, "id": self.id}
        r = requests.post(url, json=fields, headers=headers)
        handleError(r)
Example #4
0
 def downloadFile(self, user, fileId):
     headers = getHeaders(user=user)
     url = url_join(configService.getHost(), "/api/generic/file/download",
                    str(fileId))
     response = requestService.post(url, headers=headers)
     rawData = requestService.get(json.loads(
         response.content)["signed_url"],
                                  headers=None).content
     return rawData
Example #5
0
 def impersonate(self, username, apikey):
     user = self.getUser(username, apikey)
     url = url_join(configService.getHost(),
                    "api/generic/token/impersonate")
     response = requestService.post(url,
                                    json={'guid': user.guid},
                                    headers={"apikey": apikey})
     token = json.loads(response.content)['uuid']
     user.api_key = token
     return user
Example #6
0
def ping():
    """
    Ping the API.

    """
    headers = getHeaders()

    url = url_join(configService.getHost(), 'ping')
    response = requestService.get(url, headers=headers)

    return json.loads(response.content)
Example #7
0
 def newEntities(self, user, entityClass, items):
     headers = getHeaders(user=user)
     url = url_join(configService.getHost(), "/api/generic/",
                    entityClass.__entityName__, "batch")
     response = requestService.post(url,
                                    headers=headers,
                                    json={
                                        "items": items,
                                        "group_id": user.activeWorkspace
                                    })
     entities = json.loads(response.content)
     return list(map(lambda entity: entityClass(entity, user), entities))
Example #8
0
 def linkEntities(self, user, entity1, entity2):
     headers = getHeaders(user)
     url = url_join(
         configService.getHost(),
         "api/generic/",
         entity1.__entityName__,
         str(entity1.id),
         entity2.__entityName__,
         str(entity2.id),
     )
     response = requestService.put(url, headers=headers)
     return json.loads(response.content)
Example #9
0
    def newEntity(self, user, entityClass, fields):
        headers = getHeaders(user=user)
        url = url_join(configService.getHost(), "/api/generic/",
                       entityClass.__entityName__)
        fields = dict(
            filter(lambda field: field[1] is not None, fields.items()))

        if "group_id" not in fields and getattr(entityClass,
                                                "__hasParentGroup__", False):
            fields["group_id"] = user.activeWorkspace

        response = requestService.post(url, headers=headers, json=fields)
        return entityClass(json.loads(response.content), user)
Example #10
0
 def getPermissions(self, entity):
     entityName = entity.__entityName__
     headers = getHeaders(entity.__user__)
     url = url_join(
         configService.getHost(),
         "api/generic/",
         "acl",
         entityName.replace("-", "_"),
         str(entity.id),
     )
     response = requestService.get(url, headers=headers)
     resp = json.loads(response.content)
     return listToClass(resp["group_permissions"], Permission, entity)
Example #11
0
    def revokePermission(self, entity, workspace_id):
        entityName = entity.__entityName__

        headers = getHeaders(entity.__user__)
        url = url_join(configService.getHost(), "api/generic/", "acl")

        params = {
            "id": entity.id,
            "entity_class": entityName.replace("-", "_"),
            "action": "revoke",
            "group_id": workspace_id,
        }
        requestService.post(url, headers=headers, json=params)
        return entity
Example #12
0
    def removeFromCollection(self, entity, collection_id):
        entityName = entity.__entityName__

        headers = getHeaders(entity.__user__)
        url = url_join(
            configService.getHost(),
            "api/generic/",
            entityName,
            str(entity.id),
            Collection.__entityName__,
            str(collection_id),
        )
        response = requestService.delete(url, headers=headers)
        return json.loads(response.content)
Example #13
0
    def getEntity(self, user, entityClass, id, isDeleted="both"):
        if getattr(entityClass, "__isLegacy__", None):
            return self.getLegacyEntity(user, entityClass, id)

        identifier = 'guid' if getattr(entityClass, "__hasGuid__",
                                       None) else 'id'

        params = {"is_deleted": isDeleted, "get_single": 1, identifier: id}

        headers = getHeaders(user=user)
        url = url_join(configService.getHost(), "/api/generic/",
                       entityClass.__entityName__)
        response = requestService.get(url, headers=headers, params=params)
        return entityClass(json.loads(response.content), user)
Example #14
0
 def newInvitations(self,
                    user,
                    invitationType,
                    emails,
                    organization_id,
                    workspace_id=None):
     url = url_join(configService.getHost(), 'api/generic',
                    'share-link-invitation', invitationType)
     headers = getHeaders(user=user)
     json = {
         'emails': emails,
         'organization_id': organization_id,
         'organization_group_id': workspace_id
     }
     requestService.post(url=url, headers=headers, json=json)
Example #15
0
    def editEntity(self, entity, fields):
        # Filter the 'fields' dictionary by removing {'fields': None}
        # to preserve the existing data in the 'fields', otherwise
        # the 'fields' will be overwritten to 'None'.
        identifier = entity.guid if getattr(entity,
                                            "__hasGuid__", None) and hasattr(
                                                entity, 'guid') else entity.id

        newFields = dict(
            filter(lambda field: field[1] is not None, fields.items()))
        headers = getHeaders(entity.__user__)
        url = url_join(configService.getHost(), "/api/generic/",
                       entity.__entityName__, str(identifier))
        response = requestService.put(url, json=newFields, headers=headers)
        entity.__init__(json.loads(response.content), entity.__user__)
        return entity
Example #16
0
    def newFile(self, user, filepath=None, rawData=None, extraParams={}):
        if filepath is not None:
            rawData = open(filepath, 'rb')
        if rawData is None:
            raise Exception('Please specify filepath or raw data')

        files = {'file': rawData}
        params = {"group_id": user.activeWorkspace, **extraParams}
        headers = getHeaders(user=user)
        url = url_join(configService.getHost(), "/api/generic/file/upload")
        response = requestService.post(url,
                                       headers=headers,
                                       files=files,
                                       data=params)
        data = json.loads(response.content)
        return File(list(data.values())[0], user)
Example #17
0
    def acceptSharelink(self, token):
        """
        Accept a sharelink

        Parameters
        ----------
        token (class)
            The token of the sharelink.

        Returns
        -------
        None
        """
        # FIXME Refactor
        headers = getHeaders(self)
        url = url_join(configService.getHost(), "/api/generic/", "share-link",
                       'accept', token)
        requestService.post(url, headers=headers)
        return None
Example #18
0
    def getHTML(self, entity):
        """
        Gets HTML summary of experiments / protocols.

        """
        headers = getHeaders(entity.__user__)

        body = {
            "group_id": entity.__user__.activeWorkspace,
            "query_entity_name": entity.__entityName__.replace("-", "_"),
            "query_parameters": {
                "id": entity.id
            },
            "type": "html"
        }

        url = url_join(configService.getHost(), 'api/generic', 'entity-export')
        response = requestService.post(url, json=body, headers=headers)

        return json.loads(response.content)['html']
Example #19
0
    def getEntities(self, user, entityClass, count, filterParams={}):
        countParameter = min(count, 50) if count is not None else None
        searchParams = {"search": 1, "cursor": -1, "count": countParameter}

        params = {**searchParams, **filterParams}

        headers = getHeaders(user=user)
        url = url_join(configService.getHost(), "/api/generic/",
                       entityClass.__entityName__)
        response = requestService.get(url, params=params, headers=headers)
        resp = json.loads(response.content)
        items = resp["items"]
        expectedResults = min(resp["total"],
                              count) if count is not None else resp["total"]
        while len(items) < expectedResults:
            params["cursor"] = resp["next_cursor"]
            response = requestService.get(url, headers=headers, params=params)
            resp = json.loads(response.content)
            items.extend(resp["items"])
        return listToClass(items, entityClass, user)
Example #20
0
    def newUser(
        self,
        first_name,
        last_name,
        email,
        password,
        share_link_token=None,
        extraParams={},
    ):
        url = url_join(configService.getHost(), "public-api/user")
        params = {
            "first_name": first_name,
            "last_name": last_name,
            "email": email,
            "password": password,
            "share_link_token": share_link_token,
            **extraParams,
        }

        params = dict(
            filter(lambda field: field[1] is not None, params.items()))

        response = requestService.post(url=url, json=params, headers=None)
        return User(json.loads(response.content))
Example #21
0
 def deleteTag(self, tag):
     headers = getHeaders(tag.__user__)
     url = url_join(configService.getHost(), "/api/generic/",
                    Tag.__entityName__, str(tag.id))
     requestService.delete(url, headers=headers)
     return None
Example #22
0
 def getLegacyEntity(self, user, entityClass, id):
     headers = getHeaders(user=user)
     url = url_join(configService.getHost(), "/api/generic/",
                    entityClass.__entityName__, str(id))
     response = requestService.get(url, headers=headers)
     return entityClass(json.loads(response.content), user)
Example #23
0
 def removeMember(self, member):
     url = url_join(configService.getHost(), 'api/generic',
                    'user-group', str(member.id))
     headers = getHeaders(member.__user__)
     return requestService.delete(url, headers)
Example #24
0
 def login(self, username, password):
     params = {"username": username, "password": password}
     url = url_join(configService.getHost(), "/public-api/user/login")
     response = requestService.post(url=url, json=params, headers={})
     return User(json.loads(response.content))
Example #25
0
 def authenticate(self, username, apikey):
     url = url_join(configService.getHost(), "api/generic/user/info")
     response = requestService.get(url, headers={"apikey": apikey})
     user = json.loads(response.content)
     user["api_key"] = apikey
     return User(user)
Example #26
0
 def getUser(self, username, apikey):
     url = url_join(configService.getHost(), f"api/generic/user/{username}")
     response = requestService.get(url, headers={"apikey": apikey})
     user = json.loads(response.content)
     return User(user)