跳转至

使用 API 创建课程

本指南创建一个课程,添加一章,添加一个测验课时,读取回来,安全地修改 它,最后删除该课程 —— 只使用一个API密钥。

你需要 一个拥有 Edit(修改)权限的 课程、单元(API 中叫章节) 和 课时 (如何获取),并存储在 YOSHUKO_API_KEY。

每次调用都会返回它创建的对象,包括它的 id(其ID),下一次调用会使用这个值。

1. 创建一个课程

课程的网址里包含 slug,所以在你的 机构中必须是唯一的。

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

响应是 201 Created,包含新课程及其 id。你的计划中的 课程限制会生效——如果你已达到上限,调用会被拒绝并附带解释。

2. 添加一章

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. 添加一个测验课时

课时的 content_type 表示它的类型 (lesson, quiz, assignment …),而 content 包含内容。对于测验,那就是题目:

{
  "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 它到 /api/v1/agent/courses/{course_id}/chapters/{chapter_id}/lessons/。它 检查的规则和你在工作室中修改课程时用的完全一样,所以一个结构不正确的测验 会被拒绝并返回 400 和一条指出字段名的消息。

4. 修改课时而不丢失任何人的工作

人们可能在你的代理工作时,在工作室里修改同一门课程。为了确保你不会悄悄覆盖他们的更改,每次修改课时都必须发回你上次读取的 updated_at 值:

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\"}"

如果有人在你读完之后修改了这课时,你会得到 409 带着 "code": "stale_lesson" 和现在这课时的 最新版本 在 lesson。再次修改到那个版本,并且再发一次带着它的 updated_at。

整个流程,准备运行

复制任一程序,设置 YOSHUKO_API_KEY,然后运行它。它会创建一门课程、一个章节和一个测验,读取测验内容,再修改它,最后删除课程,使你的课程列表保持原样。

#!/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"])

参考

这些 端点 的每个字段和响应:课程, 章节, 课时。