Open edX REST API python package

Hi all,

I was about to start writing a python package to access the REST API of any instance easily, when a question came to my mind: does is already exist?

So please let me know if there is any effort in this direction, otherwise I will be happy to start it.

The idea, and what I have so far is something that works like this:

lms_api = OpenedxAPI(host='https://lms.example.com', client_id='ABC1234', client_secret='QWERQWER')
lms_api.change_enrollment('user@example.com', 'course-v1:ORG+COURSE+RUN', action='enroll')

Is this what you are looking for?

1 Like

Hi Sofiane!
Thanks for that! I hadn’t seen it.

I tried but it requires Django to work. Looks like it is intended to be used internally by openedx. I was looking for a package that can be used externally to make integrations. Additionally, it only provides a get method, not specific functions to access the API endpoints directly.

But I liked the way it handles oauth authentication. I think I will reuse this part and build from it.

If someone finds anything similar please let us know.

1 Like

We created one (for LabXchange). I can share most of its code with you here: Open edX API Client Example · GitHub

It has three different classes, depending on how you want to authenticate: using OAuth2 with a service user, using OAuth2 as the user who is logged in to your application (via OAuth2 SSO), or as an anonymous (not logged in) user who nevertheless has a unique session ID.

It uses Django a bit, for caching results and getting user auth data, but you can strip that out if you don’t need it. In particular the “base” client (for OAuth2 using a service user account) doesn’t depend on Django, if you remove the cache-related code. It does also use django-rest-framework’s serializers to deserialize some results, but you can also strip that out if you want.

We’re not able to support or maintain this code as a separate project right now, but feel free to use it if it’s helpful.

Example:

client = EdxAppClient(
    lms_url=settings.LMS_ROOT,
    studio_url=settings.CMS_ROOT,
    oauth_key=settings.LMS_API_AUTH_KEY,
    oauth_secret=settings.LMS_API_AUTH_SECRET,
)
print(f"This application can make API requests as the user '{client.get_username()}'")
1 Like

Thanks @braden and @BbrSofiane! I think I will make a mix of both repos and create a new one. I wanted a very thin client, without Django or other libraries. Once I have some progress I will share the repo so anyone can contribute.

1 Like

Hi all!
Well, I have finally started this project.
It consists of a package as small as possible (over all not dependent on Django) that can be imported in any python project to make calls to any LMS REST API endpoints.

Up to now, it’s intended to make anonymous calls using bearer tokens. However the code to request jwt tokens is there (just that I couldn’t find out how to authenticate a user out of a django app… your help here will be appreciated).

The code is in this repo. Only two functions are available so far: list_all_courses and change_enrollment. Anybody can contribute creating more and more functions! The final idea would be to have one function for each api entry point.

It can be installed with pip install openedx-rest-api-client.

Basic usage:

from openedx_rest_api_client import client as openedx_client

lms_url = 'https://lms.example.com'
lms_client_id = ''  # get from django admin /admin/oauth2/client/'
lms_client_secret = ''  # get from django admin /admin/oauth2/client/'

lms_client = openedx_client.OpenedxRESTAPIClient(
    base_url=lms_url, client_id=lms_client_id, client_secret=lms_client_secret)

courses = lms_client.list_all_courses()

print(courses)

Contributions are welcome!!

What can be done next:

  • Implement more client functions for the api endpoints
  • Complete test coverage
  • Implement user login to make authenticated calls
1 Like

Thank you very much for this :raised_hands: