Lecture 03 · Application Programming Interfaces
المحاضرة 03 · واجهات برمجة التطبيقات
REST and its six constraints
REST وقيوده الستة
P1 MUSTREST is a style, not a protocol: six rules, five mandatory and one optional, each one illustrated by the same smart lamp.
REST أسلوب معماري وليس بروتوكولاً: ست قواعد، خمس منها إلزامية وواحدة اختيارية، وكل قاعدة موضّحة بنفس المصباح الذكي.
What REST is, in three facts
ما هو REST، في ثلاث حقائق
Slide 17 gives the whole definition. REST stands for REpresentational State Transfer. It was introduced by Roy Thomas Fielding in his doctoral dissertation in 2000. Memorise the name, the person and the year together, because a true/false question can change any one of them.
الشريحة 17 تعطي التعريف كاملاً. REST اختصار لـ Representational State Transfer. قدّمه Roy Thomas Fielding في رسالة الدكتوراه عام 2000. احفظ الاسم والشخص والسنة معاً، لأن سؤال صح أو خطأ قد يغيّر أياً منها.
- REST is an architectural style, not a protocol. It is a set of design rules for web-based APIs. HTTP is the protocol; REST tells you how to use HTTP well.
REST أسلوب معماري (architectural style) وليس بروتوكولاً. هو مجموعة قواعد تصميم لواجهات API على الويب. البروتوكول هو HTTP، وREST يخبرك كيف تستخدم HTTP بشكل صحيح.
- REST leverages HTTP to design scalable, stateless APIs. The slide's summary box: "REST is a design style that uses HTTP properly to build scalable, stateless APIs."
REST يستفيد من HTTP لتصميم واجهات قابلة للتوسع (scalable) وعديمة الحالة (stateless). وملخص الشريحة: REST أسلوب تصميم يستخدم HTTP كما ينبغي لبناء واجهات قابلة للتوسع وعديمة الحالة.
- Statelessness means two things: the server does not store client session data, and every request must contain all the necessary information.
انعدام الحالة يعني أمرين: الخادم لا يحفظ بيانات جلسة العميل (session data)، وكل طلب يجب أن يحمل كل المعلومات اللازمة لمعالجته.
"REST is a protocol" is false. HTTP is the protocol. REST is the style. A distractor may also say "REST was introduced by Tim Berners-Lee" or "in a 2000 RFC". Both are wrong: Fielding, doctoral dissertation, 2000.
عبارة "REST بروتوكول" خاطئة. البروتوكول هو HTTP، وREST هو الأسلوب. وقد يقول خيار مضلّل إن REST قدّمه Tim Berners-Lee أو إنه صدر في RFC عام 2000. كلاهما خطأ: Fielding، رسالة دكتوراه، 2000.
Six constraints: five mandatory, one optional
ستة قيود: خمسة إلزامية وواحد اختياري
Slides 18 and 19 list the constraints. An API is only "RESTful" if it obeys the five mandatory ones. The sixth, code on demand, is the only optional constraint.
تسرد الشريحتان 18 و19 القيود. لا تُسمّى الواجهة RESTful إلا إذا التزمت بالقيود الخمسة الإلزامية. أما السادس، code on demand، فهو القيد الاختياري الوحيد.
الرسم يتتبّع طلب PATCH واحداً للمصباح 123. الأرقام تبيّن أين يعمل كل قيد: 1 الفصل بين التطبيق والخادم، 2 و4 داخل الطلب، 3 و4 داخل الاستجابة، 5 البوابة في المنتصف، و6 شيفرة اختيارية يرسلها الخادم.
| # | Constraintالقيد (Constraint) | What it requiresما الذي يتطلبه | Lamp example on the slideمثال المصباح في الشريحة |
|---|---|---|---|
| 1 | Client-ServerClient-Server (عميل وخادم) | Separation of client and server responsibilitiesالفصل بين مسؤوليات العميل ومسؤوليات الخادم | A mobile app controls a lamp through a REST API, while the server manages the lamp's stateتطبيق جوال يتحكم في مصباح عبر REST API، بينما يدير الخادم حالة المصباح |
| 2 | StatelessnessStatelessness (عدم حفظ الحالة) | Each request is self-contained and independent of previous requestsكل طلب مكتفٍ بذاته ومستقل عن الطلبات السابقة | Every request includes the lamp ID and required authentication, without relying on previous requestsكل طلب يتضمن معرّف المصباح والمصادقة المطلوبة، دون الاعتماد على طلبات سابقة |
| 3 | CacheabilityCacheability (قابلية التخزين المؤقت) | Responses indicate whether they can be cached and reusedالاستجابات تبيّن هل يمكن تخزينها مؤقتاً وإعادة استخدامها | The server returns Cache-Control: max-age=60, so lamp information can be reused for 60 secondsالخادم يُرجع Cache-Control: max-age=60، فيمكن إعادة استخدام معلومات المصباح لمدة 60 ثانية |
| 4 | Uniform InterfaceUniform Interface (واجهة موحّدة) | Standardized interaction with resources, through four subconstraintsتعامل موحّد مع الـ resources عبر أربعة قيود فرعية | URIs, PATCH with JSON, self-descriptive headers, HATEOAS links (see below)URIs، وPATCH مع JSON، وheaders تصف نفسها، وروابط HATEOAS (انظر أدناه) |
| 5 | Layered SystemLayered System (نظام طبقي) | Supports intermediary layers such as proxies, gateways and load balancersيدعم طبقات وسيطة مثل الـ proxies والـ gateways وموازنات الحمل (load balancers) | A lamp request passes through an API gateway before reaching the smart-home serversطلب المصباح يمر عبر API gateway قبل أن يصل إلى خوادم المنزل الذكي |
| 6 | Code on Demand (optional)Code on Demand (اختياري) | Servers may provide executable code to clientsيجوز للخوادم أن تقدّم كوداً قابلاً للتنفيذ للعملاء | The server sends JavaScript to the browser to provide an interactive lamp-control interfaceالخادم يرسل JavaScript إلى المتصفح ليقدّم واجهة تفاعلية للتحكم في المصباح |
يجمع هذا الجدول القيود الستة كما وردت في الشريحتين، مع مثال المصباح لكل قيد. ركّز على العمود الأخير لأن أسئلة matching تعطيك المثال وتطلب القيد. لاحظ أن القيود من 1 إلى 5 إلزامية، وأن Code on Demand وحده اختياري.
"Cats Sleep Calmly Under Lamp Cords." Client-server, Stateless, Cacheable, Uniform interface, Layered, Code on demand. The last word is the odd one out, and code on demand is the odd (optional) constraint.
"Cats Sleep Calmly Under Lamp Cords": الحروف الأولى تعطي Client-server، Stateless، Cacheable، Uniform interface، Layered، Code on demand. الكلمة الأخيرة هي الشاذة، كما أن code on demand هو القيد الشاذ (الاختياري).
Statelessness, precisely
الـ Statelessness بدقة
"Stateless" does not mean the server holds no data. The server definitely stores the lamp: it is ON, brightness 80. What it does not store is session state, meaning memory of what this particular client did in earlier requests. Compare two designs for "set lamp 123 to 80":
كلمة stateless لا تعني أن الخادم لا يحمل أي بيانات. الخادم بالتأكيد يحفظ المصباح: مُشغَّل، والسطوع 80. ما لا يحفظه هو حالة الجلسة (session state)، أي ذاكرة ما فعله هذا العميل بالذات في طلبات سابقة. قارن تصميمين لعملية "اجعل المصباح 123 على 80":
Stateful (not REST)
مع حفظ الحالة Stateful (ليس REST)
1. LOGIN user=majid 2. SELECT lamp 123 3. SET brightness 80
Request 3 only makes sense because the server remembers requests 1 and 2. If a load balancer sends request 3 to a different server, that server has no idea which lamp or which user.
الطلب الثالث لا معنى له إلا لأن الخادم يتذكر الطلبين الأول والثاني. لو أرسل load balancer الطلب الثالث إلى خادم آخر، فلن يعرف ذلك الخادم أي مصباح ولا أي مستخدم.
Stateless (REST)
بدون حفظ الحالة Stateless (REST)
PATCH /lamps/123
Authorization: Bearer eyJ...
{ "brightness": 80 }
One request carries the lamp ID (in the URI), the identity (in the token) and the change (in the body). Any server behind the balancer can process it alone. This is why statelessness gives scalability.
طلب واحد يحمل رقم المصباح (في URI)، والهوية (في token)، والتغيير (في body). أي خادم خلف load balancer يستطيع معالجته وحده. ولهذا يمنح انعدام الحالة قابلية التوسع.
Cacheability, worked
الـ Cacheability بمثال محلول
The app polls GET /lamps/123 every 10 s for 2 minutes. The response carries Cache-Control: max-age=60. How many requests reach the server?
التطبيق يرسل GET /lamps/123 كل 10 s لمدة دقيقتين. والاستجابة تحمل Cache-Control: max-age=60. كم طلباً يصل إلى الخادم؟
requests at t = 0, 10, 20, ..., 110 s -> 120 / 10 = 12 requests t = 0 cache empty -> server (copy stored, fresh until age 60) t = 10..50 age 10..50 < 60 -> cache (5 requests) t = 60 age 60, not fresh -> server (new copy stored) t = 70..110 age 10..50 < 60 -> cache (5 requests) server hits = 2, cache hits = 10, load cut = 10 / 12 = 83%
The price is staleness: if someone dims the lamp at t = 15, the app keeps showing the old brightness until t = 60.
الثمن هو تقادم البيانات (staleness): لو خفّض أحدهم سطوع المصباح عند t = 15، سيبقى التطبيق يعرض السطوع القديم حتى t = 60.
Uniform interface: the four subconstraints
الواجهة الموحّدة: القيود الفرعية الأربعة
Constraint 4 is the one with internal structure, and the slide gives a lamp example for each of its four parts. Expect a matching question here.
القيد الرابع هو الوحيد الذي له بنية داخلية، والشريحة تعطي مثال مصباح لكل جزء من أجزائه الأربعة. توقّع سؤال matching هنا.
| Subconstraintالقيد الفرعي | Meaningالمعنى | Lamp example (slide 19)مثال المصباح (الشريحة 19) |
|---|---|---|
| i. Resource identification (URIs)i. تعريف الـ resource (Resource identification، أي URIs) | Every resource has its own unique identifierلكل resource معرّف فريد خاص به | /lamps/123 uniquely identifies lamp 123/lamps/123 يعرّف المصباح 123 بشكل فريد |
| ii. Manipulation through representationsii. التعديل عبر التمثيلات (Manipulation through representations) | The client changes a resource by sending a representation of it, not by calling a functionالعميل يغيّر الـ resource بإرسال تمثيل له، لا باستدعاء دالة | PATCH /lamps/123 with {"brightness":80} updates the lamp's brightnessPATCH /lamps/123 مع {"brightness":80} يحدّث سطوع المصباح |
| iii. Self-descriptive messagesiii. رسائل تصف نفسها (Self-descriptive messages) | Each message carries enough to be understood on its ownكل رسالة تحمل ما يكفي لفهمها وحدها | PATCH, Content-Type: application/json and the JSON body describe the requested updateالـ PATCH وContent-Type: application/json والـ body بصيغة JSON تصف التحديث المطلوب |
| iv. HATEOASiv. HATEOAS | Hypermedia as the engine of application state: responses contain links to the next possible actionsالوسائط الفائقة كمحرّك لحالة التطبيق (Hypermedia as the engine of application state): الاستجابات تحتوي روابط للأفعال الممكنة التالية | The lamp's response includes links/actions to retrieve, update, or delete the lampاستجابة المصباح تتضمن روابط أو أفعالاً لجلب المصباح أو تحديثه أو حذفه |
هذا الجدول يفصّل القيد الرابع إلى أجزائه الأربعة. الفرق الدقيق بين الثاني والثالث: manipulation through representations يتعلق بأنك تغيّر المورد بإرسال تمثيل له (JSON)، بينما self-descriptive messages يتعلق بأن الرسالة تشرح نفسها بالطريقة والترويسات ونوع المحتوى. وHATEOAS يعني أن الاستجابة نفسها تخبر العميل بما يمكنه فعله بعد ذلك عبر روابط.
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 123,
"ON": true,
"brightness": 80,
"links": [
{ "rel": "self", "method": "GET", "href": "/lamps/123" },
{ "rel": "update", "method": "PATCH", "href": "/lamps/123" },
{ "rel": "delete", "method": "DELETE", "href": "/lamps/123" }
]
}
That links array is HATEOAS: the client does not need to hard-code what it may do next, because the server tells it.
مصفوفة links هذه هي HATEOAS: لا يحتاج العميل إلى تثبيت الخطوات التالية في شيفرته، لأن الخادم يخبره بها.
Matching drill: example to constraint
تدريب توصيل: المثال مقابل القيد
| Scenarioالسيناريو | Constraintالقيد |
|---|---|
| The phone app and the lamp server can be updated independentlyتطبيق الهاتف وخادم المصباح يمكن تحديث كل منهما بشكل مستقل | 1 Client-Server1 Client-Server |
| Every request carries the lamp ID and a tokenكل طلب يحمل معرّف المصباح وtoken | 2 Statelessness2 Statelessness |
Cache-Control: max-age=60 on the responseCache-Control: max-age=60 على الاستجابة | 3 Cacheability3 Cacheability |
/lamps/123 names exactly one lamp/lamps/123 يسمّي مصباحاً واحداً بالضبط | 4 Uniform interface (i, URIs)4 Uniform interface (i، الـ URIs) |
Sending {"brightness":80} to change the lampإرسال {"brightness":80} لتغيير المصباح | 4 Uniform interface (ii, representations)4 Uniform interface (ii، التمثيلات) |
PATCH plus Content-Type: application/json explain the requestالـ PATCH مع Content-Type: application/json يشرحان الطلب | 4 Uniform interface (iii, self-descriptive)4 Uniform interface (iii، رسائل تصف نفسها) |
| Response contains links to retrieve, update, deleteالاستجابة تحتوي روابط للجلب والتحديث والحذف | 4 Uniform interface (iv, HATEOAS)4 Uniform interface (iv، HATEOAS) |
| Request goes through an API gateway or load balancer firstالطلب يمر أولاً عبر API gateway أو موازن حمل | 5 Layered System5 Layered System |
| Server sends JavaScript for an interactive lamp UIالخادم يرسل JavaScript لواجهة تفاعلية للمصباح | 6 Code on Demand (optional)6 Code on Demand (اختياري) |
هذا تمرين matching: كل سيناريو في العمود الأول يقابله قيد في العمود الثاني. غطِّ العمود الثاني وحاول الإجابة. انتبه أن أربعة سيناريوهات كلها تنتمي إلى القيد نفسه (uniform interface)، وأن HATEOAS ليس قيداً سابعاً بل جزء من القيد الرابع.
"REST has six mandatory constraints." False. Six constraints, five mandatory, one optional.
"لـ REST ستة قيود إلزامية." خطأ. ستة قيود، خمسة إلزامية وواحد اختياري.
The optional one is code on demand. Not caching, not layering. If a question asks which constraint a RESTful API may skip, the answer is code on demand.
القيد الاختياري هو code on demand. ليس caching ولا layered system. إذا سُئلت عن القيد الذي يمكن لواجهة RESTful أن تتجاوزه، فالجواب code on demand.
Stateless does not mean dataless. The server stores resource state (the lamp is ON at 80). It does not store session state between requests.
Stateless لا تعني بلا بيانات. الخادم يحفظ حالة المورد (المصباح مُشغَّل على 80)، لكنه لا يحفظ حالة الجلسة بين الطلبات.
HATEOAS belongs to the uniform interface. It is subconstraint iv of constraint 4, not a separate seventh constraint.
HATEOAS جزء من uniform interface. هو الجزء الرابع من القيد الرابع، وليس قيداً سابعاً مستقلاً.
Layered system is about intermediaries, not OSI layers or TCP/IP layers. Gateway, proxy, load balancer.
Layered system يتعلق بالوسطاء، وليس بطبقات OSI أو TCP/IP. أي gateway وproxy وload balancer.
Check yourself
اختبر نفسك
Which statement about REST is correct?
أي عبارة عن REST صحيحة؟
C. Slide 17: REST is an architectural style, not a protocol, introduced by Roy Thomas Fielding in his doctoral dissertation in 2000. A is wrong because REST uses HTTP rather than replacing it. D is the opposite of statelessness.
C. الشريحة 17: REST أسلوب معماري وليس بروتوكولاً، قدّمه Roy Thomas Fielding في رسالة الدكتوراه عام 2000. والخيار A خطأ لأن REST يستخدم HTTP ولا يحل محله. والخيار D عكس الـ statelessness.
REST defines six architectural constraints. Which one is optional?
يعرّف REST ستة قيود معمارية. أيها اختياري؟
D. Five constraints are mandatory and one is optional: code on demand, where the server may send executable code such as JavaScript for an interactive lamp-control interface. A, B and C are all mandatory.
D. خمسة قيود إلزامية وواحد اختياري: code on demand، حيث يجوز للخادم أن يرسل كوداً قابلاً للتنفيذ مثل JavaScript لواجهة تفاعلية للتحكم في المصباح. أما A وB وC فكلها إلزامية.
A lamp response includes links that let the client retrieve, update, or delete the lamp. This illustrates:
استجابة المصباح تحتوي روابط تسمح للعميل بجلبه أو تحديثه أو حذفه. هذا يوضّح:
B. Hypermedia-driven application state (HATEOAS) is subconstraint iv of the uniform interface. A is wrong because links are data, not executable code. D is wrong twice: self-descriptive messages is subconstraint iii, and there is no seventh constraint.
B. حالة التطبيق المدفوعة بالوسائط الفائقة (HATEOAS) هي القيد الفرعي iv من الواجهة الموحّدة. والخيار A خطأ لأن الروابط بيانات وليست كوداً قابلاً للتنفيذ. والخيار D خطأ مرتين: الرسائل التي تصف نفسها هي القيد الفرعي iii، ولا يوجد قيد سابع.
"Every request includes the lamp ID and required authentication, without relying on previous requests." Which constraint is this?
"كل طلب يتضمن رقم المصباح والمصادقة المطلوبة، دون الاعتماد على طلبات سابقة." أي قيد هذا؟
A. That sentence is slide 18's example for statelessness: each request is self-contained. D is tempting because the lamp ID appears, but resource identification is about the URI naming the lamp uniquely, not about each request standing alone.
A. هذه الجملة هي مثال الشريحة 18 على الـ statelessness: كل طلب مكتفٍ بذاته. والخيار D مغرٍ لأن معرّف المصباح يظهر، لكن تعريف الـ resource يتعلق بأن الـ URI يسمّي المصباح بشكل فريد، لا بأن كل طلب قائم بذاته.
True or false reasoning: "Because REST is stateless, the server cannot store the lamp's brightness."
"لأن REST عديم الحالة، لا يستطيع الخادم حفظ سطوع المصباح." ما الحكم؟
C. Slide 18's client-server example says the server manages the lamp's state. Statelessness only forbids keeping client session data between requests. D is wrong because the optional constraint is code on demand.
C. مثال client-server في الشريحة 18 يقول إن الخادم يدير حالة المصباح. والـ statelessness يمنع فقط الاحتفاظ ببيانات جلسة العميل بين الطلبات. والخيار D خطأ لأن القيد الاختياري هو code on demand.