Zum Inhalt

Kurs mit der API erstellen

Dieser Leitfaden erstellt einen Kurs, fügt ein Kapitel hinzu, fügt eine Quiz Lektion hinzu, liest es zurück, ändert es sicher und entfernt schließlich den Kurs — mit nichts als einem API-Schlüssel.

Sie brauchen einen Schlüssel mit Edit (ändern) auf Kursen, Einheiten (genannt Kapitel in der API) und Lektionen (wie man einen bekommt), gespeichert in YOSHUKO_API_KEY.

Jede Anfrage gibt das Objekt zurück, das sie erstellt hat, einschließlich seiner id (seines ID), welches die nächste Anfrage verwendet.

1. Kurs erstellen

Die slug wird Teil der Webadresse des Kurses, muss also in Ihrer Organisation einzigartig sein.

curl -sS -X POST "https://www-dev.yoshuko.com/api/v1/agent/courses/" \
  -H "Authorization: Bearer $YOSHUKO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"title": "Intro to Watercolour", "slug": "intro-to-watercolour"}'
course = requests.post(
    f"{BASE}/api/v1/agent/courses/",
    headers=HEADERS,
    json={"title": "Intro to Watercolour", "slug": "intro-to-watercolour"},
    timeout=30,
).json()

Die Antwort ist 201 Created mit dem neuen Kurs, einschließlich seines id. Ihre Planungsgrenze für Kurse gilt — die Anfrage wird abgelehnt mit einer Erklärung, wenn Sie dabei sind.

2. Kapitel hinzufügen

curl -sS -X POST "https://www-dev.yoshuko.com/api/v1/agent/courses/$COURSE_ID/chapters/" \
  -H "Authorization: Bearer $YOSHUKO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"title": "Unit 1: Materials", "position": 1}'

3. Quiz-Lektion hinzufügen

Die content_type einer Lektion sagt, was für eine Art sie ist (lesson, quiz, assignment …) und content enthält den Inhalt. Für ein Quiz ist das die Fragen:

{
  "title": "Knowledge check",
  "content_type": "quiz",
  "position": 1,
  "content": {
    "title": "Knowledge check",
    "passing_score": 50,
    "questions": [{
      "id": "q1",
      "type": "multiple_choice",
      "prompt_html": "<p>Which paper weight suits wet washes?</p>",
      "points": 1,
      "options": [
        {"id": "a", "html": "300 gsm", "is_correct": true},
        {"id": "b", "html": "80 gsm", "is_correct": false}
      ]
    }]
  }
}

POST es an /api/v1/agent/courses/{course_id}/chapters/{chapter_id}/lessons/. Es wird durch dieselben Regeln geprüft wie die Seite, wo Sie Kurse im Studio ändern, also wird ein Quiz, was nicht richtig gebaut wurde, abgelehnt mit 400 und einer Nachricht, die das Feld nennt.

4. Eine Lektion ändern, ohne dass jemandes Arbeit verloren geht

Menschen können den gleichen Kurs im Studio gleichzeitig wie Ihr Agent bearbeiten. Um sicherzustellen, dass Sie niemals stillschweigend ihre Änderung überschreiben, muss jede Lektion-Änderung das updated_at-Wert zurücksenden, den Sie zuletzt gelesen haben:

curl -sS -X PATCH "https://www-dev.yoshuko.com/api/v1/agent/courses/$COURSE_ID/chapters/$CHAPTER_ID/lessons/$LESSON_ID/" \
  -H "Authorization: Bearer $YOSHUKO_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"title\": \"Quick check\", \"updated_at\": \"$UPDATED_AT\"}"

Wenn jemand die Lektion nachdem Sie sie gelesen haben verändert hat, erhalten Sie 409 mit "code": "stale_lesson" und die Lektion so wie sie jetzt ist in lesson. Machen Sie Ihre Änderung erneut an dieser Version und senden Sie es erneut mit ihrem updated_at.

Alles fertig, bereit zum Start

