Limesurvey remotecontrol API, python glue

More
3 years 4 months ago #134289 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?
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']

Please Log in or Create an account to join the conversation.

LimeSurvey Partners
More
3 years 4 months ago - 3 years 4 months ago #134317 by Deusdeorum
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:
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:
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: 3 years 4 months ago by Deusdeorum. Reason: adding information

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #134495 by Deusdeorum

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:

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:
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
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.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #134513 by frederikprijck
I got it working in C# => prntscr.com/ar9ykj

Could you share your HTTP headers + body ?

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #134527 by Deusdeorum

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..

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #134528 by frederikprijck
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
{"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 ?
Attachments:
Last edit: 3 years 4 months ago by frederikprijck.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago #134536 by Deusdeorum
Warning: Spoiler! [ Click to expand ]


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 ):
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:

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #134539 by frederikprijck
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: 3 years 4 months ago by frederikprijck.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #134540 by Deusdeorum
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: 3 years 4 months ago by Deusdeorum.

Please Log in or Create an account to join the conversation.

More
3 years 4 months ago - 3 years 4 months ago #134541 by frederikprijck
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 ! :-)
Attachments:
Last edit: 3 years 4 months ago by frederikprijck.
The following user(s) said Thank You: Deusdeorum

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!