COE 558Lecture 03المحاضرة 03Part 04الجزء 04
REST: the style and its six constraintsREST: الأسلوب وقيوده الستة
What REST is and is not, the six architectural constraints (five mandatory, code on demand optional), the four sub-constraints of the uniform interface, and the five design elements every REST API must specify.ما هو REST وما ليس هو، وقيوده المعمارية الستة (خمسة إلزامية، و code on demand اختياري)، والقيود الفرعية الأربعة للواجهة الموحدة (uniform interface)، وعناصر التصميم الخمسة التي يجب أن تحددها كل واجهة REST API.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 17-21
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
The first three parts gave you the vocabulary of web APIs: endpoints, routes, methods, status codes and collection versus item URLs. This part answers a different question: what makes an API built from those parts RESTful? The answer is not a file format and not a library. It is a short list of constraints, and every one of them is a deliberate trade made so that a service can grow from one lamp to millions of devices.
This is one of the densest exam targets of the lecture. Expect to list the six constraints, say which is optional, define statelessness and say why it helps scaling, name the four uniform-interface sub-constraints, explain HATEOAS, answer "REST is a protocol: true or false", and name the five design elements. The smart-home lamp from the earlier parts carries every example.
أعطتك الأجزاء الثلاثة الأولى مفردات واجهات الويب البرمجية: نقاط النهاية (endpoints)، والمسارات (routes)، والطرق (methods)، ورموز الحالة (status codes)، والفرق بين عنوان المجموعة (collection) وعنوان العنصر الواحد (item). أما هذا الجزء فيجيب عن سؤال مختلف: ما الذي يجعل واجهة API مبنية من هذه الأجزاء RESTful؟ الجواب ليس صيغة ملفات وليس مكتبة برمجية. إنه قائمة قصيرة من القيود، وكل قيد منها مقايضة مقصودة تسمح لخدمة ما بأن تنمو من مصباح واحد إلى ملايين الأجهزة.
هذا من أكثف أجزاء المحاضرة من حيث أسئلة الاختبار. توقّع أن يُطلب منك سرد القيود الستة، وتحديد القيد الاختياري منها، وتعريف انعدام الحالة (statelessness) وبيان لماذا يساعد على التوسع، وتسمية القيود الفرعية الأربعة للواجهة الموحدة، وشرح HATEOAS، والإجابة عن سؤال "REST بروتوكول: صح أم خطأ"، وتسمية عناصر التصميم الخمسة. ومصباح المنزل الذكي من الأجزاء السابقة يرافق كل الأمثلة.
By the end you canفي نهاية هذا الجزء ستستطيع
- State who introduced REST, when and where, and explain why it is an architectural style rather than a protocol.أن تذكر من قدّم REST ومتى وأين، وأن تشرح لماذا هو أسلوب معماري (architectural style) وليس بروتوكولًا.
- List the six REST constraints in order, mark code on demand as the only optional one, and give the lamp example for each.أن تسرد قيود REST الستة بالترتيب، وتحدد أن الشيفرة عند الطلب (code on demand) هي القيد الاختياري الوحيد، وتعطي مثال المصباح لكل قيد.
- Define statelessness precisely and explain why it makes horizontal scaling and failover easy.أن تعرّف انعدام الحالة (statelessness) تعريفًا دقيقًا، وتشرح لماذا يجعل التوسع الأفقي (horizontal scaling) وتجاوز الأعطال (failover) سهلين.
- Decide from a Cache-Control: max-age value whether a later request is served from cache or must reach the server.أن تحكم من قيمة Cache-Control: max-age هل يُخدم طلب لاحق من الذاكرة المؤقتة (cache) أم يجب أن يصل إلى الخادم.
- Name the four uniform-interface sub-constraints and explain HATEOAS with a concrete response.أن تسمّي القيود الفرعية الأربعة للواجهة الموحدة (uniform interface)، وتشرح HATEOAS باستجابة ملموسة.
- Name the five design elements of a REST API and map resources, JSON and headers onto them.أن تسمّي عناصر التصميم الخمسة لواجهة REST API، وتربط بها الموارد (resources) وJSON والترويسات (headers).
Picture two teams building a lamp API. One sends GET /getLampStatus?id=123 and POST /doSetBrightness, keeps a login session in server memory, and returns a different JSON shape from every endpoint. The other uses /lamps/123 for the lamp, changes it with standard methods, and puts everything the server needs into each request. Both speak HTTP. Only the second one is following REST.
تخيّل فريقين يبنيان واجهة API لمصباح. الفريق الأول يرسل GET /getLampStatus?id=123 وPOST /doSetBrightness، ويحتفظ بجلسة تسجيل الدخول في ذاكرة الخادم، ويعيد شكلًا مختلفًا منJSON من كل نقطة نهاية. والفريق الثاني يستخدم /lamps/123 للدلالة على المصباح، ويغيّره بالطرق القياسية، ويضع في كل طلب كل ما يحتاجه الخادم. كلا الفريقين يتحدث HTTP، لكن الثاني وحده يتبع REST.
The difference is not the protocol, which is the same, but the design discipline. REST stands for REpresentational State Transfer. Roy Thomas Fielding introduced it in his doctoral dissertation at the University of California, Irvine, in 2000, as a description of the principles that had guided the design of the Web itself.
الفرق ليس في البروتوكول (protocol)، فهو نفسه لدى الفريقين، بل في الانضباط التصميمي. REST اختصار لـ REpresentational State Transfer، أي "نقل الحالة التمثيلية". قدّمه Roy Thomas Fielding في أطروحة الدكتوراه الخاصة به في جامعة كاليفورنيا، إرفاين (University of California, Irvine) عام 2000، وصفًا للمبادئ التي وجّهت تصميم الويب نفسه.
REST is an architectural style: a named set of constraints on how the components of a system may interact. A protocol fixes message formats, field names and the order of exchanges. A style fixes none of that; it says things like "the server must not remember the client between requests" and leaves the bytes to whatever protocol you choose. REST then leverages HTTP to build scalable, stateless APIs, because HTTP already has what the constraints ask for: URLs to name resources, a fixed set of methods, status codes, caching headers and media types.
REST هو أسلوب معماري (architectural style): أي مجموعة مسمّاة من القيود على الطريقة التي يُسمح بها لمكونات النظام أن تتفاعل. البروتوكول يحدد صيغ الرسائل وأسماء الحقول وترتيب التبادلات. أما الأسلوب فلا يحدد شيئًا من ذلك، بل يقول أشياء مثل "يجب ألا يتذكر الخادم العميل بين طلب وآخر"، ويترك البايتات لأي بروتوكول تختاره. ثم يستفيد REST من HTTP لبناء واجهات API قابلة للتوسع وعديمة الحالة، لأن HTTP يملك أصلًا ما تطلبه هذه القيود: عناوين URL لتسمية الموارد، ومجموعة ثابتة من الطرق، ورموز الحالة، وترويسات التخزين المؤقت، وأنواع الوسائط (media types).
Statelessness, stated preciselyانعدام الحالة بتعريف دقيق
The slide singles out one constraint before listing the others, because it is the one most people get wrong. Statelessness has two halves, and an exam answer needs both:
تخصّ الشريحة قيدًا واحدًا بالذكر قبل سرد البقية، لأنه القيد الذي يخطئ فيه أكثر الناس. انعدام الحالة (Statelessness) له شقّان، وإجابة الاختبار تحتاج إلى الاثنين:
- The server does not store client session data between requests.لا يخزّن الخادم بيانات جلسة العميل (session data) بين الطلبات.
- Every request must contain all the information necessary to process it.يجب أن يحتوي كل طلب على كل المعلومات اللازمة لمعالجته.
The two halves are the same rule seen from each side. If the server keeps nothing, the request must bring everything: which lamp, who is asking, and what change is wanted. Note what the server is allowed to keep: the lamp's own state, its brightness and power, is resource state and lives on the server. What it may not keep is conversation state, such as "this client logged in five minutes ago and was looking at lamp 123".
الشقّان هما القاعدة نفسها منظورًا إليها من كل جانب. إذا كان الخادم لا يحتفظ بشيء، فيجب أن يحمل الطلب كل شيء: أي مصباح، ومن يسأل، وما التغيير المطلوب. لاحظ ما يُسمح للخادم بالاحتفاظ به: حالة المصباح نفسه، أي سطوعه وتشغيله، هي حالة المورد (resource state) وتعيش على الخادم. أما ما لا يجوز له الاحتفاظ به فهو حالة المحادثة (conversation state)، مثل "هذا العميل سجّل دخوله قبل خمس دقائق وكان ينظر إلى المصباح 123".
Recall
Who introduced REST, in what kind of document, and in which year? Then give both halves of statelessness.من قدّم REST، وفي أي نوع من الوثائق، وفي أي عام؟ ثم اذكر شقّي انعدام الحالة.
Quick checkاختبار سريع
Which statement about REST is correct?أي العبارات التالية عن REST صحيحة؟
REST defines six architectural constraints: five mandatory and one optional. Fielding builds them up one at a time, each added constraint buying a property the Web needed. Here is the whole map before we walk through it; hover to see the lamp example on each card.
يعرّف REST ستة قيود معمارية (architectural constraints): خمسة إلزامية وواحد اختياري. يبنيها Fielding قيدًا بعد قيد، وكل قيد يُضاف يكسب خاصية كان الويب يحتاج إليها. إليك الخريطة كاملة قبل أن نمرّ عليها واحدًا واحدًا، ومرّر المؤشر لترى مثال المصباح على كل بطاقة.
1. Client-server: split the jobs1. العميل والخادم (Client-server): تقسيم المهام
A mobile app shows a slider and a power button. A server holds the truth about lamp 123: is it on, how bright is it. The app never touches the lamp directly; it asks the server through the REST API. That is the client-server constraint: separation of client and server responsibilities. The client owns the user interface, the server owns the data and its state.
تطبيق جوال يعرض شريط تمرير وزر تشغيل. والخادم يحتفظ بالحقيقة عن المصباح 123: هل هو مضاء، وكم سطوعه. التطبيق لا يلمس المصباح مباشرة أبدًا، بل يسأل الخادم عبر واجهة REST API. هذا هو قيد العميل والخادم (client-server constraint): الفصل بين مسؤوليات العميل ومسؤوليات الخادم. العميل يملك واجهة المستخدم، والخادم يملك البيانات وحالتها.
The payoff is independent evolution. You can ship an iPhone app, an Android app and a web dashboard against the same server, or rewrite the server in a new language, without changing the others, as long as the interface between them holds.
والمكسب هو التطور المستقل لكل طرف. يمكنك إطلاق تطبيق iPhone وتطبيق Android ولوحة تحكم على الويب تتعامل كلها مع الخادم نفسه، أو إعادة كتابة الخادم بلغة جديدة، دون تغيير البقية، ما دامت الواجهة بينهما ثابتة.
2. Statelessness: every request stands alone2. انعدام الحالة (Statelessness): كل طلب قائم بذاته
Now the app sends three requests in a row: read lamp 123, dim it to 80, read it again. Under statelessness each of those requests carries the lamp ID and the required authentication, and none of them relies on the request before it. The server answers each one as if it had never seen this client.
الآن يرسل التطبيق ثلاثة طلبات متتالية: اقرأ المصباح 123، ثم خفّض سطوعه إلى 80، ثم اقرأه مرة أخرى. في ظل انعدام الحالة يحمل كل طلب من هذه الطلبات معرّف المصباح والمصادقة المطلوبة، ولا يعتمد أي منها على الطلب الذي قبله. ويجيب الخادم عن كل طلب كأنه لم يرَ هذا العميل من قبل.
That is why the constraint exists. Fielding lists three gains: visibility (a monitor can understand a request by looking at that request alone), reliability (recovering from a partial failure is easy) and scalability (the server frees resources right after each request). In cloud terms the third one is the big one: if no server holds your session, a load balancer can send your next request to any of ten identical servers, and you scale by adding servers. The cost is that every request repeats some data, such as the token, which adds a few bytes to each message.
ولهذا وُجد هذا القيد. يذكر Fielding ثلاثة مكاسب: الوضوح (visibility)، إذ يستطيع نظام المراقبة فهم الطلب بالنظر إليه وحده؛ والموثوقية (reliability)، إذ يسهل التعافي من عطل جزئي؛ وقابلية التوسع (scalability)، إذ يحرر الخادم موارده فور انتهاء كل طلب. وبمصطلحات السحابة فإن المكسب الثالث هو الأهم: إذا لم يكن أي خادم يحتفظ بجلستك، يستطيع موازن الأحمال (load balancer) أن يرسل طلبك التالي إلى أي خادم من عشرة خوادم متطابقة، فتتوسع بإضافة خوادم. والثمن أن كل طلب يكرر بعض البيانات، مثل الرمز (token)، مما يضيف بضعة بايتات إلى كل رسالة.
| Aspectالجانب | Stateless (REST)عديم الحالة (REST) | Statefulذو حالة (stateful) |
|---|---|---|
| What each request carriesما يحمله كل طلب | Lamp ID, auth token and the change wantedمعرّف المصباح (lamp ID) ورمز المصادقة (auth token) والتغيير المطلوب | Only the change; the server remembers who and which lampالتغيير فقط، والخادم يتذكر من يسأل وأي مصباح |
| Where session data livesأين تعيش بيانات الجلسة | On the clientعلى العميل | In server memoryفي ذاكرة الخادم |
| Scaling outالتوسع الأفقي (scaling out) | Add servers; any server answers any requestأضف خوادم، فأي خادم يجيب عن أي طلب | Requests must stick to the server holding the sessionيجب أن تلتصق الطلبات بالخادم الذي يحتفظ بالجلسة |
| Failoverتجاوز الأعطال (failover) | Another server simply takes the next requestيتولى خادم آخر الطلب التالي ببساطة | Session is lost when that server crashesتضيع الجلسة عند تعطل ذلك الخادم |
| Costالتكلفة | A little repeated data per requestقليل من البيانات المكررة في كل طلب | Memory per client on the serverذاكرة لكل عميل على الخادم |
3. Cacheability: let responses be reused3. قابلية التخزين المؤقت (Cacheability): اسمح بإعادة استخدام الاستجابات
Lamp status does not change every millisecond. If a dashboard refreshes every few seconds, sending each refresh all the way to the server is waste. Cacheability says that responses must indicate whether they can be cached and reused. In HTTP that label is the Cache-Control header. When the server returns Cache-Control: max-age=60, it allows the lamp information to be reused for 60 s. A response that must never be reused, such as a one-time token, says so with no-store.
حالة المصباح لا تتغير كل جزء من الثانية. فإذا كانت لوحة التحكم تتحدّث كل بضع ثوانٍ، فإن إرسال كل تحديث حتى الخادم هدر. تنص قابلية التخزين المؤقت (Cacheability) على أن الاستجابات يجب أن توضّح هل يمكن تخزينها مؤقتًا وإعادة استخدامها أم لا. وفي HTTP هذه العلامة هي الترويسة Cache-Control. فعندما يعيد الخادم Cache-Control: max-age=60، فهو يسمح بإعادة استخدام معلومات المصباح لمدة 60 s. والاستجابة التي يجب ألا يُعاد استخدامها أبدًا، مثل رمز يُستخدم مرة واحدة، تصرّح بذلك عبر no-store.
Worked exampleمثال محلول
Is the cached lamp status still usable?هل ما زالت حالة المصباح المخزنة مؤقتًا صالحة للاستخدام؟
The responseالاستجابة
The server generates a response for GET /lamps/123 at t = 0 s with Cache-Control: max-age=60. Its freshness lifetime is 60 s, counted from when the origin server generated it.يولّد الخادم استجابة لـ GET /lamps/123 عند t = 0 s ومعها Cache-Control: max-age=60. مدة صلاحيتها (freshness lifetime) هي 60 s، تُحسب من لحظة توليدها في الخادم الأصلي (origin server).Request at 45 sطلب عند 45 s
The stored copy is 45 s old. Since 45 < 60, it is still fresh, so the cache answers and the request never reaches the server.عمر النسخة المخزنة 45 s. وبما أن 45 < 60، فهي ما زالت طازجة (fresh)، لذلك تجيب الذاكرة المؤقتة ولا يصل الطلب إلى الخادم أبدًا.Request at 75 sطلب عند 75 s
The copy is now 75 s old. Since 75 > 60, it is stale, so the request must go back to the server, which may send a new copy or confirm the old one is still valid.عمر النسخة الآن 75 s. وبما أن 75 > 60، فهي منتهية الصلاحية (stale)، لذلك يجب أن يعود الطلب إلى الخادم، الذي قد يرسل نسخة جديدة أو يؤكد أن النسخة القديمة ما زالت صالحة.Resultالنتيجة
Fresh while age is below max-age: 45 s is served from cache, 75 s goes to the server.النسخة طازجة ما دام عمرها أقل من max-age: الطلب عند 45 s يُخدم من الذاكرة المؤقتة، والطلب عند 75 s يذهب إلى الخادم.
Recall
A lamp response is generated at t = 0 s with Cache-Control: max-age=60. What happens to requests at 30 s, 59 s and 90 s?وُلّدت استجابة للمصباح عند t = 0 s ومعها Cache-Control: max-age=60. ماذا يحدث للطلبات عند 30 s و59 s و90 s؟
Recall
Why does statelessness make a REST API easier to scale?لماذا يجعل انعدام الحالة توسيع واجهة REST API أسهل؟
Quick checkاختبار سريع
Which request design breaks the statelessness constraint?أي تصميم للطلبات يكسر قيد انعدام الحالة؟
You want lamp 123 at 80 percent brightness. Under REST the app sends PATCH /lamps/123 with the header Content-Type: application/json and the body {"brightness": 80}. The response describes the lamp and includes links for what you can do next. That one exchange already demonstrates all four parts of the next constraint.
تريد أن يكون سطوع المصباح 123 عند 80 بالمئة. في REST يرسل التطبيق PATCH /lamps/123 مع الترويسة Content-Type: application/json والجسم {"brightness": 80}. وتصف الاستجابة المصباح وتتضمن روابط لما يمكنك فعله بعد ذلك. هذا التبادل الواحد يوضّح وحده الأجزاء الأربعة كلها للقيد التالي.
4. Uniform interface: one way to talk to every resource4. الواجهة الموحدة (Uniform interface): طريقة واحدة للتعامل مع كل مورد
The uniform interface means standardized interaction with resources. Every resource is handled with the same small set of moves, so a client that knows how to talk to lamps also knows how to talk to users or thermostats. It has four sub-constraints:
الواجهة الموحدة (uniform interface) تعني تفاعلًا موحدًا قياسيًا مع الموارد. يُتعامل مع كل مورد بالمجموعة الصغيرة نفسها من الحركات، فالعميل الذي يعرف كيف يتعامل مع المصابيح يعرف أيضًا كيف يتعامل مع المستخدمين أو منظمات الحرارة. ولها أربعة قيود فرعية:
- Resource identification through URIs. Each resource has a URI: /lamps/123 uniquely identifies lamp 123.تعريف الموارد عبر عناوين URI. لكل مورد (resource) عنوان URI: فالعنوان /lamps/123 يعرّف المصباح 123 تعريفًا فريدًا.
- Manipulation through representations. The client never touches the lamp object; it sends a representation of the desired state. PATCH /lamps/123 with {"brightness": 80} updates the brightness. PATCH applies a partial change, unlike PUT which replaces the whole resource.التعديل عبر التمثيلات. العميل لا يلمس كائن المصباح أبدًا، بل يرسل تمثيلًا (representation) للحالة المرغوبة. فالطلب PATCH /lamps/123 مع {"brightness": 80} يحدّث السطوع. والطريقة PATCH تطبّق تغييرًا جزئيًا، بخلاف PUT التي تستبدل المورد كاملًا.
- Self-descriptive messages. Each message carries enough to be understood alone. The method PATCH, the header Content-Type: application/json and the JSON body together describe the requested update; that is a self-descriptive message.رسائل تصف نفسها بنفسها. كل رسالة تحمل ما يكفي لفهمها وحدها. فالطريقة PATCH، والترويسة Content-Type: application/json، وجسم JSON، تصف معًا التحديث المطلوب؛ وهذه هي الرسالة ذاتية الوصف (self-descriptive message).
- Hypermedia as the engine of application state (HATEOAS). The lamp's response includes links or actions to retrieve, update or delete the lamp, so the client discovers what it can do next from the response instead of from hard-coded URLs.الوسائط التشعبية بوصفها محرّك حالة التطبيق (HATEOAS). تتضمن استجابة المصباح روابط أو إجراءات لجلب المصباح أو تحديثه أو حذفه، فيكتشف العميل ما يمكنه فعله بعد ذلك من الاستجابة نفسها بدلًا من عناوين URL مكتوبة مسبقًا في الشيفرة.
An illustrative HATEOAS response for lamp 123استجابة HATEOAS توضيحية للمصباح 123
- id
- 123123
- brightness
- 8080
- links.self
- GET /lamps/123GET /lamps/123
- links.update
- PATCH /lamps/123PATCH /lamps/123
- links.delete
- DELETE /lamps/123DELETE /lamps/123
Recall
Name the four sub-constraints of the uniform interface and give the lamp example of each.سمِّ القيود الفرعية الأربعة للواجهة الموحدة، وأعطِ مثال المصباح لكل منها.
5. Layered system: the client sees only the next hop5. النظام الطبقي (Layered system): العميل لا يرى إلا الطبقة التالية
A lamp request rarely goes straight to one server. It passes through an API gateway that checks the token and rate limits, then a load balancer that picks a healthy server, before reaching the smart-home servers. A layered system supports such intermediary layers, including proxies, gateways and load balancers. In Fielding's words, each component cannot see beyond the immediate layer it is interacting with.
نادرًا ما يذهب طلب المصباح مباشرة إلى خادم واحد. فهو يمر عبر بوابة API (API gateway) تتحقق من الرمز وتطبّق حدود معدّل الطلبات (rate limits)، ثم عبر موازن أحمال يختار خادمًا سليمًا، قبل أن يصل إلى خوادم المنزل الذكي. والنظام الطبقي (layered system) يدعم مثل هذه الطبقات الوسيطة، ومنها الخوادم الوسيطة (proxies) والبوابات وموازنات الأحمال. وبتعبير Fielding: لا يستطيع أي مكوّن أن يرى ما وراء الطبقة المباشرة التي يتفاعل معها.
Sends PATCH /lamps/123 to one address
يرسل PATCH /lamps/123 إلى عنوان واحد
Checks token, rate limits, routes
تتحقق من الرمز (token) وحدود المعدل (rate limits) وتوجّه الطلب
Picks a healthy server
يختار خادمًا سليمًا
Hold the lamp state
تحفظ حالة المصباح
This is where the constraints reinforce each other. Because requests are stateless, the load balancer can send each one anywhere. Because messages are self-descriptive and cacheable, a proxy in the middle can cache them without knowing anything about lamps. For the edge-to-cloud continuum this matters directly: a cache or gateway at the edge is just another layer the client never has to know about.
هنا تعزّز القيود بعضها بعضًا. فلأن الطلبات عديمة الحالة، يستطيع موازن الأحمال أن يرسل كل طلب إلى أي مكان. ولأن الرسائل ذاتية الوصف وقابلة للتخزين المؤقت، يستطيع خادم وسيط في المنتصف أن يخزنها مؤقتًا دون أن يعرف أي شيء عن المصابيح. وهذا يهم مباشرة في سلسلة الحافة إلى السحابة (edge-to-cloud continuum): فالذاكرة المؤقتة أو البوابة الموجودة على الحافة (edge) ما هي إلا طبقة أخرى لا يحتاج العميل أبدًا إلى معرفتها.
6. Code on demand: the optional one6. الشيفرة عند الطلب (Code on demand): القيد الاختياري
The server sends JavaScript to the browser to provide an interactive lamp-control interface. That is code on demand: servers may provide executable code to clients, extending what the client can do after it is deployed. It is the only optional constraint, because it reduces visibility: an intermediary cannot easily tell what downloaded code will do.
يرسل الخادم شيفرة JavaScript إلى المتصفح ليقدّم واجهة تفاعلية للتحكم في المصباح. هذه هي الشيفرة عند الطلب (code on demand): يجوز للخوادم أن تزوّد العملاء بشيفرة قابلة للتنفيذ، فتوسّع ما يستطيع العميل فعله بعد نشره. وهي القيد الاختياري الوحيد، لأنها تقلل الوضوح (visibility): إذ لا يستطيع الوسيط أن يعرف بسهولة ما ستفعله الشيفرة التي جرى تنزيلها.
Quick checkاختبار سريع
Which REST constraint is the optional one?أي قيود REST هو القيد الاختياري؟
Quick checkاختبار سريع
What does HATEOAS require of the lamp response?ماذا يتطلب HATEOAS من استجابة المصباح؟
The constraints say what a REST API must respect. The slides then switch to practice: what you actually write down when designing one. Take "dim lamp 123 to 80" one last time and list every decision you had to make.
تحدد القيود ما يجب أن تلتزم به واجهة REST API. ثم تنتقل الشرائح إلى التطبيق العملي: ما الذي تكتبه فعلًا عندما تصمم واجهة كهذه. خذ "خفّض سطوع المصباح 123 إلى 80" مرة أخيرة، واسرد كل قرار كان عليك اتخاذه.
The five design elements, filled in for dimming lamp 123عناصر التصميم الخمسة، مملوءة لعملية خفض سطوع المصباح 123
- 1. URL1. عنوان URL
- /lamps/123/lamps/123
- 2. HTTP request method2. طريقة طلب HTTP
- PATCHPATCH
- 3. HTTP response status code3. رمز حالة استجابة HTTP
- 200 OK200 OK
- 4. Header fields4. حقول الترويسة (header fields)
- Content-Type: application/json, Authorization: Bearer tokenContent-Type: application/json, Authorization: Bearer token
- 5. JSON message5. رسالة JSON
- {"brightness": 80}{"brightness": 80}
Those five rows are the REST design elements: URL, HTTP request method, HTTP response status code, header fields and JSON message. The next part uses exactly this list to design the full lamp API and a calculator API, so learn it now as a checklist.
هذه الصفوف الخمسة هي عناصر تصميم REST (REST design elements): URL، وطريقة طلب HTTP، ورمز حالة استجابة HTTP، وحقول الترويسة، ورسالة JSON. يستخدم الجزء التالي هذه القائمة بالضبط لتصميم واجهة المصباح كاملة وواجهة آلة حاسبة، لذا احفظها الآن كقائمة تحقق.
How the pieces map onto the constraintsكيف ترتبط هذه الأجزاء بالقيود
Every entity the system manages, whether a file, a user, a virtual machine or an IoT device, is treated as a resource, and each resource is uniquely identified by a URI such as /lamps/123. That is design element 1 and uniform-interface sub-constraint i.
كل كيان يديره النظام، سواء كان ملفًا أو مستخدمًا أو آلة افتراضية أو جهاز إنترنت أشياء (IoT)، يُعامل على أنه مورد (resource)، وكل مورد يُعرَّف تعريفًا فريدًا بعنوان URI مثل /lamps/123. هذا هو عنصر التصميم 1 والقيد الفرعي i للواجهة الموحدة.
A resource is represented in JSON, the format most REST APIs use. JSON represents resource data as key-value pairs, and those representations are exchanged in HTTP request and response bodies. That is design element 5 and sub-constraint ii. Its media type is application/json.
يُمثَّل المورد بصيغة JSON، وهي الصيغة التي تستخدمها معظم واجهات REST. تمثّل JSON بيانات المورد على شكل أزواج مفتاح وقيمة (key-value pairs)، وتُتبادل هذه التمثيلات داخل أجسام (bodies) طلبات HTTP واستجاباته. هذا هو عنصر التصميم 5 والقيد الفرعي ii. ونوع الوسائط الخاص بها هو application/json.
Additional information travels in header fields: an authentication token, the Content-Type of the body, and the response formats the client accepts (Accept). That is design element 4, and it is what makes messages self-descriptive.
تنتقل المعلومات الإضافية في حقول الترويسة (header fields): رمز المصادقة (authentication token)، وContent-Type الخاص بالجسم، وصيغ الاستجابة التي يقبلها العميل (Accept). هذا هو عنصر التصميم 4، وهو ما يجعل الرسائل ذاتية الوصف.
Finally, REST APIs follow stateless communication: each request contains the information the server needs to process it independently. That is why the token sits in a header of every request instead of in a server-side session.
وأخيرًا، تتبع واجهات REST تواصلًا عديم الحالة: كل طلب يحتوي على المعلومات التي يحتاجها الخادم لمعالجته باستقلال. ولهذا يوضع الرمز في ترويسة كل طلب بدلًا من حفظه في جلسة على جانب الخادم.
| Design elementعنصر التصميم | Lamp exampleمثال المصباح | Constraint it servesالقيد الذي يخدمه |
|---|---|---|
| URLعنوان URL | /lamps/123/lamps/123 | Uniform interface: identification by URIالواجهة الموحدة: التعريف عبر URI |
| HTTP request methodطريقة طلب HTTP | PATCHPATCH | Uniform interface: standard verbs, self-descriptiveالواجهة الموحدة: أفعال قياسية، ورسائل ذاتية الوصف |
| HTTP response status codeرمز حالة استجابة HTTP | 200 OK200 OK | Self-descriptive messagesالرسائل ذاتية الوصف (self-descriptive messages) |
| Header fieldsحقول الترويسة (header fields) | Authorization, Content-Type, AcceptAuthorization, Content-Type, Accept | Statelessness and self-descriptive messagesانعدام الحالة والرسائل ذاتية الوصف |
| JSON messageرسالة JSON | {"brightness": 80}{"brightness": 80} | Manipulation through representationsالتعديل عبر التمثيلات (manipulation through representations) |
Recall
Name the five design elements of a REST API, in the slide's order.سمِّ عناصر التصميم الخمسة لواجهة REST API بترتيب الشريحة.
Quick checkاختبار سريع
Which is not one of the five REST design elements?أي مما يلي ليس من عناصر تصميم REST الخمسة؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- REST (REpresentational State Transfer) was introduced by Roy Thomas Fielding in his doctoral dissertation in 2000.قدّم Roy Thomas Fielding أسلوب REST (اختصار REpresentational State Transfer، أي نقل الحالة التمثيلية) في أطروحة الدكتوراه الخاصة به عام 2000.
- REST is an architectural style, a set of constraints, not a protocol. HTTP is how it is usually realized.REST أسلوب معماري، أي مجموعة من القيود، وليس بروتوكولًا. وعادةً ما يُطبَّق عبر HTTP.
- Six constraints in order: client-server, stateless, cacheable, uniform interface, layered system, code on demand. Mnemonic: Can Students Cache Useful Lamp Code?القيود الستة بالترتيب: client-server، وstateless، وcacheable، وuniform interface، وlayered system، وcode on demand. وجملة الحفظ: Can Students Cache Useful Lamp Code?
- Five are mandatory; code on demand is the only optional one.خمسة منها إلزامية، والشيفرة عند الطلب (code on demand) هي القيد الاختياري الوحيد.
- Stateless: the server stores no client session data, and every request carries everything needed, such as the lamp ID and the auth token.انعدام الحالة (stateless): الخادم لا يخزّن أي بيانات جلسة (session) للعميل، وكل طلب يحمل كل ما يلزم لمعالجته، مثل معرّف المصباح (lamp ID) ورمز المصادقة (auth token).
- Statelessness lets any server answer any request, so you scale by adding servers and survive a server crash.انعدام الحالة يسمح لأي خادم بالرد على أي طلب، فتتوسع بإضافة خوادم، ويصمد النظام عند تعطل أحد الخوادم.
- Cache-Control: max-age=60 on a response made at t = 0 s: a request at 45 s is served from cache, one at 75 s goes back to the server.استجابة صدرت عند t = 0 s ومعها Cache-Control: max-age=60: الطلب عند 45 s يُخدم من الذاكرة المؤقتة، والطلب عند 75 s يعود إلى الخادم.
- Uniform interface: URIs identify resources, representations manipulate them, messages describe themselves, hypermedia drives the next step (HATEOAS).الواجهة الموحدة: عناوين URI تعرّف الموارد، والتمثيلات (representations) تُستخدم لتعديلها، والرسائل تصف نفسها بنفسها، والوسائط التشعبية (hypermedia) تقود الخطوة التالية (HATEOAS).
- Layered system: proxies, API gateways and load balancers can sit in the path while the client sees only the next hop.النظام الطبقي: يمكن أن تقع في مسار الطلب خوادم وسيطة (proxies) وبوابات API (API gateways) وموازنات أحمال (load balancers)، بينما لا يرى العميل إلا الطبقة التالية مباشرة.
- Five design elements: URL, HTTP request method, HTTP response status code, header fields, JSON message.عناصر التصميم الخمسة: URL، وطريقة طلب HTTP، ورمز حالة استجابة HTTP، وحقول الترويسة (header fields)، ورسالة JSON.
Sourcesالمصادر
- Chapter 5: Representational State Transfer (REST), in Architectural Styles and the Design of Network-based Software ArchitecturesPaperRoy T. Fielding, doctoral dissertation, University of California, Irvine (2000)Section 5.1 derives client-server, stateless, cache, uniform interface (four constraints), layered system and optional code on demandيشتق القسم 5.1 القيود: client-server، وstateless، وcache، وuniform interface (أربعة قيود فرعية)، وlayered system، والقيد الاختياري code on demand(opens in a new tab)(يفتح في تبويب جديد)
- Chapter 6: Experience and EvaluationPaperRoy T. Fielding, doctoral dissertation (2000)Section 6.3.4.2 on why cookies mismatch REST's model of application stateالقسم 6.3.4.2 عن سبب عدم توافق ملفات تعريف الارتباط (cookies) مع نموذج REST لحالة التطبيق(opens in a new tab)(يفتح في تبويب جديد)
- REST APIs must be hypertext-drivenArticleRoy T. Fielding (2008)Why HATEOAS is required for an API to be RESTfulلماذا يُعد HATEOAS شرطًا لكي تكون الواجهة البرمجية RESTful(opens in a new tab)(يفتح في تبويب جديد)
- RFC 9111: HTTP Caching (June 2022)RFCIETFSection 4.2 freshness, Section 5.2.2.1 max-ageالقسم 4.2 عن الصلاحية (freshness)، والقسم 5.2.2.1 عن max-age(opens in a new tab)(يفتح في تبويب جديد)
- Cache-Control headerDocsMDN Web Docs (Mozilla)max-age counts from generation at the origin; Age reduces remaining freshnessيُحسب max-age من لحظة التوليد في الخادم الأصلي، والترويسة Age تُنقص مدة الصلاحية المتبقية(opens in a new tab)(يفتح في تبويب جديد)
- RESTDocsMDN Web Docs (Mozilla)(opens in a new tab)(يفتح في تبويب جديد)
- RFC 5789: PATCH Method for HTTP (March 2010)RFCIETF(opens in a new tab)(يفتح في تبويب جديد)
- RFC 8259: The JSON Data Interchange Format (December 2017)RFCIETFObjects are name/value pairs; media type application/jsonالكائنات أزواج من الاسم والقيمة (name/value)، ونوع الوسائط application/json(opens in a new tab)(يفتح في تبويب جديد)