Kopieren Sie entweder das Programm, setzen Sie YOSHUKO_API_KEY, und führen Sie es aus. Es erstellt einen Kurs, ein Kapitel und ein Quiz, liest das Quiz zurück, ändert es und entfernt den Kurs erneut, sodass Ihre Liste von Kursen so bleibt wie sie war.

#!/usr/bin/env bash
set -euo pipefail
BASE="https://www-dev.yoshuko.com/api/v1/agent"
AUTH="Authorization: Bearer $YOSHUKO_API_KEY"
JSON="Content-Type: application/json"
SLUG="api-demo-$(date +%s)"

COURSE_ID=$(curl -sSf -X POST "$BASE/courses/" -H "$AUTH" -H "$JSON" \
  -d "{\"title\": \"API demo course\", \"slug\": \"$SLUG\"}" | jq -r .id)
echo "course  $COURSE_ID"

CHAPTER_ID=$(curl -sSf -X POST "$BASE/courses/$COURSE_ID/chapters/" -H "$AUTH" -H "$JSON" \
  -d '{"title": "Unit 1", "position": 1}' | jq -r .id)
echo "chapter $CHAPTER_ID"

LESSON=$(curl -sSf -X POST "$BASE/courses/$COURSE_ID/chapters/$CHAPTER_ID/lessons/" \
  -H "$AUTH" -H "$JSON" -d '{
    "title": "Knowledge check", "content_type": "quiz", "position": 1,
    "content": {"title": "Knowledge check", "passing_score": 50, "questions": [{
      "id": "q1", "type": "multiple_choice", "prompt_html": "<p>2 + 2?</p>", "points": 1,
      "options": [{"id": "a", "html": "4", "is_correct": true},
                  {"id": "b", "html": "5", "is_correct": false}]}]}}')
LESSON_ID=$(echo "$LESSON" | jq -r .id)
UPDATED_AT=$(echo "$LESSON" | jq -r .updated_at)
echo "lesson  $LESSON_ID"

curl -sSf -X PATCH "$BASE/courses/$COURSE_ID/chapters/$CHAPTER_ID/lessons/$LESSON_ID/" \
  -H "$AUTH" -H "$JSON" -d "{\"title\": \"Quick check\", \"updated_at\": \"$UPDATED_AT\"}" \
  | jq '{title, content_type, questions: (.content.questions | length)}'

curl -sSf -X DELETE "$BASE/courses/$COURSE_ID/" -H "$AUTH"
echo "deleted $COURSE_ID"

import os
import time

import requests

BASE = "https://www-dev.yoshuko.com/api/v1/agent"
HEADERS = {"Authorization": f"Bearer {os.environ['YOSHUKO_API_KEY']}"}


def call(method, path, **kwargs):
    response = requests.request(method, BASE + path, headers=HEADERS, timeout=30, **kwargs)
    response.raise_for_status()
    return response.json() if response.content else None


course = call("POST", "/courses/", json={
    "title": "API demo course", "slug": f"api-demo-{int(time.time())}"})
try:
    chapter = call("POST", f"/courses/{course['id']}/chapters/",
                   json={"title": "Unit 1", "position": 1})
    lessons = f"/courses/{course['id']}/chapters/{chapter['id']}/lessons/"
    lesson = call("POST", lessons, json={
        "title": "Knowledge check", "content_type": "quiz", "position": 1,
        "content": {"title": "Knowledge check", "passing_score": 50, "questions": [{
            "id": "q1", "type": "multiple_choice", "prompt_html": "<p>2 + 2?</p>",
            "points": 1, "options": [{"id": "a", "html": "4", "is_correct": True},
                                     {"id": "b", "html": "5", "is_correct": False}]}]},
    })
    edited = call("PATCH", f"{lessons}{lesson['id']}/",
                  json={"title": "Quick check", "updated_at": lesson["updated_at"]})
    print(edited["title"], edited["content_type"], len(edited["content"]["questions"]))
finally:
    call("DELETE", f"/courses/{course['id']}/")
    print("deleted", course["id"])

Referenz

Jedes Feld und jede Antwort für diese Endpunkte: Kurse, Kapitel, Lektionen.