import endpoints
import MySQLdb
from os import getenv
from protorpc.messages import IntegerField, FloatField, Message
from protorpc.remote import Service


QUERY = "SELECT Distance, Potential FROM IonPMF WHERE charge1 = %d AND " \
        "charge2 = %d AND sigma1 = %.2f AND sigma2 = %.2f"


class IonPmfRequest(Message):
    charge1 = IntegerField(1, required=True)
    charge2 = IntegerField(2, required=True)
    sigma1 = FloatField(3, required=True)
    sigma2 = FloatField(4, required=True)


class IonPmfResponse(Message):
    distance = FloatField(1, repeated=True)
    potential = FloatField(2, repeated=True)


@endpoints.api(name='ionPmfApi', version='v1', description='REST API for iPMF')
class IonPmfApiV1(Service):

    @endpoints.method(IonPmfRequest, IonPmfResponse, path='pmf/1d',
                      http_method='GET', name='ipmf.get1d')
    def get1d(self, request):
        if (getenv('SERVER_SOFTWARE') and
            getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
            db = MySQLdb.connect(unix_socket='/cloudsql/senpai-io:ion-pmf',
                                 user='root', db='iPMF')
        else:
            db = MySQLdb.connect(host='localhost', user='root', db='iPMF')

        cursor = db.cursor()
        cursor.execute(QUERY % (int(request.charge1), int(request.charge2),
                                float(request.sigma1), float(request.sigma2)))
        query_results = cursor.fetchall()
        distance = [row[0] for row in query_results]
        potential = [row[1] for row in query_results]
        return IonPmfResponse(distance=distance, potential=potential)

application = endpoints.api_server([IonPmfApiV1])

@@ -1247,16 +1247,570 @@
 ursor()%0A
+        charge1 = request.charge1%0A        charge2 = request.charge2%0A        sigma1 = request.sigma1%0A        sigma2 = request.sigma2%0A        if charge1 %3C charge2:%0A            # 1,-1 is valid, but -1,1 is not.%0A            # swap the values if necessary%0A            charge1, charge2 = charge2, charge1%0A        elif charge1 == charge2 and sigma1 %3E sigma2:%0A            # we only store the upper right half%0A            # of the sigma matrices since they are%0A            # symmetric for +1,+1 and -1,-1 charge pairs%0A            sigma1, sigma2 = sigma2, sigma1%0A%0A
         
@@ -1833,32 +1833,24 @@
 UERY %25 (int(
-request.
 charge1), in
@@ -1851,24 +1851,16 @@
 1), int(
-request.
 charge2)
@@ -1895,32 +1895,24 @@
       float(
-request.
 sigma1), flo
@@ -1914,24 +1914,16 @@
 , float(
-request.
 sigma2))
@@ -1990,14 +1990,21 @@
  = %5B
+float(
 row%5B0%5D
+)
  for
@@ -2051,14 +2051,21 @@
  = %5B
+float(
 row%5B1%5D
+)
  for

Add validation logic for input params.