import mysql.connector
from mitmproxy import ctx

class Replacer:
    def __init__(self, cnx, cursor):
        self.cnx, self.cursor = cnx, cursor

    def getUser(self, flow):
        if 'proxyauth' in flow.metadata: # TODO and user != None
            return flow.metadata['proxyauth'][0]
        else:
            return ""

    def isUser(self, flow):
        self.cursor.execute("SELECT * FROM User WHERE UserId='" + self.getUser(flow) + "';") # TODO make sure this doesnt include empty str
        row = self.cursor.fetchone ()
        return not (row is None)

    def createNewEntryForSite(self, userId, host):
        ctx.log("- Creating new site entry")
        # create new host
        self.cursor.execute("INSERT INTO Host (HostName) VALUES ('" + host + "');")
        newHostId = self.cursor.lastrowid
        # add site for host
        self.cursor.execute("INSERT INTO Site (Url, HostId) VALUES ('" + host + "', " + str(newHostId) + ");")
        # add user host
        self.cursor.execute("INSERT INTO UserHost(UserId, HostId) VALUES ('" + userId + "', " + str(newHostId) + ");")
        self.cnx.commit()
        return newHostId

    def getHostId(self, flow):
        host = flow.request.pretty_host
        self.cursor.execute("SELECT HostId FROM Site WHERE Url='" + host + "';")
        row = self.cursor.fetchone ()
        if (row is None):
            return self.createNewEntryForSite(self.getUser(flow), host)
        else:
            return row[0]

    def getIdeaId(self, flow, hostId):
        self.cursor.execute("SELECT IdeaId FROM UserHost WHERE UserId='" + self.getUser(flow) + "' AND HostId=" + str(hostId) + ";")
        row = self.cursor.fetchone ()
        if (row is None):
            self.cursor.execute("INSERT INTO UserHost(UserId, HostId) VALUES ('" + self.getUser(flow) + "', " + str(hostId) + ");")
            self.cnx.commit()
            return self.cursor.lastrowid
        else:
            return row[0]

    def request(self, flow):
        ctx.log("Request recieved: " + flow.request.pretty_host + ". Metadata: " + str(flow.metadata))
        if not self.isUser(flow):
            ctx.log("- Not a user")
            return
        hostId = self.getHostId(flow)
        ideaId = self.getIdeaId(flow, hostId)
        flow.request.headers["x-idea-id"] = str(ideaId)

def start():
    cnx = mysql.connector.connect(user='root', password='Hello', database='test')
    cursor = cnx.cursor()
    return Replacer(cnx, cursor)

def done():
    self.cursor.close()
    self.cnx.close()

@@ -1,30 +1,23 @@
 import 
-mysql.connector
+requests
 %0Afrom mi
@@ -40,110 +40,33 @@
 tx%0A%0A
-class Replacer:%0A    def __init__(self, cnx, cursor):%0A        self.cnx, self.cursor = cnx, cursor%0A%0A    
+# TODO authenticate user%0A
 def 
@@ -69,30 +69,24 @@
 def getUser(
-self, 
 flow):%0A     
@@ -235,2209 +235,214 @@
 %22%22%0A%0A
-    def isUser(self, flow):%0A        self.cursor.execute(%22SELECT * FROM User WHERE UserId='%22 + self.getUser(flow) + %22';%22) # TODO make sure this doesnt include empty str%0A        row = self.cursor.fetchone ()%0A        return not (row is None)%0A%0A    def createNewEntryForSite(self, userId, host):%0A        ctx.log(%22- Creating new site entry%22)%0A        # create new host%0A        self.cursor.execute(%22INSERT INTO Host (HostName) VALUES ('%22 + host + %22');%22)%0A        newHostId = self.cursor.lastrowid%0A        # add site for host%0A        self.cursor.execute(%22INSERT INTO Site (Url, HostId) VALUES ('%22 + host + %22', %22 + str(newHostId) + %22);%22)%0A        # add user host%0A        self.cursor.execute(%22INSERT INTO UserHost(UserId, HostId) VALUES ('%22 + userId + %22', %22 + str(newHostId) + %22);%22)%0A        self.cnx.commit()%0A        return newHostId%0A%0A    def getHostId(self, flow):%0A        host = flow.request.pretty_host%0A        self.cursor.execute(%22SELECT HostId FROM Site WHERE Url='%22 + host + %22';%22)%0A        row = self.cursor.fetchone ()%0A        if (row is None):%0A            return self.createNewEntryForSite(self.getUser(flow), host)%0A        else:%0A            return row%5B0%5D%0A%0A    def getIdeaId(self, flow, hostId):%0A        self.cursor.execute(%22SELECT IdeaId FROM UserHost WHERE UserId='%22 + self.getUser(flow) + %22' AND HostId=%22 + str(hostId) + %22;%22)%0A        row = self.cursor.fetchone ()%0A        if (row is None):%0A            self.cursor.execute(%22INSERT INTO UserHost(UserId, HostId) VALUES ('%22 + self.getUser(flow) + %22', %22 + str(hostId) + %22);%22)%0A            self.cnx.commit()%0A            return self.cursor.lastrowid%0A        else:%0A            return row%5B0%5D%0A%0A    def request(self, flow):%0A        ctx.log(%22Request recieved: %22 + flow.request.pretty_host + %22. Metadata: %22 + str(flow.metadata))%0A        if not self.isUser(flow):%0A            ctx.log(%22- Not a user%22)%0A            return%0A        hostId = self.getHostId(flow)%0A        ideaId = self.getIdeaId(flow, hostId)%0A        flow.request.headers%5B%22x-idea-id%22%5D = str(ideaId)%0A%0Adef start():%0A    cnx = mysql.connector.connect(user='root', password='Hello', database='test')%0A    cursor = cnx.cursor()%0A    return Replacer(cnx, cursor)%0A%0Adef done():%0A    self.cursor.close()%0A    self.cnx.close()
+def request(flow):%0A    payload = %7B 'username': getUser(flow), 'url': flow.request.pretty_host %7D%0A    r = requests.get(%22http://localhost:3000/idea%22, params=payload)%0A    flow.request.headers%5B%22x-idea-id%22%5D = r.text
 %0A

Use API to get id