def runSearchRequest( self, requestStr, requestClass, responseClass, objectGenerator): """ Runs the specified request. The request is a string containing a JSON representation of an instance of the specified requestClass. We return a string representation of an instance of the specified responseClass in JSON format. Objects are filled into the page list using the specified object generator, which must return (object, nextPageToken) pairs, and be able to resume iteration from any point using the nextPageToken attribute of the request object. """ self.startProfile() try: request = protocol.fromJson(requestStr, requestClass) except protocol.json_format.ParseError: raise exceptions.InvalidJsonException(requestStr) # TODO How do we detect when the page size is not set? if not request.page_size: request.page_size = self._defaultPageSize if request.page_size < 0: raise exceptions.BadPageSizeException(request.page_size) responseBuilder = response_builder.SearchResponseBuilder( responseClass, request.page_size, self._maxResponseLength) nextPageToken = None for obj, nextPageToken in objectGenerator(request): responseBuilder.addValue(obj) if responseBuilder.isFull(): break responseBuilder.setNextPageToken(nextPageToken) responseString = responseBuilder.getSerializedResponse() self.endProfile() return responseString
def populateFromJson(self, jsonString): try: parsed = protocol.fromJson(jsonString, protocol.Experiment) except: raise exceptions.InvalidJsonException(jsonString) if parsed.message_create_time != "": self._created = parsed.message_create_time if parsed.message_update_time != "": self._updated = parsed.message_update_time self._run_time = parsed.run_time self._description = parsed.description self._molecule = parsed.molecule self._strategy = parsed.strategy self._selection = parsed.selection self._library = parsed.library self._library_layout = parsed.library_layout self._instrument_model = parsed.instrument_model self._instrument_data_file = parsed.instrument_data_file self._sequencing_center = parsed.sequencing_center self._platform_unit = parsed.platform_unit attributes = {} for key in parsed.attributes.attr: attributes[key] = { "values": protocol.toJsonDict(parsed.attributes.attr[key]) } self.setAttributes(attributes) return self
def setAttributesJson(self, attributesJson): """ Sets the attributes dictionary from a JSON string. """ try: self._attributes = json.loads(attributesJson) except: raise exceptions.InvalidJsonException(attributesJson) return self
def setSpeciesFromJson(self, speciesJson): """ Sets the species, an OntologyTerm, to the specified value, given as a JSON string. See the documentation for details of this field. """ try: parsed = protocol.fromJson(speciesJson, protocol.OntologyTerm) except: raise exceptions.InvalidJsonException(speciesJson) self._species = protocol.toJsonDict(parsed)
def runAddAnnouncement(self, flaskrequest): """ Takes a flask request from the frontend and attempts to parse into an AnnouncePeerRequest. If successful, it will log the announcement to the `announcement` table with some other metadata gathered from the request. """ announcement = {} # We want to parse the request ourselves to collect a little more # data about it. try: requestData = protocol.fromJson( flaskrequest.get_data(), protocol.AnnouncePeerRequest) announcement['hostname'] = flaskrequest.host_url announcement['remote_addr'] = flaskrequest.remote_addr announcement['user_agent'] = flaskrequest.headers.get('User-Agent') except AttributeError: # Sometimes in testing we will send protocol requests instead # of flask requests and so the hostname and user agent won't # be present. try: requestData = protocol.fromJson( flaskrequest, protocol.AnnouncePeerRequest) except Exception as e: raise exceptions.InvalidJsonException(e) except Exception as e: raise exceptions.InvalidJsonException(e) # Validate the url before accepting the announcement peer = datamodel.peers.Peer(requestData.peer.url) peer.setAttributesJson(protocol.toJson( requestData.peer.attributes)) announcement['url'] = peer.getUrl() announcement['attributes'] = peer.getAttributes() try: self.getDataRepository().insertAnnouncement(announcement) except: raise exceptions.BadRequestException(announcement['url']) return protocol.toJson( protocol.AnnouncePeerResponse(success=True))
def populateFromJson(self, jsonString): try: parsed = protocol.fromJson(jsonString, protocol.BioSample) except: raise exceptions.InvalidJsonException(jsonString) self._created = parsed.created self._updated = parsed.updated self._description = parsed.description self._disease = protocol.toJsonDict(parsed.disease) self._individualId = parsed.individual_id self._info = {} for key in parsed.info: self._info[key] = {"values": protocol.toJsonDict(parsed.info[key])} return self
def populateFromJson(self, jsonString): # TODO validate try: parsed = protocol.fromJson(jsonString, protocol.Individual) except: raise exceptions.InvalidJsonException(jsonString) self._created = parsed.created self._updated = parsed.updated self._description = parsed.description self._species = protocol.toJsonDict(parsed.species) self._sex = protocol.toJsonDict(parsed.sex) self._info = {} for key in parsed.info: self._info[key] = {"values": protocol.toJsonDict(parsed.info[key])} return self
def populateFromJson(self, jsonString): try: parsed = protocol.fromJson(jsonString, protocol.Biosample) except: raise exceptions.InvalidJsonException(jsonString) self._created = parsed.created self._updated = parsed.updated self._description = parsed.description self._disease = protocol.toJsonDict(parsed.disease) self._individualId = parsed.individual_id attributes = {} for key in parsed.attributes.attr: attributes[key] = { "values": protocol.toJsonDict(parsed.attributes.attr[key]) } self.setAttributes(attributes) return self
def runListReferenceBases(self, requestJson): """ Runs a listReferenceBases request for the specified ID and request arguments. """ # In the case when an empty post request is made to the endpoint # we instantiate an empty ListReferenceBasesRequest. if not requestJson: request = protocol.ListReferenceBasesRequest() else: try: request = protocol.fromJson( requestJson, protocol.ListReferenceBasesRequest) except protocol.json_format.ParseError: raise exceptions.InvalidJsonException(requestJson) compoundId = datamodel.ReferenceCompoundId.parse(request.reference_id) referenceSet = self.getDataRepository().getReferenceSet( compoundId.reference_set_id) reference = referenceSet.getReference(request.reference_id) start = request.start end = request.end if end == 0: # assume meant "get all" end = reference.getLength() if request.page_token: pageTokenStr = request.page_token start = paging._parsePageToken(pageTokenStr, 1)[0] chunkSize = self._maxResponseLength nextPageToken = None if start + chunkSize < end: end = start + chunkSize nextPageToken = str(start + chunkSize) sequence = reference.getBases(start, end) # build response response = protocol.ListReferenceBasesResponse() response.offset = start response.sequence = sequence if nextPageToken: response.next_page_token = nextPageToken return protocol.toJson(response)
def populateFromJson(self, jsonString): try: parsed = protocol.fromJson(jsonString, protocol.Analysis) except: raise exceptions.InvalidJsonException(jsonString) if parsed.created != "": self._created = parsed.created if parsed.updated != "": self._updated = parsed.updated self._description = parsed.description self._name = parsed.name self._type = parsed.type self._software = parsed.software attributes = {} for key in parsed.attributes.attr: attributes[key] = { "values": protocol.toJsonDict(parsed.attributes.attr[key]) } self.setAttributes(attributes) return self