Skip to main content

A client to the LimeSurvey Remote Control API 2, written in modern Python.

Project description

Citric

Tests Documentation Status Updates codecov FOSSA Status PyPI version Python versions PyPI - Downloads

A client to the LimeSurvey Remote Control API 2, written in modern Python.

Installation

$ pip install citric

Usage

For the full JSON-RPC reference, see the RemoteControl 2 API docs.

Get surveys and questions

from citric import Client

LS_URL = "http://localhost:8001/index.php/admin/remotecontrol"

with Client(LS_URL, "iamadmin", "secret") as client:
    # Get all surveys from user "iamadmin"
    surveys = client.list_surveys("iamadmin")

    for s in surveys:
        print(s["surveyls_title"])

        # Get all questions, regardless of group
        questions = client.list_questions(s["sid"])
        for q in questions:
            print(q["title"], q["question"])

Export responses to a pandas dataframe

import io
import pandas as pd

survey_id = 123456

df = pd.read_csv(
    io.BytesIO(client.export_responses(survey_id, file_format="csv")),
    delimiter=";",
    parse_dates=["datestamp", "startdate", "submitdate"],
    index_col="id",
)

Custom requests session

It's possible to use a custom session object to make requests. For example, to cache the requests and reduce the load on your server in read-intensive applications, you can use requests-cache:

import requests_cache

cached_session = requests_cache.CachedSession(
    expire_after=3600,
    allowable_methods=["POST"],
)

with Client(
    LS_URL,
    "iamadmin",
    "secret",
    requests_session=cached_session,
) as client:

    # Get all surveys from user "iamadmin"
    surveys = client.list_surveys("iamadmin")

    # This should hit the cache. Running the method in a new client context will
    # not hit the cache because the RPC session key would be different.
    surveys = client.list_surveys("iamadmin")

Use a different authentication plugin

By default, this client uses the internal database for authentication but arbitrary plugins are supported by the auth_plugin argument.

with Client(
    LS_URL,
    "iamadmin",
    "secret",
    auth_plugin="AuthLDAP",
) as client:
    ...

Common plugins are Authdb (default), AuthLDAP and Authwebserver.

Get uploaded files and move them to S3

import base64
import io

import boto3
from citric import Client

s3 = boto3.client("s3")

with Client(
    "https://mylimeserver.com/index.php/admin/remotecontrol",
    "iamadmin",
    "secret",
) as client:
    survey_id = 12345
    files = client.get_uploaded_files(survey_id)
    for file in files:
        content = base64.b64decode(files[file]["content"])  # Decode content
        question_id = files[file]["meta"]["question"]["qid"]
        s3.upload_fileobj(
            io.BytesIO(content),
            "my-s3-bucket",
            f"uploads/{survey_id}/{question_id}/{file}",
        )

Fall back to citric.Session for low-level interaction

This library doesn't (yet) implement all RPC methods, so if you're in dire need to use a method not currently supported, you can use the session attribute to invoke the underlying RPC interface without having to pass a session key explicitly:

# Call the copy_survey method, not available in Client
with Client(LS_URL, "iamadmin", "secret") as client:
    new_survey_id = client.session.copy_survey(35239, "copied_survey")

Development

Use pyenv to setup default Python versions for this repo:

pyenv local 3.10.0 3.9.7 3.8.11 3.7.11

Install project dependencies

poetry install

Docs

To generate the documentation site, use the following commands:

poetry install -E docs
poetry run sphinx-build docs build

Docker

You can setup a local instance of LimeSurvey with Docker Compose:

docker-compose up -d

Now you can access LimeSurvey at port 8001.

Import an existing survey file and start testing with it:

from citric import Client

LS_URL = "http://localhost:8001/index.php/admin/remotecontrol"

with Client(LS_URL, "iamadmin", "secret") as client:
    # Import survey from a file
    with open("examples/limesurvey_survey_432535.lss", "rb") as f:
        survey_id = client.import_survey(f, "lss")
    print("New survey:", survey_id)

Testing

This project uses nox for running tests and linting on different Python versions:

pip install --user --upgrade nox
nox -r

Run only a linting session

nox -rs lint

pre-commit

pip install --user --upgrade pre-commit
pre-commit install

Releasing an upgrade

Bump the package version

poetry version <version>
poetry publish

Credits

License

FOSSA Status

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

citric-0.0.9.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

citric-0.0.9-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file citric-0.0.9.tar.gz.

File metadata

  • Download URL: citric-0.0.9.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for citric-0.0.9.tar.gz
Algorithm Hash digest
SHA256 fbc7c2e5c818cadea537304561eb5eb0468153f4eee78445bde4fefef9d81fee
MD5 0485288db3ff3e3038cefad792c21e64
BLAKE2b-256 05da2a87337c6cc2edfe3d72bcbbe7083c3a8591cf16daeebbd114e28b5f8de2

See more details on using hashes here.

File details

Details for the file citric-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: citric-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for citric-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d2297913899b3982adbfbffff6e5a183914983e461511f9334ffa4f66bbe13f6
MD5 b440dc03b5cd1435571e09d3841b7ca3
BLAKE2b-256 591b54bff71f654689f58d0c5180a79468fd41f7b40cfacdebfe4dd27b7ccd16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page