コード例 #1
0
    def post(self):
        '''
        parameters:
            q
            scopes
            fields
            email

            jsoninput   if true, input "q" is a json string, must be decoded as a list.
        '''
        kwargs = self.get_query_params()
        self._examine_kwargs('POST', kwargs)
        q = kwargs.pop('q', None)
        jsoninput = kwargs.pop('jsoninput', None) in ('1', 'true')
        if q:
            # ids = re.split('[\s\r\n+|,]+', q)
            try:
                ids = json.loads(q) if jsoninput else split_ids(q)
                if not isinstance(ids, list):
                    raise ValueError
            except ValueError:
                ids = None
                res = {'success': False, 'error': 'Invalid input for "q" parameter.'}
            if ids:
                scopes = kwargs.pop('scopes', None)
                fields = kwargs.pop('fields', None)
                res = self.esq.mget_biothings(ids, fields=fields, scopes=scopes, **kwargs)
        else:
            res = {'success': False, 'error': "Missing required parameters."}

        encode = not isinstance(res, str)    # when res is a string, e.g. when rawquery is true, do not encode it as json
        self.return_json(res, encode=encode)
        self.ga_track(event=self._ga_event_object('POST', {'qsize': len(q) if q else 0}))
コード例 #2
0
 def str_to_list(val):
     """ Cast Biothings-style str to list. """
     try:  # core splitting algorithm
         lst = split_ids(str(val))
     except ValueError as err:
         raise OptionError(str(err))
     return lst
コード例 #3
0
ファイル: helper.py プロジェクト: sirloon/biothings.api
 def str_to_list(self, val, param=''):
     if self.jsoninput:
         try:
             val = json.loads(val)
         except Exception:
             pass
     if not isinstance(val, list):
         try:
             val = split_ids(val)
         except ValueError as e:
             raise BiothingParameterTypeError(str(e), param)
     return val
コード例 #4
0
 def str_to_list(self, val, param=''):
     if self.jsoninput:
         try:
             val = json.loads(val)
         except Exception:
             pass
     if not isinstance(val, list):
         try:
             val = split_ids(str(val))
         except ValueError as e:
             raise OptionArgError(reason=str(e), param=param)
     return val
コード例 #5
0
ファイル: helper.py プロジェクト: greg-k-taylor/biothings.api
    def _typify(self, arg, argval, json_list_input=False):
        ''' Try to get the parameter's type from settings '''
        # first see if this parameter has an alias
        # do value translations, if they exist
        if 'type' not in self.kwarg_settings[arg]:
            return argval

        if self.kwarg_settings[arg]['type'] == list:
            ret = []
            if json_list_input:
                try:
                    ret = json.loads(argval)
                    if not isinstance(ret, list):
                        raise ValueError
                except Exception:
                    ret = []
                    #raise BiothingParameterTypeError('Could not listify "{}" in parameter "{}" with "jsoninput" True'.format(argval, arg))
            if not ret:
                try:
                    ret = split_ids(argval)
                except ValueError as e:
                    raise BiothingParameterTypeError(
                        "Could not split argument '{0}' due to the following error: '{1}'"
                        .format(arg, str(e)))
                #ret = [x for x in re.split(getattr(self.web_settings, 'LIST_SPLIT_REGEX', '[\s\r\n+|,]+'), argval) if x]
            ret = ret[:self.kwarg_settings[arg].get(
                'max', getattr(self.web_settings, 'LIST_SIZE_CAP', 1000))]
        elif self.kwarg_settings[arg]['type'] == int:
            try:
                ret = int(argval)
            except ValueError:
                raise BiothingParameterTypeError(
                    "Expected '{0}' parameter to have integer type.  Couldn't convert '{1}' to integer"
                    .format(arg, argval))
        elif self.kwarg_settings[arg]['type'] == float:
            try:
                ret = float(argval)
            except ValueError:
                raise BiothingParameterTypeError(
                    "Expected '{0}' parameter to have float type.  Couldn't convert '{1}' to float"
                    .format(arg, argval))
        elif self.kwarg_settings[arg]['type'] == bool:
            ret = self._boolify(argval)
        else:
            ret = argval
        return ret
コード例 #6
0
    def post(self):
        '''
        parameters:
            q
            scopes
            fields
            species

            jsoninput   if true, input "q" is a json string, must be decoded as a list.
        '''
        kwargs = self.get_query_params()
        q = kwargs.pop('q', None)
        jsoninput = kwargs.pop('jsoninput', None) in ('1', 'true')
        if q:
            # ids = re.split('[\s\r\n+|,]+', q)
            try:
                ids = json.loads(q) if jsoninput else split_ids(q)
                if not isinstance(ids, list):
                    raise ValueError
            except ValueError:
                ids = None
                res = {'success': False, 'error': 'Invalid input for "q" parameter.'}
            if ids:
                scopes = kwargs.pop('scopes', None)
                if scopes:
                    scopes = [x.strip() for x in scopes.split(',')]
                fields = kwargs.pop('fields', None)
                res = yield Task(self.esq.mget_gene2, ids, fields=fields, scopes=scopes, **kwargs)
        else:
            res = {'success': False, 'error': "Missing required parameters."}

        self.return_json(res)
        self.ga_track(event={'category': 'v2_api',
                             'action': 'query_post',
                             'label': 'qsize',
                             'value': len(q) if q else 0})