import requests
import sys
import json

def CheckResponse(r):
    if str(r.status_code) == "200":
        print "Finished"
        sys.stdout.flush()
    else:
        print r
        sys.stdout.flush()
        
def cytoscape(t,namespace,command,PARAMS,host="localhost",port=1234):
    """
    General function for interacting with Cytoscape API.
    
    :param t: type of http call, ie. "POST" or "GET".
    :param namespace: namespace where the request should be executed. eg. "string"
    :param commnand: command to execute. eg. "protein query"
    :param PARAM: a dictionary with the parameters. Check your swagger normaly running on
    http://localhost:1234/v1/swaggerUI/swagger-ui/index.html?url=http://localhost:1234/v1/commands/swagger.json
    
    :returns: For "POST" the data in the content's response. For "GET" None. 
    
    eg.

    data=cytoscape("P","string","protein query",{"pubmed":"p53\np21","limit":"50"})

    """     

    for p in PARAMS.keys():
        v=str(PARAMS[p])
        v=v.replace(" ","%20")    
    
    if (t == "GET") or (t == "G"):
        P=[]
        for p in PARAMS.keys():
            P.append(str(p)+"="+str(PARAMS[p]))
        P="&".join(P)
        URL="http://"+str(host)+":"+str(port)+"/v1/commands/"+str(namespace)+"/"+str(command)+"?"+P
        print "'"+URL+"'"
        sys.stdout.flush()
        r = requests.get(url = URL)
        CheckResponse(r)
        res=None
    
    elif (t == "POST") or (t == "P"):
        URL="http://"+str(host)+":"+str(port)+"/v1/commands/"+str(namespace)+"/"+str(command)
        r = requests.post(url = URL, json = PARAMS)
        CheckResponse(r)
        res=r.content
        res=json.loads(res)
        res=res["data"]
    
    return res

@@ -223,10 +223,8 @@
 ape(
-t,
 name
@@ -270,16 +270,22 @@
 ort=1234
+,t=%22P%22
 ):%0A    %22
@@ -353,62 +353,8 @@
    %0A
-    :param t: type of http call, ie. %22POST%22 or %22GET%22.%0A
     
@@ -695,16 +695,70 @@
 er.json%0A
+    :param t: type of http call, ie. %22POST%22 or %22GET%22.%0A
     %0A   
@@ -854,13 +854,8 @@
     
-data=
 cyto
@@ -861,17 +861,17 @@
 oscape(%22
-P
+G
 %22,%22strin
@@ -875,22 +875,21 @@
 ring%22,%22p
-rotein
+ubmed
  query%22,
@@ -906,10 +906,9 @@
 %22p53
-%5Cn
+ 
 p21%22
@@ -924,16 +924,17 @@
 %2250%22%7D)%0A%0A
+%0A
     %22%22%22 
@@ -939,16 +939,68 @@
 %22     %0A%0A
+    if (t == %22GET%22) or (t == %22G%22):%0A        P=%5B%5D%0A    
     for 
@@ -1027,16 +1027,20 @@
         
+  
+  
 v=str(PA
@@ -1048,16 +1048,20 @@
 AMS%5Bp%5D)%0A
+    
         
@@ -1088,95 +1088,8 @@
 %22)  
-  %0A    %0A    if (t == %22GET%22) or (t == %22G%22):%0A        P=%5B%5D%0A        for p in PARAMS.keys():
 %0A   

Update cytoscape.py