+
Skip to content

FastAPI application serving as a bridge to simplify interaction with the Moodle API, providing discoverable endpoints and clear parameter guidelines.

License

Notifications You must be signed in to change notification settings

MoodleNG/MoodlewareAPI

Repository files navigation

MoodlewareAPI

MoodlewareAPI Logo

A FastAPI bridge that simplifies interaction with the Moodle API, providing discoverable endpoints and clear parameter guidelines. Built with FastAPI and Docker.

visits

🚀 Features

  • Configuration-driven via config.json
  • Dynamic FastAPI routes generated from config
  • Built-in token retrieval endpoint
  • Interactive API docs (Swagger UI)
  • Docker-ready

📦 Run

Docker

Use the included compose.yaml or run the image directly.

Docker Compose (recommended):

services:
  moodlewareapi:
    build: .
    ports:
      - "8000:8000"
    restart: unless-stopped
    environment:
      # Option A: Fixed Moodle URL for all requests
      - MOODLE_URL=https://moodle.example.edu
      # Option B: Per-request Moodle URL (set to * or leave empty)
      # - MOODLE_URL=*
      - ALLOW_ORIGINS=*
      - LOG_LEVEL=info

Start:

docker compose up -d --build

Manual

pip install -r requirements.txt
python asgi.py

Service URLs:

🛠️ Usage

1) Get a Moodle token

  • If MOODLE_URL is unset or *, include moodle_url in the query.
  • If MOODLE_URL is a real URL, moodle_url is not required.

Example (per-request Moodle URL):

curl "http://localhost:8000/auth?moodle_url=https://moodle.school.edu&username=USER&password=PASS&service=moodle_mobile_app"

2) Call Moodle functions via REST proxy

Provide the token either via Authorization header or ?wstoken=.

Example using Authorization header (with preconfigured MOODLE_URL):

curl -H "Authorization: Bearer YOUR_TOKEN" "http://localhost:8000/core_webservice_get_site_info"

Example using query parameter (with preconfigured MOODLE_URL):

curl "http://localhost:8000/core_webservice_get_site_info?wstoken=YOUR_TOKEN"

Notes:

  • When ALLOW_ORIGINS=*, credentials are disabled per CORS spec.
  • Each response includes passthrough headers X-Moodle-Direct-URL and X-Moodle-Direct-Method for debugging.

⚙️ Environment

  • MOODLE_URL
    • Set to a full base URL (e.g., https://moodle.example.com) to use it for all requests, or
    • Set to * or leave empty to require moodle_url per request.
  • PORT (default 8000)
  • ALLOW_ORIGINS (comma-separated; * allows all without credentials)
  • LOG_LEVEL (critical|error|warning|info|debug, default info)

🔧 Config (config.json)

Minimal shape of an entry:

{
  "path": "/core_webservice_get_site_info",
  "method": "GET",
  "function": "core_webservice_get_site_info",
  "description": "Get Moodle site information & user information",
  "tags": ["Core"],
  "query_params": [
    {
      "name": "userid",
      "type": "int",
      "required": false,
      "description": "User ID"
    }
  ],
  "responses": {
    "200": {
      "description": "OK"
    }
  }
}
  • path: Path added under the Moodle base URL
  • method: HTTP method
  • function: Moodle wsfunction name (auto-added for /webservice/rest/server.php)
  • description, tags: For docs grouping
  • query_params: Parameter list with name, type (str|int|bool|float|double|list), required, default, description
  • responses: Optional OpenAPI response metadata

📋 Requirements

  • Python (see requirements.txt)
  • Docker (optional)

📄 License

MIT. See LICENSE.


Made with ❤️ by MyDrift

About

FastAPI application serving as a bridge to simplify interaction with the Moodle API, providing discoverable endpoints and clear parameter guidelines.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载