Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Limesurvey remotecontrol API, python glue

  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago #134289 by Deusdeorum
Limesurvey remotecontrol API, python glue was created by Deusdeorum
I have been using the Python glue for limesurvey where most of the API works as a charm, however, im struggling with the list_participants(), this is working, but I cannot manage to retrieve other attributes than the default ones (i.e email, firstname, lastname, ID). Have anyone managed to get the list_participants working with other attributes, not necessary in the python case but in any language?
Code:
def list_participants(self, sid, iStart=0, iLimit=100, bUnused='false', aAttributes='false', aConditions=array()):
        data = """ {    "id" : 1,
                        "method":"list_participants",
                        "params": { "sSessionKey": "%s",
                                    "iSurveyID":  %s,
                                    "iStart": %s,
                                    "iLimit": %s,
                                    "bUnused": "%s",
                                    "aAttributes": "%s",
                                    "aConditions": "%s"
                        } } """ % (self.session_key, sid, iStart, iLimit, bUnused, aAttributes, aConditions)
        return self._getJSON(data)['result']
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago - 7 years 11 months ago #134317 by Deusdeorum
Replied by Deusdeorum on topic Limesurvey remotecontrol API, python glue
Same goes for the R case, it seems like i'm missing something..

Adding the following to the list_participants remotecontrol_handle.php will work, but it still feels like i'm calling the API wrong.

Adding:
Code:
usesleft'=>$token->attributes['usesleft']
under the list_participants in remotecontrol_handle.php will return usesleft as default, but i'm sure i'm calling the API wrong

In the python glue case:
Calling the API:
Code:
lime.list_participants(sid, iStart=0, iLimit=10000, bUnused='false', aAttributes='usesleft', aConditions=None)
this will return the defaults: 'participant_info.email', 'participant_info.firstname', 'participant_info.lastname', 'tid', 'token'
Last edit: 7 years 11 months ago by Deusdeorum. Reason: adding information
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago #134495 by Deusdeorum
Replied by Deusdeorum on topic Limesurvey remotecontrol API, python glue

Deusdeorum wrote: Same goes for the R case, it seems like i'm missing something..

Adding the following to the list_participants remotecontrol_handle.php will work, but it still feels like i'm calling the API wrong.

Adding:

Code:
usesleft'=>$token->attributes['usesleft']
under the list_participants in remotecontrol_handle.php will return usesleft as default, but i'm sure i'm calling the API wrong

In the python glue case:
Calling the API:
Code:
lime.list_participants(sid, iStart=0, iLimit=10000, bUnused='false', aAttributes='usesleft', aConditions=None)
this will return the defaults: 'participant_info.email', 'participant_info.firstname', 'participant_info.lastname', 'tid', 'token'


So after adding
Code:
usesleft'=>$token->attributes['usesleft']
under the list_participants in remotecontrol_handle.php I can get the usesleft but, however, now i encountered that bUnused='false'/'true' will not have an impact but only return unused tokens in any case.
The topic has been locked.
More
7 years 11 months ago #134513 by frederikprijck
Replied by frederikprijck on topic Limesurvey remotecontrol API, python glue
I got it working in C# => prntscr.com/ar9ykj

Could you share your HTTP headers + body ?
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago #134527 by Deusdeorum
Replied by Deusdeorum on topic Limesurvey remotecontrol API, python glue

frederikprijck wrote: I got it working in C# => prntscr.com/ar9ykj

Could you share your HTTP headers + body ?


Thanks for your reply, not sure what the HTTP headers and body is, im using the glue on github

How did you manage it to work? No matter which brackets, quotemarks etc I use it still fails to give me extra attributes..
The topic has been locked.
More
7 years 11 months ago - 7 years 11 months ago #134528 by frederikprijck
Replied by frederikprijck on topic Limesurvey remotecontrol API, python glue
Hi,

