Example #1
0
class Api(object):
	def __init__(self):
		self.converter = Converter()

	@cherrypy.expose
	def index(self):
		return "<h1>Api Root</h1><p>Available API Functions:</p>.../api/list and .../api/convert_value"

	@cherrypy.expose
	@cherrypy.tools.json_out()
	def convert_value(self, de, para, valor):
	#TODO: MAKE CHECKING FOR CORRECT DATA TYPES WORK
		output = {
			'errors': "Undefined",
			'success': "Undefined",
			'value': "Undefined"
		}
		#BUG: value comming in from ajax call is not int, float or str ?!
		# makes it impossible to check
		if de is None or not de or para is None or not para or valor is None or not valor:
			errors = []
			output['success'] = False
			if not de:
				errors.append("Invalid FROM option was inputted!")
			if not para:
				errors.append("Invalid TO option was inputted!")
			if not valor:
				errors.append("Invalid value was inputted!")
			output['errors'] = errors
		else:
			output['errors'] = 'No errors'
			output['success'] = True
			output['value'] = self.converter.convert(de, para, valor)

		return output
	@cherrypy.expose
	@cherrypy.tools.json_out()		
	def list(self):
		output = {
			'errors': 'No errors',
			'success': "Undefined",
			'rate_list': "Undefined"
		}	
		output['rate_list'] = self.converter.list()
		output['success'] = True
		return output