إنشاء دورة باستخدام API¶
هذا الدليل ينشئ دورة، يضيف فصلًا، يضيف درس اختبار (glossary:quiz)، يقرأه مرة أخرى، يغيره بأمان، ثم يزيل الدورة — باستخدام فقط مفتاح API.
أنت بحاجة إلى مفتاح مع Edit (تغيير) على الدورة, الوحدات (تُسمى فصولًا في API) و الدرس
(كيفية الحصول عليه), ويُخزن في YOSHUKO_API_KEY.
كل استدعاء يُرجع الكائن الذي أنشأه، بما في ذلك id (معرفه ID)، والذي يستخدمه الاستدعاء التالي.
1. إنشاء دورة¶
الـ slug يصبح جزءًا من عنوان الويب للدورة، لذا يجب أن يكون فريدًا في مؤسستك.
الرد هو 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.
الكل جاهز للتشغيل¶
Copy either program, set YOSHUKO_API_KEY, and run it. It creates a course, a chapter and a
quiz, reads the quiz back, changes it, and removes the course again so your list of courses is
left as it was.
#!/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"])
المرجع¶
كل حقل ورد لهذه نقاط الاتصال: الدورات, الفصول, الدروس.