I'm not aware of the python project. But the Remote API is accessed using HTTP, looking at the linked Github project I can tell it's also calling the RPC over HTTP.
To ensure everything is sent to the RPC as expected, it's a good idea to inspect the HTTP headers and body (this is generally a good idea when doing HTTP Requests).

I use fiddler ( www.telerik.com/fiddler ) for this but I'm pretty sure there will be other tools around which can sort you out.

The next thing you should do is make your call to the RPC and make sure it is recorded.
Now you can inspect the body and ensure it has a format as the screenshots attached:

This is the JSON extract of the body of the Http Post Request
Code:
{"method":"list_participants",
  "params":{
       "id":1,
       "sSessionKey":"xxxx",
        "iSurveyID":111,
        "iStart":0,
        "iLimit":100000,
        "bUnused":false,
        "aAttributes":["language","completed","usesleft"],
        "aConditions":{"usesleft":"0"} 
  }
}
 

This is copied from my succesful request.

Apart from inspecting the Request, you can also inspect the response. (See the other attached screenshot)
Here you can see that I succesfully got the 3 extra fields (usesleft, completed and language) for each token.

Also, which version of LimeSurvey are you using ?
Last edit: 7 years 11 months ago by frederikprijck.
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago #134536 by Deusdeorum
Replied by Deusdeorum on topic Limesurvey remotecontrol API, python glue
Warning: Spoiler!


Thanks a lot for your clarifications and your reply. Im on the version 2.06+ build 151018.

I'm using fiddler now as a debugger but still can't figure it out.

I have rewritten some python code for the general case (taken mostly from manual.limesurvey.org/RemoteControl_2_API ):
Code:
def list_participants():
    req = urllib2.Request(url='http://www.myurl.com/index.php/admin/remotecontrol',\
                          data='{\"method\":\"list_participants\",\"params\":{\"sSessionKey\":\"MYSESSIONKEY\",\"iSurveyID\":MYSID,\"iStart\":0,\"iLimit\":200,\"bUnused\":\"True\",\"aAttributes\":\"usesleft\",\"aConditions\":\"\"},\"id\":1}')
    req.add_header('content-type', 'application/json')
    req.add_header('connection', 'Keep-Alive')
    try:
        f = urllib2.urlopen(req)
        myretun = f.read()
        j=json.loads(myretun)
        print j
        return j['result']
    except :
        e = sys.exc_info()[0]
        print ( "<p>Error: %s</p>" % e )

However, both of the methods fails in returning usesleft where both returns the following JSON in fiddler (see picture). You think there could be something on the server side?
Attachments:
The topic has been locked.
More
7 years 11 months ago - 7 years 11 months ago #134539 by frederikprijck
Replied by frederikprijck on topic Limesurvey remotecontrol API, python glue
You have to send aAttributes as an array, your data should be:


'{\"method\":\"list_participants\",\"params\":{\"sSessionKey\":\"MYSESSIONKEY\",\"iSurveyID\":MYSID,\"iStart\":0,\"iLimit\":200,\"bUnused\":\"True\",\"aAttributes\":[\"usesleft\"],\"aConditions\":\"\"},\"id\":1}'
Last edit: 7 years 11 months ago by frederikprijck.
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 11 months ago - 7 years 11 months ago #134540 by Deusdeorum
Replied by Deusdeorum on topic Limesurvey remotecontrol API, python glue
wow, thanks a lot frederikprijck, this will indeed give back any extended attributes with [\"usesleft\"] , I really cant believe I missed that one :blink: :laugh:

Im going to pull request the github :P
Last edit: 7 years 11 months ago by Deusdeorum.
The topic has been locked.
More
7 years 11 months ago - 7 years 11 months ago #134541 by frederikprijck
Replied by frederikprijck on topic Limesurvey remotecontrol API, python glue
You see it's important to inspect the HTTP Request.
Compare your request data with mine in the attached screenshot ...

If you inspect your request with fiddler, you should see an array now aswell ...

Glad you worked it out ! :-)
Last edit: 7 years 11 months ago by frederikprijck.
The following user(s) said Thank You: Deusdeorum
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose