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)
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)
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)
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
def ping(): """ Ping the API. """ headers = getHeaders() url = url_join(configService.getHost(), 'ping') response = requestService.get(url, headers=headers) return json.loads(response.content)
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))
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)
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)
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)
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
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)
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)
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)
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)
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
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
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)
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']
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
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)
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)