Esempio n. 1
0
	def _get(self,format,params,maxlen,tablename,api,getparams):
		# define response
		data = {'response':{}}
		# turn params into database request
		wherecond = []
		paramerror = []
		for each in params:
			if each in getparams:
				wherecond.append('%s=$%s' %(each,each))
			else:
				paramerror.append(each+" is not an accepted key.")
		# stop if param errors
		if len(paramerror) > 0:
			" and ".join(paramerror)
			data['response']['error'] = get_error(paramerror)
		else:
			if len(wherecond) == 0:
				results = db.select(tablename)
				rlist = results.list()	
			elif len(wherecond) > 0:
				wherecond = ' AND '.join(wherecond)
				results = db.select(tablename,where=wherecond,vars=params)
				rlist = results.list()
			# errors
			if len(rlist) == 0:
				data['response']['error'] = get_error(1)
			elif maxlen == 1 and len(rlist) > 1:
				data['response']['error'] = get_error(2)
			# accepts
			elif maxlen == 1 and len(rlist) == 1:		
				if params.has_key('include_citations'):
					include_citations = params['include_citations']
				else:
					include_citations = 0			
				data['response'][api] = self._build_data(rlist[0],format,getparams,int(include_citations))
			elif maxlen == None and len(rlist) >= 1:
				data['response'][api] = []
				for each in rlist:
					if params.has_key('include_citations'):
						ic = params['include_citations']
					else:
						ic = 0
					item = self._build_data(each,format,getparams,int(ic))
					data['response'][api].append({api[:-1]:item})
		if format == 'json':
			return json.dumps(data)
		elif format == 'xml':
			res =  XMLDict.convert_dict_to_xml(data)
			return res
Esempio n. 2
0
    def createResponse(
        status,
        code,
        result,
        serialize,
        request,
        format='json',
        header=None,
    ):
        """ Create and format a response
            Available formats: JSON ('json'), XML ('xml')
        """

        format = format.lower()

        # set status

        if status == False:
            status = 'failure'
        else:
            status = 'success'

        if serialize:
            output = [i.serialize for i in result]
        else:
            output = result

        res = Response('')

        # set header

        if format == 'xml':
            res.content_type = 'application/xml'
        else:

            # default - format = "json"

            format = 'json'
            res.content_type = 'application/json'

        if header != None:
            res.headerlist = header

        if format == 'xml':
            response = \
                XMLDict.convert_dict_to_xml({'response': {'status': status,
                    'code': code, 'data': output}})
        else:

            # default - format = "json":

            response = json.dumps({
                'status': status,
                'code': code,
                'data': output
            })

        callback = request.params.get('callback', None)
        if callback:
            response = callback + '(' + response + ')'
            res.text = response
        else:
            res.body = response

        return res
Esempio n. 3
0
    def createResponse(
        status,
        code,
        result,
        serialize,
        request,
        format='json',
        header=None,
        ):
        """ Create and format a response
            Available formats: JSON ('json'), XML ('xml')
        """

        format = format.lower()

        # set status

        if status == False:
            status = 'failure'
        else:
            status = 'success'

        if serialize:
            output = [i.serialize for i in result]
        else:
            output = result

        res = Response('')

        # set header

        if format == 'xml':
            res.content_type = 'application/xml'
        else:

              # default - format = "json"

            format = 'json'
            res.content_type = 'application/json'

        if header != None:
            res.headerlist = header

        if format == 'xml':
            response = \
                XMLDict.convert_dict_to_xml({'response': {'status': status,
                    'code': code, 'data': output}})
        else:

              # default - format = "json":

            response = json.dumps({'status': status, 'code': code,
                                  'data': output})

        callback = request.params.get('callback', None)
        if callback:
            response = callback + '(' + response + ')'
            res.text = response
        else:
            res.body = response

        return res