Majid Al-RaimiFull guideالشرح الكامل

COE 558Lecture 03المحاضرة 03Full guideالشرح الكامل

Application Programming Interfaces (APIs)واجهات برمجة التطبيقات (APIs)

The whole lecture on one page, taught concept by concept. Work through the parts in order, mark each concept once you understand it, and open the slide chips when you want the original slides.المحاضرة كاملة في صفحة واحدة، مشروحة مفهومًا مفهومًا. امشِ على الأجزاء بالترتيب، وعلّم كل مفهوم بعد أن تفهمه، وافتح أزرار الشرائح عندما تريد الرجوع إلى الشرائح الأصلية.

Partsالأجزاء
5
Conceptsالمفاهيم
19
Slidesالشرائح
27
Readingمدة القراءة
114 min114 دقيقة
Understoodما فهمته
0/19 conceptsمفاهيم

Part 01: APIs, endpoints and routesواجهات APIs ونقاط النهاية والمسارات

The API as the layer between components, the split between interface, protocol and connection, and how a server exposes resources through endpoints and routes.واجهة API بوصفها الطبقة الوسيطة بين المكونات، والفصل بين الواجهة (interface) والبروتوكول (protocol) والاتصال (connection)، وكيف يعرض الخادم موارده عبر نقاط النهاية (endpoints) والمسارات (routes).

4 concepts4 مفاهيم, ، slides 1-7الشرائح 1-7

Why this part mattersلماذا يهمّ هذا الجزء

Lecture 01 used a lamp you could switch on from a browser using PUT and read with GET. This lecture zooms into that arrow between client and server and gives every piece of it a precise name: the API, the protocol, the connection, the endpoint, the resource and the route. Those six words are the vocabulary of every cloud service you will design, and the midterm will ask you to tell them apart.

The part moves from the outside in. First the API as a contract between whole components, then the three layers that any call crosses, then what sits behind the server's door (an endpoint guarding a resource), and finally the exact key a client uses to open that door: a route, which is a method plus a URL.

استخدمت المحاضرة 01 مصباحًا يمكنك تشغيله من المتصفح باستخدام PUT وقراءة حالته باستخدام GET. هذه المحاضرة تقترب من ذلك السهم بين العميل والخادم وتعطي كل جزء منه اسمًا دقيقًا: الـ API، والبروتوكول (protocol)، والاتصال (connection)، ونقطة النهاية (endpoint)، والمورد (resource)، والمسار (route). هذه الكلمات الست هي مفردات كل خدمة سحابية ستصممها، وسيطلب منك الامتحان النصفي أن تميّز بينها.

يسير هذا الجزء من الخارج إلى الداخل. أولًا الـ API بوصفه عقدًا بين مكوّنات كاملة، ثم الطبقات الثلاث التي يعبرها أي استدعاء، ثم ما يقف خلف باب الخادم (نقطة نهاية تحرس موردًا)، وأخيرًا المفتاح الدقيق الذي يستخدمه العميل لفتح ذلك الباب: المسار (route)، وهو طريقة (method) مع عنوان URL.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Describe the API as the interface between components and state the two jobs of the API layer between frontend and backend.تصف الـ API بأنه الواجهة بين المكوّنات، وتذكر المهمتين اللتين تقوم بهما طبقة الـ API بين الواجهة الأمامية (frontend) والخلفية (backend).
  2. Distinguish API, protocol and connection in one line each, and place each one in the lamp example and the TCP/IP stack.تميّز بين الـ API والبروتوكول (protocol) والاتصال (connection) بسطر واحد لكل منها، وتضع كل واحد منها في مثال المصباح وفي مكدّس TCP/IP.
  3. Define an endpoint as an abstraction of a resource that supports CRUD and holds the logic to run it.تعرّف نقطة النهاية (endpoint) بأنها تجريد لمورد (resource) يدعم عمليات CRUD ويحمل المنطق اللازم لتنفيذها.
  4. Define a route as HTTP method plus URL, map routes to CRUD operations, and explain why the client sees the server as a set of APIs.تعرّف المسار (route) بأنه طريقة HTTP مع عنوان URL، وتربط المسارات بعمليات CRUD، وتشرح لماذا يرى العميل الخادم على أنه مجموعة من الـ APIs.

Open a weather app on your phone. The screen that draws today's temperature is the frontend. The database of sensor readings sitting in a data center is the backend. The phone never opens that database directly, and the database knows nothing about pixels. Something in between answers "give me today's readings for Dhahran" and, when you submit a correction, "store this new reading". That something is an Application Programming Interface (API).

افتح تطبيق الطقس على هاتفك. الشاشة التي ترسم درجة حرارة اليوم هي الواجهة الأمامية (frontend). وقاعدة بيانات قراءات المستشعرات الموجودة في مركز بيانات هي الخلفية (backend). الهاتف لا يفتح تلك القاعدة مباشرة أبدًا، والقاعدة لا تعرف شيئًا عن البكسلات. هناك شيء في المنتصف يجيب عن "أعطني قراءات اليوم للظهران"، وعندما ترسل تصحيحًا يجيب عن "خزّن هذه القراءة الجديدة". ذلك الشيء هو واجهة برمجة التطبيقات Application Programming Interface (API).

The lecture's definition is short: an API allows different software components to communicate with each other, and it acts as the interface between components. The word that matters is interface. MDN describes an API as a simple contract between the application offering it and the software that uses it: the provider promises which requests it will accept and what it will send back, and the caller promises to ask only in those terms. Neither side needs to see inside the other.

تعريف المحاضرة قصير: الـ API يسمح لمكوّنات برمجية مختلفة بأن تتواصل فيما بينها، وهو يعمل كواجهة بين المكوّنات. الكلمة المهمة هي الواجهة (interface). يصف موقع MDN الـ API بأنه عقد بسيط بين التطبيق الذي يقدّمه والبرنامج الذي يستخدمه: المزوّد يتعهد بالطلبات التي سيقبلها وبما سيعيده، والمستدعي يتعهد بألا يطلب إلا بتلك الشروط. ولا يحتاج أي طرف إلى رؤية ما بداخل الطرف الآخر.

The middle layer and its two jobsالطبقة الوسطى ومهمتاها

The lecture draws the application as three stacked boxes: frontend on top, backend at the bottom, and the API layer (the middle layer) between them. Every interaction between top and bottom goes through the middle, and it flows in one of two directions.

ترسم المحاضرة التطبيق على شكل ثلاثة صناديق متراصّة: الواجهة الأمامية في الأعلى، والخلفية في الأسفل، وAPI layer (الطبقة الوسطى) بينهما. كل تفاعل بين الأعلى والأسفل يمر عبر الوسط، ويسير في أحد اتجاهين.

  1. Read path: the API layer fetches data from the backend and hands it up to be displayed on the frontend. The weather app showing today's temperature is this path.مسار القراءة (read path): تجلب طبقة الـ API البيانات من الخلفية وتسلّمها إلى الأعلى لتُعرض على الواجهة الأمامية. تطبيق الطقس الذي يعرض حرارة اليوم هو هذا المسار.
  2. Write path: the API layer takes data from the frontend and inserts it into the backend. The correction you submit is this path.مسار الكتابة (write path): تأخذ طبقة الـ API البيانات من الواجهة الأمامية وتدخلها في الخلفية. التصحيح الذي ترسله هو هذا المسار.
Data rises from backend to frontend on the left, and sinks from frontend to backend on the right, always through the API layerتصعد البيانات من الخلفية إلى الواجهة الأمامية على اليسار، وتنزل من الواجهة الأمامية إلى الخلفية على اليمين، دائمًا عبر طبقة الـ API

In the visual, the left arrows are the read path (fetch, then display) and the right arrows are the write path (take, then insert). Every arrow starts or ends at the middle box; none jumps straight from frontend to backend. That is the whole point of a middle layer: if the backend swaps its database engine, or the frontend is rewritten from a web page into a mobile app, only one side of the contract changes and the other keeps working.

في الرسم، الأسهم اليسرى هي مسار القراءة (جلب ثم عرض) والأسهم اليمنى هي مسار الكتابة (أخذ ثم إدخال). كل سهم يبدأ أو ينتهي عند الصندوق الأوسط، ولا يقفز أي سهم مباشرة من الواجهة الأمامية إلى الخلفية. وهذا هو جوهر وجود طبقة وسطى: إذا غيّرت الخلفية محرك قاعدة البيانات، أو أُعيدت كتابة الواجهة الأمامية من صفحة ويب إلى تطبيق جوال، يتغير طرف واحد فقط من العقد ويستمر الطرف الآخر في العمل.

Connect this to Lecture 01. There, a relationship between two components was defined as a protocol plus an interface. The API is precisely the interface half: the set of operations one component offers the other. The next concept fills in the protocol half and adds a third piece underneath.

اربط هذا بالمحاضرة 01. هناك عُرّفت العلاقة بين مكوّنين بأنها بروتوكول زائد واجهة. والـ API هو بالضبط نصف الواجهة: مجموعة العمليات التي يقدّمها مكوّن للآخر. المفهوم التالي يملأ نصف البروتوكول ويضيف قطعة ثالثة تحته.

Recall

What are the two responsibilities of the API layer between frontend and backend?ما المسؤوليتان اللتان تقوم بهما طبقة الـ API بين الواجهة الأمامية والخلفية؟

Fetch data from the backend and display it on the frontend, and take data from the frontend and insert it into the backend.
جلب البيانات من الخلفية وعرضها على الواجهة الأمامية، وأخذ البيانات من الواجهة الأمامية وإدخالها في الخلفية.

Take the lamp from Lecture 01 and send it one request: GET /lamp/energy. Three separate things have to be true for that request to work, and each one answers a different question.

خذ المصباح من المحاضرة 01 وأرسل إليه طلبًا واحدًا: GET /lamp/energy. يجب أن تتحقق ثلاثة أشياء منفصلة لكي ينجح هذا الطلب، وكل واحد منها يجيب عن سؤال مختلف.

  1. The server must offer an operation called "read energy" at /lamp/energy. If it does not, nothing else matters. That is the question of what you can call.يجب أن يقدّم الخادم عملية اسمها "قراءة الطاقة" (read energy) عند /lamp/energy. إن لم يفعل، فلا شيء آخر يهم. هذا هو سؤال ماذا يمكنك أن تستدعي.
  2. The client must phrase the request in a format the server understands: a method (GET), a target path, a version, headers, and later a status code in the reply. That is the question of how you call it.يجب أن يصوغ العميل الطلب بصيغة يفهمها الخادم: طريقة (GET)، ومسار هدف (target path)، ونسخة (version)، وترويسات (headers)، ثم لاحقًا رمز حالة (status code) في الرد. هذا هو سؤال كيف تستدعيه.
  3. The bytes of that request must actually reach the server: through a TCP connection between two sockets, IP packets across routers, and frames over Wi-Fi or Ethernet. That is the question of how the data physically travels.يجب أن تصل بايتات ذلك الطلب فعلًا إلى الخادم: عبر اتصال TCP بين مقبسين (sockets)، وحزم IP عبر الموجّهات (routers)، وإطارات (frames) عبر Wi-Fi أو Ethernet. هذا هو سؤال كيف تنتقل البيانات فعليًا.

Slide 3 compresses this into the single most quotable line of the lecture. Learn it word for word.

تختصر الشريحة 3 هذا كله في أكثر سطر قابل للاقتباس في المحاضرة. احفظه حرفيًا.

API=what you can callProtocol=how you call itConnection=how the data physically travels\begin{aligned}\textbf{API} &= \text{what you can call}\\ \textbf{Protocol} &= \text{how you call it}\\ \textbf{Connection} &= \text{how the data physically travels}\end{aligned}
The three-way split on slide 3التقسيم الثلاثي في الشريحة 3

The slide's picture says the same thing for two components that each carry a Web API port. The API is the interface between them: a Web API is the interface, and HTTP is the protocol used to implement the communication. The line underneath is the connection, the physical path the data takes.

صورة الشريحة تقول الشيء نفسه لمكوّنين يحمل كل منهما منفذ Web API. الـ API هو الواجهة بينهما: Web API هو الواجهة، وHTTP هو protocol (البروتوكول) المستخدم لتنفيذ التواصل. والخط الذي تحتهما هو connection (الاتصال)، أي المسار المادي الذي تسلكه البيانات.

Three stacked layers light in turn: what you can call, how you call it, how the data travelsتضيء ثلاث طبقات متراصّة بالتتابع: ما يمكنك استدعاؤه، وكيف تستدعيه، وكيف تنتقل البيانات

Read the visual from the top. The three small squares in the top band are the operations the API offers (the Web API ports on the slide). The middle band is the HTTP request that names one of them. The bottom band is the wire that carries the bytes from one end to the other.

اقرأ الرسم من الأعلى. المربعات الثلاثة الصغيرة في الشريط العلوي هي العمليات التي يقدّمها الـ API (منافذ Web API في الشريحة). الشريط الأوسط هو طلب HTTP الذي يسمّي واحدة منها. والشريط السفلي هو السلك الذي يحمل البايتات من طرف إلى الطرف الآخر.

Where each layer lives in the TCP/IP stackأين تقع كل طبقة في مكدّس TCP/IP

This is not new material wearing a new name; it is the stack from Lecture 01 seen from the application's point of view. HTTP is an application-layer protocol. Everything under it (TCP at the transport layer, IP at the network layer, then the link and physical layers) is what the lecture now calls the connection. The socket you met in Lecture 01 is the door between the two: the program writes its HTTP message into a socket, and from there the connection takes over. RFC 9110 states the same split from the protocol side: HTTP operates over a reliable transport or session-layer connection, and HTTP itself is independent of the transport protocol.

هذه ليست مادة جديدة باسم جديد؛ إنها المكدّس من المحاضرة 01 منظورًا إليه من جهة التطبيق. HTTP بروتوكول في طبقة التطبيق (application layer). وكل ما تحته (TCP في طبقة النقل، وIP في طبقة الشبكة، ثم طبقتا الربط والطبقة المادية) هو ما تسميه المحاضرة الآن الاتصال (connection). والمقبس (socket) الذي رأيته في المحاضرة 01 هو الباب بين الاثنين: يكتب البرنامج رسالة HTTP في المقبس، ومن هناك يتولى الاتصال الأمر. ويذكر RFC 9110 التقسيم نفسه من جهة البروتوكول: يعمل HTTP فوق اتصال نقل موثوق أو اتصال في طبقة الجلسة (session layer)، وHTTP نفسه مستقل عن بروتوكول النقل.

LayerالطبقةQuestion it answersالسؤال الذي تجيب عنهLamp exampleمثال المصباحWhere you met itأين قابلتها
API (interface)الـ API (الواجهة)What can I call?ماذا يمكنني أن أستدعي؟read energy at /lamp/energy, set state at /lamp/stateقراءة الطاقة عند /lamp/energy، وضبط الحالة عند /lamp/stateLecture 01, the interface half of a relationshipالمحاضرة 01، نصف الواجهة في العلاقة بين مكوّنين
Protocolالبروتوكول (protocol)How do I call it?كيف أستدعيه؟HTTP: GET and PUT requests with status codesHTTP: طلبات GET وPUT مع رموز الحالة (status codes)Lecture 01, the protocol half; application layerالمحاضرة 01، نصف البروتوكول؛ طبقة التطبيق (application layer)
Connectionالاتصال (connection)How does it travel?كيف تنتقل البيانات؟TCP between two sockets, over IP, over Wi-FiTCP بين مقبسين (sockets)، فوق IP، فوق Wi-FiLecture 01, transport layer and belowالمحاضرة 01، طبقة النقل (transport layer) وما تحتها
The three layers for the lampالطبقات الثلاث في مثال المصباح

Why three layers and not one: the swap testلماذا ثلاث طبقات وليست واحدة: اختبار الاستبدال

The best evidence that these are three different things is that each can change while the others stay put.

أقوى دليل على أن هذه ثلاثة أشياء مختلفة هو أن كل واحد منها يمكن أن يتغير بينما يبقى الآخران كما هما.

  • Change the API only. Add a brightness operation at /lamp/brightness. The protocol is still HTTP and the wire is still TCP over Wi-Fi.غيّر الـ API فقط. أضف عملية للسطوع عند /lamp/brightness. يبقى البروتوكول HTTP ويبقى السلك TCP عبر Wi-Fi.
  • Change the connection only. Move the lamp from Wi-Fi to Ethernet. The same HTTP requests reach the same operations. HTTP/3 goes further and runs over QUIC instead of TCP; RFC 9114 describes it as supporting the same semantics over a new transport protocol, so the API the client sees does not change.غيّر الاتصال فقط. انقل المصباح من Wi-Fi إلى Ethernet. تصل طلبات HTTP نفسها إلى العمليات نفسها. ويذهب HTTP/3 أبعد من ذلك فيعمل فوق QUIC بدلًا من TCP؛ ويصفه RFC 9114 بأنه يدعم الدلالات (semantics) نفسها فوق بروتوكول نقل جديد، لذلك لا يتغير الـ API الذي يراه العميل.
  • Reuse the protocol. The same HTTP carries millions of unrelated APIs, from lamps to banks.أعد استخدام البروتوكول. بروتوكول HTTP نفسه يحمل ملايين الـ APIs التي لا علاقة بينها، من المصابيح إلى البنوك.

Recall

Complete the three lines: API is ..., protocol is ..., connection is ...أكمل الأسطر الثلاثة: الـ API هو ...، والبروتوكول هو ...، والاتصال هو ...

API = what you can call. Protocol = how you call it (HTTP for Web APIs). Connection = how the data physically travels (TCP/IP under HTTP).
الـ API = ما يمكنك استدعاؤه. البروتوكول = كيف تستدعيه (HTTP في حالة Web APIs). الاتصال = كيف تنتقل البيانات فعليًا (TCP/IP تحت HTTP).

Quick checkاختبار سريع

The lamp server adds GET /lamp/brightness and nothing else changes. Which layer changed?أضاف خادم المصباح GET /lamp/brightness ولم يتغير أي شيء آخر. أي طبقة تغيرت؟

Picture a smart-home server that manages lamps, a thermostat and user accounts. A phone app, the HTTP client, sends it many requests: one about a lamp, one about the temperature, one about the logged-in user. The HTTP server does not answer them all from one undifferentiated blob of code. It has several doors, and each request is answered at the door responsible for it.

تخيّل خادم منزل ذكي يدير مصابيح وجهاز تحكم في الحرارة (thermostat) وحسابات مستخدمين. تطبيق الهاتف، أي HTTP client، يرسل إليه طلبات كثيرة: واحد عن مصباح، وواحد عن درجة الحرارة، وواحد عن المستخدم المسجّل دخوله. لا يجيب HTTP server عنها كلها من كتلة واحدة غير متمايزة من الكود. بل له عدة أبواب، ويُجاب عن كل طلب عند الباب المسؤول عنه.

Slide 4 names those doors: the API on the server is also referred to as an endpoint. The client sends multiple requests, the server has multiple endpoints (drawn as boxes 1, 2 and 3 on its edge), and the endpoint is where the API processes the request and returns data. So an endpoint is not a network address and not a machine; it is a point on the server's boundary where requests arrive and responses leave.

تسمّي الشريحة 4 تلك الأبواب: يُشار إلى الـ API على الخادم أيضًا باسم endpoint (نقطة النهاية). يرسل العميل طلبات متعددة، وللخادم نقاط نهاية متعددة (مرسومة كصناديق 1 و2 و3 على حافته)، ونقطة النهاية هي المكان الذي يعالج فيه الـ API الطلب ويعيد البيانات. إذن نقطة النهاية ليست عنوان شبكة وليست جهازًا؛ إنها نقطة على حدود الخادم تصل إليها الطلبات وتخرج منها الاستجابات.

What an endpoint stands forما الذي تمثّله نقطة النهاية

Slide 5 asks the deeper question: what does an endpoint represent? Its answer has three parts, and each is worth a mark.

تطرح الشريحة 5 السؤال الأعمق: ماذا تمثّل نقطة النهاية؟ لجوابها ثلاثة أجزاء، وكل جزء يستحق درجة.

  1. An endpoint is an abstraction of a resource. The client interacts with an endpoint that manages a resource. A resource is any entity the server manages: a file, a user, a virtual machine, an IoT lamp. RFC 9110 is explicit that HTTP does not limit the nature of a resource; it only defines an interface for interacting with one.نقطة النهاية تجريد (abstraction) لمورد. يتعامل العميل مع نقطة نهاية تدير resource (موردًا). المورد هو أي كيان يديره الخادم: ملف، أو مستخدم، أو آلة افتراضية، أو مصباح IoT. ويصرّح RFC 9110 بأن HTTP لا يقيّد طبيعة المورد؛ بل يعرّف فقط واجهة للتعامل معه.
  2. An endpoint supports four basic operations on that resource, known as CRUD: Create, Read, Update, Delete.تدعم نقطة النهاية أربع عمليات أساسية على ذلك المورد، تُعرف باسم CRUD: Create (إنشاء)، وRead (قراءة)، وUpdate (تحديث)، وDelete (حذف).
  3. An endpoint provides the logic needed to execute those four operations on the resource: validating input, checking the current state, talking to the database or the device.توفر نقطة النهاية المنطق اللازم لتنفيذ هذه العمليات الأربع على المورد: التحقق من المدخلات، وفحص الحالة الحالية، والتخاطب مع قاعدة البيانات أو الجهاز.

"Abstraction" is the key word. Lamp 7 is, in reality, a row in a database and a relay on a GPIO pin. The client sees neither. It sees a lamp it can create, read, update and delete, and the endpoint translates those four verbs into whatever the real resource needs. The slide's summary line follows: endpoints act as access points for retrieving or modifying resources.

"التجريد" (abstraction) هو الكلمة المفتاحية. المصباح 7 في الواقع صف في قاعدة بيانات ومرحّل (relay) على منفذ GPIO. والعميل لا يرى أيًّا منهما. بل يرى مصباحًا يمكنه إنشاؤه وقراءته وتحديثه وحذفه، ونقطة النهاية تترجم هذه الأفعال الأربعة إلى ما يحتاجه المورد الحقيقي. ومن هنا يأتي سطر الخلاصة في الشريحة: تعمل نقاط النهاية كنقاط وصول لاسترجاع الموارد أو تعديلها.

HTTP clientعميل HTTP

Phone app or browser sending requests

تطبيق هاتف أو متصفح يرسل الطلبات

request, responseطلب، استجابة
Endpointنقطة النهاية
inside HTTP serverداخل خادم HTTP

Abstraction of the resource; holds the CRUD logic

تجريد للمورد، وتحمل منطق CRUD

create, read, update, deleteإنشاء، قراءة، تحديث، حذف
Resourceالمورد
inside HTTP serverداخل خادم HTTP

The lamp, user, file or VM itself

المصباح أو المستخدم أو الملف أو الآلة الافتراضية (VM) نفسه

The client talks only to the endpoint; the endpoint performs CRUD on the resource, all inside the HTTP serverيتحدث العميل مع نقطة النهاية فقط؛ ونقطة النهاية تنفذ عمليات CRUD على المورد، وكل ذلك داخل خادم HTTP

Reading the arrows as data flowقراءة الأسهم على أنها تدفق للبيانات

Look at the direction of the four arrows on slide 5. Create, Update and Delete point from the endpoint to the resource; Read points from the resource back to the endpoint. The slide is drawing which way the data moves. Creating, updating and deleting push a change into the resource. Reading pulls the resource's state out toward the client.

انظر إلى اتجاه الأسهم الأربعة في الشريحة 5. أسهم Create وUpdate وDelete تتجه من نقطة النهاية إلى المورد؛ وسهم Read يتجه من المورد عائدًا إلى نقطة النهاية. الشريحة ترسم الاتجاه الذي تتحرك فيه البيانات. الإنشاء والتحديث والحذف تدفع تغييرًا إلى داخل المورد. أما القراءة فتسحب حالة المورد إلى الخارج نحو العميل.

Create, Update and Delete flow into the resource; Read flows out of itتتدفق Create وUpdate وDelete إلى داخل المورد؛ وتتدفق Read خارجة منه

In the visual, three arrows draw toward the resource and one draws back. Notice that the client never touches the resource box; its only line ends at the endpoint.

في الرسم، ثلاثة أسهم تُرسم نحو المورد وسهم واحد يُرسم عائدًا. لاحظ أن العميل لا يلمس صندوق المورد أبدًا؛ فخطه الوحيد ينتهي عند نقطة النهاية.

Recall

Name the four operations an endpoint supports, and say what else the endpoint provides.سمِّ العمليات الأربع التي تدعمها نقطة النهاية، واذكر ما الذي توفره نقطة النهاية أيضًا.

Create, Read, Update, Delete (CRUD). It also provides the logic necessary to execute those operations on the resource.
Create وRead وUpdate وDelete (CRUD). وتوفر أيضًا المنطق اللازم لتنفيذ هذه العمليات على المورد.

Quick checkاختبار سريع

According to the lecture, what does an endpoint represent?بحسب المحاضرة، ماذا تمثّل نقطة النهاية (endpoint)؟

A client wants to switch lamp 7 off, not read it and not delete it. The endpoint for lamp 7 supports all four CRUD operations, so the request must say which one. It does that with two pieces of information together: the URL that leads to the endpoint, and the HTTP method that picks the operation.

يريد عميل أن يطفئ المصباح 7، لا أن يقرأه ولا أن يحذفه. نقطة النهاية الخاصة بالمصباح 7 تدعم عمليات CRUD الأربع كلها، لذلك يجب أن يحدد الطلب أيّها يريد. ويفعل ذلك بمعلومتين معًا: عنوان URL الذي يقود إلى نقطة النهاية، وطريقة HTTP التي تختار العملية.

  • GET /lamps/7 reads lamp 7.GET /lamps/7 يقرأ المصباح 7.
  • PUT /lamps/7 updates lamp 7, for example to switch it off.PUT /lamps/7 يحدّث المصباح 7، مثلًا لإطفائه.
  • DELETE /lamps/7 deletes (or disables) lamp 7.DELETE /lamps/7 يحذف المصباح 7 (أو يعطّله).
  • POST /lamps creates a new lamp. Its URL differs because the new lamp has no number yet; Part 3 of this lecture explains why creation goes to the collection.POST /lamps ينشئ مصباحًا جديدًا. عنوانه مختلف لأن المصباح الجديد ليس له رقم بعد؛ ويشرح الجزء 3 من هذه المحاضرة لماذا يذهب الإنشاء إلى المجموعة (collection).

Each of those pairs is a route. Slide 6 states the rule: a URL leads to an endpoint, which defines routes to manage different actions, and a route is an HTTP method plus a URL. A resource can therefore be exposed through multiple API operations, typically distinguished by HTTP methods and URL paths.

كل زوج من هذه الأزواج هو route (مسار). تذكر الشريحة 6 القاعدة: عنوان URL يقود إلى نقطة نهاية، ونقطة النهاية تعرّف مسارات لإدارة أفعال مختلفة، والمسار هو HTTP method زائد عنوان URL. لذلك يمكن عرض المورد الواحد عبر عدة عمليات API، تُميَّز عادة بطرق HTTP ومسارات URL.

Route=HTTP method+URL\text{Route} = \text{HTTP method} + \text{URL}
Slide 6الشريحة 6
Each method and URL pair lights its own CRUD operationكل زوج من طريقة وعنوان URL يضيء عملية CRUD الخاصة به

The visual shows why both halves are needed. Three of the four routes share the URL /lamps/7, so the URL alone cannot tell Read from Update from Delete. And GET appears on many endpoints across a server, so the method alone cannot tell lamp 7 from the thermostat. Only the pair is unique. That is exactly how server frameworks dispatch requests: Express, for instance, runs a handler only when both the path and the HTTP method match.

يبيّن الرسم لماذا نحتاج إلى النصفين معًا. ثلاثة من المسارات الأربعة تشترك في العنوان /lamps/7، لذلك لا يستطيع العنوان وحده أن يميّز Read من Update من Delete. وGET تظهر على نقاط نهاية كثيرة في الخادم، لذلك لا تستطيع الطريقة وحدها أن تميّز المصباح 7 من جهاز التحكم في الحرارة. الزوج وحده هو الفريد. وهكذا بالضبط توزّع أطر عمل الخوادم الطلبات: Express مثلًا لا يشغّل المعالج (handler) إلا عندما يتطابق المسار وطريقة HTTP كلاهما.

The bottom box of slide 6 ties the three roles together, and it is a ready-made exam answer:

الصندوق السفلي في الشريحة 6 يربط الأدوار الثلاثة معًا، وهو إجابة جاهزة للامتحان:

  • The server hosts the endpoint.الخادم يستضيف نقطة النهاية.
  • The client communicates with the endpoint via specific routes.العميل يتواصل مع نقطة النهاية عبر مسارات (routes) محددة.
  • The routes correspond to CRUD operations on the endpoint.المسارات تقابل عمليات CRUD على نقطة النهاية.
TermالمصطلحWhat it isما هوLamp exampleمثال المصباح
Resourceالمورد (resource)The thing being managedالشيء الذي تتم إدارتهLamp 7 itselfالمصباح 7 نفسه
Endpointنقطة النهاية (endpoint)The server-side abstraction a URL leads to; holds the CRUD logicالتجريد على جهة الخادم الذي يقود إليه عنوان URL؛ ويحمل منطق CRUD/lamps/7/lamps/7
Routeالمسار (route)HTTP method + URL; selects one operationطريقة HTTP + عنوان URL؛ يختار عملية واحدةPUT /lamps/7PUT /lamps/7
Endpoint versus route versus resourceنقطة النهاية مقابل المسار مقابل المورد

From the client's side: the server is a set of APIsمن جهة العميل: الخادم مجموعة من الـ APIs

Now stand where the client stands. It cannot see the server's language, its database or its hardware. It sends a request and gets a response, and the only thing it knows about the server is the list of routes it may call. Slide 7 puts it plainly: for the client, the HTTP server is a set of APIs. Each numbered box on the server's edge is one API the client can address, and the same request and response pattern repeats at every one.

الآن قف حيث يقف العميل. إنه لا يرى لغة الخادم ولا قاعدة بياناته ولا عتاده. يرسل طلبًا ويتلقى استجابة، والشيء الوحيد الذي يعرفه عن الخادم هو قائمة المسارات التي يجوز له استدعاؤها. تقولها الشريحة 7 بوضوح: بالنسبة إلى العميل، خادم HTTP مجموعة من الـ APIs. كل صندوق مرقّم على حافة الخادم هو API واحد يستطيع العميل مخاطبته، ونمط الطلب والاستجابة نفسه يتكرر عند كل واحد منها.

This is the client-server idea from Lecture 01 with the interior hidden. It is also what makes the rest of the course possible: if the client only knows the set of APIs, the server behind them can be moved from a board next to the lamp to an edge node or a cloud region without the client noticing.

هذه فكرة العميل والخادم (client-server) من المحاضرة 01 مع إخفاء ما بالداخل. وهي أيضًا ما يجعل بقية المقرر ممكنًا: إذا كان العميل لا يعرف إلا مجموعة الـ APIs، فيمكن نقل الخادم الذي خلفها من لوحة بجانب المصباح إلى عقدة طرفية (edge node) أو إلى منطقة سحابية (cloud region) دون أن يلاحظ العميل.

Recall

What is a route, and how does it relate to an endpoint and CRUD?ما المسار، وما علاقته بنقطة النهاية وبعمليات CRUD؟

A route is HTTP method + URL. The URL leads to an endpoint, the endpoint defines routes, and each route corresponds to one CRUD operation on that endpoint.
المسار هو طريقة HTTP + عنوان URL. عنوان URL يقود إلى نقطة نهاية، ونقطة النهاية تعرّف المسارات، وكل مسار يقابل عملية CRUD واحدة على تلك النقطة.

Recall

How does the client see the HTTP server, according to slide 7?كيف يرى العميل خادم HTTP بحسب الشريحة 7؟

As a set of APIs: it sends a request to one of them and receives a response, without seeing anything behind them.
على أنه مجموعة من الـ APIs: يرسل طلبًا إلى واحد منها ويتلقى استجابة، دون أن يرى أي شيء خلفها.

Quick checkاختبار سريع

Which pair uniquely identifies a route on the server?أي زوج يحدد المسار (route) على الخادم تحديدًا فريدًا؟

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • An API lets software components communicate; it acts as the interface between components.يتيح الـ API لمكوّنات البرمجيات أن تتواصل فيما بينها، فهو يعمل كواجهة بين المكوّنات.
  • The API layer fetches data from the backend to display on the frontend, and takes data from the frontend to insert into the backend.تجلب طبقة الـ API البيانات من الخلفية (backend) لعرضها على الواجهة الأمامية (frontend)، وتأخذ البيانات من الواجهة الأمامية لإدخالها في الخلفية.
  • API = what you can call. Protocol (HTTP) = how you call it. Connection = how the data physically travels.الـ API = ما يمكنك استدعاؤه. البروتوكول (HTTP) = كيف تستدعيه. الاتصال (connection) = كيف تنتقل البيانات فعليًا.
  • The connection is the TCP/IP stack from Lecture 01 under HTTP: a TCP connection between two sockets, carried by IP and the link.الاتصال هو مكدّس TCP/IP من المحاضرة 01 تحت HTTP: اتصال TCP بين مقبسين (sockets)، تحمله طبقة IP وطبقة الربط (link).
  • The API on the server is also called an endpoint; an endpoint is an abstraction of a resource.يُسمّى الـ API على الخادم أيضًا نقطة النهاية (endpoint)، ونقطة النهاية تجريد لمورد (resource).
  • An endpoint supports the four CRUD operations (Create, Read, Update, Delete) and provides the logic to execute them.تدعم نقطة النهاية عمليات CRUD الأربع (Create إنشاء، Read قراءة، Update تحديث، Delete حذف) وتوفر المنطق اللازم لتنفيذها.
  • A URL leads to an endpoint, which defines routes; a route = HTTP method + URL, and routes correspond to CRUD operations.يقود عنوان URL إلى نقطة نهاية تعرّف المسارات (routes)؛ والمسار = طريقة HTTP + عنوان URL، والمسارات تقابل عمليات CRUD.
  • For the client, the HTTP server is a set of APIs: it sends a request to one of them and gets a response.بالنسبة إلى العميل، خادم HTTP مجموعة من الـ APIs: يرسل طلبًا إلى واحد منها ويتلقى استجابة.

Sourcesالمصادر

Part 02: HTTP methods and message anatomyطرق HTTP وتشريح الرسائل

How the four CRUD operations map onto GET, POST, PUT and DELETE, and how to read every field of an HTTP request and response using the index.html example.كيف تُربط عمليات CRUD الأربع بالطرق GET وPOST وPUT وDELETE، وكيف تقرأ كل حقل في طلب HTTP واستجابته باستخدام مثال index.html.

3 concepts3 مفاهيم, ، slides 8-12الشرائح 8-12

Why this part mattersلماذا يهمّ هذا الجزء

Part 01 said a client talks to an endpoint through an API. This part opens the envelope and shows exactly what travels: which verb the client picks, and what the request and the response look like as text on the wire.

ذكر الجزء 01 أن العميل يتواصل مع endpoint عبر API. هذا الجزء يفتح الظرف ويُريك بالضبط ما الذي ينتقل: أي فعل (verb) يختاره العميل، وكيف يبدو الطلب والاستجابة كنصٍّ على السلك.

Everything later in the lecture, designing endpoints for the lamp API and the REST constraints, is written in this vocabulary. On the midterm you should be able to look at a raw message and name each piece without hesitating, and to map Create, Read, Update and Delete to their methods in your sleep.

كل ما يأتي لاحقًا في المحاضرة، من تصميم الـ endpoints لـ API المصباح إلى قيود REST، مكتوب بهذه المفردات. في الامتحان النصفي يجب أن تستطيع النظر إلى رسالة خام وتسمية كل جزء منها دون تردد، وأن تربط Create وRead وUpdate وDelete بطرقها حتى وأنت نائم.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Map each CRUD operation to its HTTP method from memory, in both slide orders.اربط كل عملية من عمليات CRUD بطريقة HTTP المقابلة لها من الذاكرة، بترتيبي الشريحتين كليهما.
  2. Say which methods are safe and which are idempotent, and why POST is neither.حدد أي الطرق آمنة (safe) وأيها متساوية الأثر (idempotent)، ولماذا لا تكون POST أيًّا منهما.
  3. Label every part of an HTTP request: method, request target, version, header fields, empty line, body.سمِّ كل جزء من طلب HTTP: الطريقة (method)، وهدف الطلب (request target)، والإصدار، وحقول الترويسة (header fields)، والسطر الفارغ، والجسم (body).
  4. Label every part of an HTTP response, and read a status code by its class.سمِّ كل جزء من استجابة HTTP، واقرأ رمز الحالة (status code) من خلال فئته.
  5. Compute a Content-Length from a body and explain what the empty line does.احسب قيمة Content-Length من جسم الرسالة، واشرح وظيفة السطر الفارغ.

Think of a smart lamp exposed through an API. You might add a new lamp to the system, ask for its current state, set it to a new state, or retire it. Those four actions are all an application ever does to stored data, and HTTP has one verb for each.

تخيّل مصباحًا ذكيًا متاحًا عبر API. قد تضيف مصباحًا جديدًا إلى النظام، أو تسأل عن حالته الحالية، أو تضبطه على حالة جديدة، أو تُخرجه من الخدمة. هذه الأفعال الأربعة هي كل ما يفعله أي تطبيق بالبيانات المخزنة، ولـ HTTP فعل واحد لكل منها.

The table to memorizeالجدول الذي يجب حفظه

The four actions are the CRUD operations: Create, Read, Update, Delete. Each request names its operation with an HTTP method, the verb at the very start of the message. The lecture maps them like this: POST creates a new Resource, GET returns one, PUT updates or replaces one, and DELETE removes one.

الأفعال الأربعة هي عمليات CRUD: الإنشاء (Create)، والقراءة (Read)، والتحديث (Update)، والحذف (Delete). كل طلب يسمّي عمليته بـ HTTP method، وهو الفعل الموجود في بداية الرسالة تمامًا. وتربطها المحاضرة هكذا: POST ينشئ resource جديدًا، وGET يُرجع موردًا، وPUT يحدّث موردًا أو يستبدله، وDELETE يحذفه.

Methods on the left in slide 8's order, operations on the right in CRUD order. The wires cross because GET and POST swap places.الطرق على اليسار بترتيب الشريحة 8، والعمليات على اليمين بترتيب CRUD. تتقاطع الأسلاك لأن GET وPOST يتبادلان مكانيهما.

Slide 8 lists GET first. Slide 9 lists the same four in CRUD order, so POST comes first. The mapping is identical; only the row order changed. If an exam table is shuffled, anchor on the pair people swap: you POST a new item (Create) and GET an existing one (Read). DELETE is Delete, and PUT is the one left, Update.

الشريحة 8 تضع GET أولًا. والشريحة 9 تسرد الطرق الأربع نفسها بترتيب CRUD، فتأتي POST أولًا. الربط متطابق؛ لم يتغير إلا ترتيب الصفوف. إذا جاء جدول في الامتحان مخلوط الترتيب، فاعتمد على الزوج الذي يخلط الناس بينه: ترسل عنصرًا جديدًا بـ POST (إنشاء)، وتجلب عنصرًا موجودًا بـ GET (قراءة). DELETE هي الحذف، وPUT هي المتبقية، أي التحديث.

MethodالطريقةCRUDCRUDActionالإجراءSafeآمنة (Safe)Idempotentمتساوية الأثر (Idempotent)
POSTPOSTCreateإنشاء (Create)Create a new resourceإنشاء مورد جديدNoلاNoلا
GETGETReadقراءة (Read)Return a resourceإرجاع موردYesنعمYesنعم
PUTPUTUpdate or Replaceتحديث أو استبدال (Update or Replace)Replace the resource with the state you sendاستبدال المورد بالحالة التي ترسلهاNoلاYesنعم
DELETEDELETEDeleteحذف (Delete)Remove the resourceإزالة الموردNoلاYesنعم
PATCHPATCHUpdate (partial)تحديث جزئي (Update, partial)Change only the fields you send (slide 19)تغيير الحقول التي ترسلها فقط (الشريحة 19)NoلاNot guaranteedغير مضمون
HTTP methods, CRUD operations, and two properties beyond the slidesطرق HTTP، وعمليات CRUD، وخاصيتان خارج الشرائح

PUT replaces, PATCH editsPUT يستبدل، وPATCH يعدّل

The slides write PUT as "Update / Replace", and the word replace is the precise one. A PUT sends the full new state of the resource at a URL the client already knows, and the server stores exactly that. If you PUT a lamp with only a brightness field, a strict server would forget the lamp's colour. For changing one field there is PATCH, defined in RFC 5789, which sends only a set of changes. PATCH does not appear in the tables on slides 8 and 9, but it appears on slide 19, so meet it now.

تكتب الشرائح PUT على أنها "Update / Replace"، وكلمة الاستبدال (replace) هي الأدق. طلب PUT يرسل الحالة الجديدة الكاملة للمورد إلى URL يعرفه العميل مسبقًا، والخادم يخزّن ذلك بالضبط. إذا أرسلت PUT لمصباح بحقل السطوع (brightness) فقط، فإن الخادم الصارم سينسى لون المصباح. لتغيير حقل واحد توجد PATCH، المعرّفة في RFC 5789، وهي ترسل مجموعة من التغييرات فقط. لا تظهر PATCH في جدولي الشريحتين 8 و9، لكنها تظهر في الشريحة 19، لذا تعرّف عليها الآن.

Beyond the slides: safe and idempotentخارج الشرائح: الآمن (safe) ومتساوي الأثر (idempotent)

A method is safe when it does not ask the server to change anything. GET is safe: reading a lamp's state never turns it on. That is why browsers, caches and crawlers feel free to send GET requests on their own.

تكون الطريقة آمنة (safe) عندما لا تطلب من الخادم تغيير أي شيء. GET آمنة: قراءة حالة المصباح لا تشغّله أبدًا. ولهذا تشعر المتصفحات والـ caches وبرامج الزحف (crawlers) بحرية إرسال طلبات GET من تلقاء نفسها.

A method is Idempotent method when sending the same request twice leaves the server in the same state as sending it once. Picture a flaky mobile connection: the app sends a request, the reply is lost, and the app retries. Whether the retry is harmless depends on the method.

تكون الطريقة idempotent (متساوية الأثر) عندما يترك إرسال الطلب نفسه مرتين الخادمَ في الحالة نفسها التي يتركه فيها إرساله مرة واحدة. تخيّل اتصال جوال متقطعًا: يرسل التطبيق طلبًا، فيضيع الرد، فيعيد التطبيق المحاولة. كون إعادة المحاولة غير ضارة يعتمد على الطريقة.

Two identical POSTs create two lamps. Two identical PUTs leave one lamp in the same state.طلبا POST متطابقان ينشئان مصباحين. وطلبا PUT متطابقان يتركان مصباحًا واحدًا في الحالة نفسها.
  • PUT /lamps/7 with state "on", sent twice: the lamp is "on" after the first and still "on" after the second. Idempotent.PUT /lamps/7 بالحالة "on"، مرسلًا مرتين: المصباح "on" بعد الأول وما زال "on" بعد الثاني. متساوية الأثر (idempotent).
  • DELETE /lamps/7 sent twice: the first removes it, the second finds nothing. The response codes differ (often 200 then 404), but the end state, no lamp 7, is the same. Idempotent.DELETE /lamps/7 مرسلًا مرتين: الأول يحذفه، والثاني لا يجد شيئًا. رموز الاستجابة تختلف (غالبًا 200 ثم 404)، لكن الحالة النهائية، أي عدم وجود المصباح 7، هي نفسها. متساوية الأثر (idempotent).
  • GET /lamps/7 sent twice: nothing changes at all. Safe, and therefore idempotent.GET /lamps/7 مرسلًا مرتين: لا يتغير أي شيء على الإطلاق. آمنة، وبالتالي متساوية الأثر.
  • POST /lamps sent twice: two new lamps with two new IDs. Not idempotent.POST /lamps مرسلًا مرتين: مصباحان جديدان بمعرّفين (IDs) جديدين. ليست متساوية الأثر.

Recall

Without looking: which method goes with each CRUD letter, and which of the four are idempotent?دون أن تنظر: أي طريقة تقابل كل حرف من CRUD، وأي الطرق الأربع متساوية الأثر؟

Create is POST, Read is GET, Update (replace) is PUT, Delete is DELETE. GET, PUT and DELETE are idempotent; POST is not, because each POST creates another resource. GET is also safe.
Create هي POST، وRead هي GET، وUpdate (استبدال) هي PUT، وDelete هي DELETE. GET وPUT وDELETE متساوية الأثر؛ وPOST ليست كذلك، لأن كل POST ينشئ موردًا آخر. وGET آمنة أيضًا.

Quick checkاختبار سريع

A phone app retries a request after a timeout. Which retry can create a duplicate resource on the server?تطبيق على الجوال يعيد إرسال طلب بعد انتهاء المهلة (timeout). أي إعادة محاولة قد تنشئ موردًا مكررًا على الخادم؟

Anatomy of an HTTP requestتشريح طلب HTTP

Type http://localhost/index.html into a browser. Before any page appears, the browser, acting as an HTTP client, writes a short text message and sends it to the HTTP server on your own machine.

اكتب http://localhost/index.html في المتصفح. قبل أن تظهر أي صفحة، يكتب المتصفح، بصفته HTTP client، رسالة نصية قصيرة ويرسلها إلى HTTP server على جهازك نفسه.

On the server side the lecture draws the picture of part 01: the Endpoint receives the request, reads the resource from the server's file system, and sends it back. Here the resource is a static file, index.html, so "reading" just means opening the file. Later in the lecture the same pattern will read a lamp's state from a database instead.

على جانب الخادم ترسم المحاضرة صورة الجزء 01: الـ endpoint يستقبل الطلب، ويقرأ المورد من نظام ملفات الخادم، ثم يعيده. هنا المورد ملف ثابت، index.html، لذا فإن "القراءة" تعني ببساطة فتح الملف. لاحقًا في المحاضرة سيقرأ النمط نفسه حالة المصباح من قاعدة بيانات بدلًا من ذلك.

The URL is split across the messageالـ URL يتوزع على أجزاء الرسالة

The URL does not travel as one piece. The host part, localhost, is used to find and connect to the server, and is repeated inside the message as the Host header. The path part, /index.html, goes into the first line. Here is the full request from slide 11:

الـ URL لا ينتقل قطعة واحدة. جزء المضيف (host)، أي localhost، يُستخدم لإيجاد الخادم والاتصال به، ويتكرر داخل الرسالة في ترويسة Host. وجزء المسار (path)، أي /index.html، يذهب إلى السطر الأول. وهذا هو الطلب الكامل من الشريحة 11:

Each token of the request line gets its name, then the header fields, then the empty line that closes the head. A GET carries no body.كل جزء من سطر الطلب يأخذ اسمه، ثم حقول الترويسة، ثم السطر الفارغ الذي يُغلق الرأس. طلب GET لا يحمل جسمًا.

The first line is the Request line. Its grammar in the HTTP/1.1 standard is exact: method, single space, request target, single space, version.

السطر الأول هو request line (سطر الطلب). وقواعده في معيار HTTP/1.1 دقيقة: الطريقة، ثم مسافة واحدة، ثم هدف الطلب، ثم مسافة واحدة، ثم الإصدار.

  1. Request line: GET /index.html HTTP/1.1.سطر الطلب: GET /index.html HTTP/1.1.
  2. Zero or more Header fields, one per line, each a name, a colon and a value.صفر أو أكثر من header fields (حقول الترويسة)، حقل في كل سطر، وكل حقل اسم ثم نقطتان رأسيتان (:) ثم قيمة.
  3. An empty line (just CRLF, the two characters carriage return and line feed).سطر فارغ (مجرد CRLF، أي الحرفين: الرجوع إلى بداية السطر (carriage return) والانتقال إلى سطر جديد (line feed)).
  4. An optional body. A GET normally has none; a POST or PUT carries the new resource here.جسم اختياري. طلب GET عادةً بلا جسم؛ أما POST أو PUT فيحمل المورد الجديد هنا.

Every part of the slide 11 requestكل جزء من طلب الشريحة 11

GET
The method: the operation the client wants, here Read.الطريقة (method): العملية التي يريدها العميل، وهي هنا القراءة (Read).
/index.html
The request target: the path that names the resource on the server. The lecturer splits it into the route / and the resource index.html.هدف الطلب (request target): المسار الذي يسمّي المورد على الخادم. ويقسمه المحاضر إلى الـ route وهو / والمورد (resource) وهو index.html.
HTTP/1.1
The protocol version the client speaks.إصدار البروتوكول الذي يتحدث به العميل.
Host: localhost
Header field naming the server (and port, if not the default). Mandatory in HTTP/1.1.حقل ترويسة يسمّي الخادم (والمنفذ (port) إن لم يكن الافتراضي). وهو إلزامي في HTTP/1.1.
Accept: text/html
Header field telling the server which media type the client would like back.حقل ترويسة يخبر الخادم بنوع الوسائط (media type) الذي يود العميل استلامه.
(empty line)(سطر فارغ)
A bare CRLF that marks the end of the header fields.CRLF مجرد يعلن نهاية حقول الترويسة.
(body)(الجسم)
Absent here. A GET asks for data, so it carries nothing after the empty line.غير موجود هنا. طلب GET يطلب بيانات، لذا لا يحمل شيئًا بعد السطر الفارغ.

Two headers appear on the slide. Host: localhost names the server. It looks redundant, since the client already connected to that machine, but one machine often hosts many websites on one IP address, and Host tells it which one you mean. RFC 9112 makes it mandatory: a client must send Host, and a server must answer 400 Bad Request to an HTTP/1.1 request without it. Accept: text/html says what kind of content the client would like back, here an HTML page. A JSON API client would send Accept: application/json.

تظهر ترويستان في الشريحة. Host: localhost تسمّي الخادم. تبدو زائدة، لأن العميل اتصل بذلك الجهاز أصلًا، لكن جهازًا واحدًا كثيرًا ما يستضيف مواقع عديدة على عنوان IP واحد، وHost تخبره أيها تقصد. ويجعلها RFC 9112 إلزامية: يجب على العميل إرسال Host، ويجب على الخادم أن يرد بـ 400 Bad Request على أي طلب HTTP/1.1 يخلو منها. Accept: text/html تقول نوع المحتوى الذي يود العميل استلامه، وهو هنا صفحة HTML. وعميل API يتعامل بـ JSON سيرسل Accept: application/json.

Route and resource: the lecturer's vocabularyالمسار (route) والمورد (resource): مفردات المحاضر

Slide 11 labels the / as the "HTTP route (root)" and index.html as the "resource". Use those words in this course: the route is where on the server you are going, and the resource is the thing you get. In part 01 a Route was defined as a method plus a path, so GET together with /index.html is one route.

تسمّي الشريحة 11 الرمز / بـ "HTTP route (root)" وindex.html بـ "resource". استخدم هذه الكلمات في هذا المقرر: الـ route هو المكان الذي تذهب إليه على الخادم، والـ resource هو الشيء الذي تحصل عليه. في الجزء 01 عُرِّف الـ route على أنه طريقة زائد مسار، لذا فإن GET مع /index.html يشكّلان route واحدًا.

You can watch a real request yourself. The verbose flag of curl prints what it sends and receives:

يمكنك مشاهدة طلب حقيقي بنفسك. خيار الإسهاب (verbose) في curl يطبع ما يرسله وما يستقبله:

curl -v http://localhost/index.html
> GET /index.html HTTP/1.1
> Host: localhost
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 500
<
<html> ... </html>
Lines starting with > are the request curl sends; lines starting with < are the response. The bare > and < lines are the empty lines.

Recall

Write the request line for reading index.html from localhost, and name its three parts. Which header is mandatory in HTTP/1.1?اكتب سطر الطلب لقراءة index.html من localhost، وسمِّ أجزاءه الثلاثة. وأي ترويسة إلزامية في HTTP/1.1؟

GET /index.html HTTP/1.1: method, request target (path), HTTP version. The Host header is mandatory; here Host: localhost.
GET /index.html HTTP/1.1: الطريقة، وهدف الطلب (المسار)، وإصدار HTTP. وترويسة Host إلزامية؛ وهي هنا Host: localhost.

Quick checkاختبار سريع

Which header field must every HTTP/1.1 request carry, or the server must reject it?أي حقل ترويسة يجب أن يحمله كل طلب HTTP/1.1، وإلا وجب على الخادم رفضه؟

Anatomy of an HTTP responseتشريح استجابة HTTP

The server found index.html. Its reply has the same shape as the request: a first line, header fields, an empty line, then the body. Only the first line is different.

وجد الخادم index.html. ورده له شكل الطلب نفسه: سطر أول، ثم حقول ترويسة، ثم سطر فارغ، ثم الجسم. السطر الأول وحده هو المختلف.

The status lineسطر الحالة (status line)

A response starts with a Status line: HTTP/1.1 200 OK. It has three parts: the version, the Status code 200, and the status message OK, which the standard calls the reason phrase. Programs act on the number; the phrase is for humans and clients should not rely on it.

تبدأ الاستجابة بـ status line: HTTP/1.1 200 OK. وله ثلاثة أجزاء: الإصدار، وstatus code وهو 200، ورسالة الحالة OK التي يسمّيها المعيار reason phrase. البرامج تتصرف بناءً على الرقم؛ أما العبارة فهي للبشر، ويجب ألا يعتمد عليها العملاء.

Status line, header fields, the empty line, then the body. The ruler counts the body of a tiny page one byte at a time.سطر الحالة، ثم حقول الترويسة، ثم السطر الفارغ، ثم الجسم. المسطرة تعدّ جسم صفحة صغيرة جدًا بايتًا بايتًا.

Every part of the slide 12 responseكل جزء من استجابة الشريحة 12

HTTP/1.1
The protocol version the server answers in.إصدار البروتوكول الذي يجيب به الخادم.
200
The status code: a three-digit outcome for programs to read. 200 means success.رمز الحالة (status code): نتيجة من ثلاثة أرقام تقرؤها البرامج. و200 تعني النجاح.
OK
The status message (reason phrase): a human-readable label for the code.رسالة الحالة (reason phrase): تسمية للرمز يقرؤها البشر.
Content-Type: text/html
Header field naming the media type of the body, here an HTML page.حقل ترويسة يسمّي نوع وسائط الجسم (media type)، وهو هنا صفحة HTML.
Content-Length: 500
Header field giving the size of the body: 500 bytes.حقل ترويسة يعطي حجم الجسم: 500 bytes.
(empty line)(سطر فارغ)
A bare CRLF. Everything after it is body.CRLF مجرد. كل ما بعده هو الجسم.
<html>...</html>
The message body: the actual content of index.html.جسم الرسالة (message body): المحتوى الفعلي لـ index.html.

The first digit of the code is its class, and the class alone tells you who is responsible. RFC 9110 defines five, and every valid code lies between 100 and 599 (slide 24 returns to this range).

الرقم الأول من الرمز هو فئته، والفئة وحدها تخبرك من المسؤول. يعرّف RFC 9110 خمس فئات، وكل رمز صالح يقع بين 100 و599 (تعود الشريحة 24 إلى هذا النطاق).

ClassالفئةMeaningالمعنىIn wordsبالكلماتExamplesأمثلة
1xx1xxInformationalمعلوماتية (Informational)Request received, still processingاستُلم الطلب، وما زالت المعالجة جارية100 Continue100 Continue
2xx2xxSuccessنجاح (Success)Request received, understood and acceptedاستُلم الطلب وفُهم وقُبل200 OK, 201 Created200 OK, 201 Created
3xx3xxRedirectionإعادة توجيه (Redirection)Client must take another step, usually go elsewhereعلى العميل أن يتخذ خطوة أخرى، غالبًا الذهاب إلى مكان آخر301 Moved Permanently301 Moved Permanently
4xx4xxClient errorخطأ من العميل (Client error)The request is wrong or cannot be fulfilledالطلب خاطئ أو لا يمكن تلبيته400 Bad Request, 404 Not Found400 Bad Request, 404 Not Found
5xx5xxServer errorخطأ من الخادم (Server error)A valid request, but the server failedطلب صالح، لكن الخادم أخفق500 Internal Server Error500 Internal Server Error
Status code classesفئات رموز الحالة

A useful pairing with the CRUD table: a successful GET answers 200 OK, a successful POST usually answers 201 Created, and asking for a lamp that does not exist answers 404 Not Found.

ربط مفيد مع جدول CRUD: طلب GET الناجح يُرد عليه بـ 200 OK، وطلب POST الناجح يُرد عليه عادةً بـ 201 Created، وطلب مصباح غير موجود يُرد عليه بـ 404 Not Found.

The headers describe the bodyالترويسات تصف الجسم

The two response headers on slide 12 tell the client how to read what follows. The Content-Type header, text/html, says the body is an HTML page, so the browser renders it instead of showing raw text. The Content-Length header, 500, says the Message body is exactly 500 bytes long, so the client knows when it has received all of it.

ترويستا الاستجابة في الشريحة 12 تخبران العميل كيف يقرأ ما يليهما. ترويسة Content-Type، وقيمتها text/html، تقول إن الجسم صفحة HTML، فيعرضها المتصفح بدلًا من إظهار نص خام. وترويسة Content-Length، وقيمتها 500، تقول إن الـ message body طوله 500 bytes بالضبط، فيعرف العميل متى استلمه كله.

Worked exampleمثال محلول

Computing Content-Length for a tiny pageحساب Content-Length لصفحة صغيرة جدًا

  1. Write the bodyاكتب الجسم

    Suppose the server returns the smallest possible page: <html></html>.
    لنفترض أن الخادم يُرجع أصغر صفحة ممكنة: <html></html>.
  2. Count charactersعُدّ الحروف

    <html> is 6 characters and </html> is 7, so the body has 6 + 7 = 13 characters.
    <html> تتكون من 6 حروف و</html> من 7، لذا يحتوي الجسم على 6 + 7 = 13 حرفًا.
  3. Convert to bytesحوّل إلى بايتات

    Every character here is ASCII, and an ASCII character takes one byte in UTF-8. So 13 characters = 13 bytes.
    كل حرف هنا من ASCII، وحرف ASCII يأخذ بايتًا واحدًا في UTF-8. إذن 13 characters = 13 bytes.
  4. Resultالنتيجة

    Content-Length: 13. Headers and the status line are not counted, only the body.
    Content-Length: 13. الترويسات وسطر الحالة لا تُحسب، الجسم فقط.

The empty line the slide leaves outالسطر الفارغ الذي تُغفله الشريحة

Slide 12 draws a gap between Header and Body but never says what is in the gap. It is an empty line: a CRLF with nothing before it. The receiver reads line by line, and the moment it meets an empty line it knows the header section has ended. Everything after it is body, and Content-Length tells it how many bytes of body to read.

ترسم الشريحة 12 فجوة بين Header وBody لكنها لا تقول أبدًا ما الذي في الفجوة. إنه سطر فارغ: CRLF لا يسبقه شيء. المستقبِل يقرأ سطرًا سطرًا، وفي اللحظة التي يصادف فيها سطرًا فارغًا يعرف أن قسم الترويسات انتهى. كل ما بعده جسم، وContent-Length تخبره كم بايتًا من الجسم عليه أن يقرأ.

Recall

Name the five status code classes, and say what 200, 201 and 404 mean.سمِّ فئات رموز الحالة الخمس، وقل ماذا تعني 200 و201 و404.

1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error. 200 OK (success), 201 Created (a new resource was made, typical after POST), 404 Not Found (no such resource).
1xx معلوماتية (informational)، 2xx نجاح، 3xx إعادة توجيه (redirection)، 4xx خطأ من العميل، 5xx خطأ من الخادم. 200 OK (نجاح)، 201 Created (أُنشئ مورد جديد، وهو المعتاد بعد POST)، 404 Not Found (لا يوجد مورد كهذا).

Recall

How does a client know where the headers of a response end and how much body to read?كيف يعرف العميل أين تنتهي ترويسات الاستجابة، وكم من الجسم عليه أن يقرأ؟

The headers end at the first empty line (a bare CRLF). Content-Length then gives the body size in bytes, so the client reads exactly that many bytes after the empty line.
تنتهي الترويسات عند أول سطر فارغ (CRLF مجرد). ثم يعطي Content-Length حجم الجسم بالبايتات، فيقرأ العميل هذا العدد من البايتات بالضبط بعد السطر الفارغ.

Quick checkاختبار سريع

In the slide 12 response, what does the value 500 in Content-Length: 500 measure?في استجابة الشريحة 12، ماذا تقيس القيمة 500 في Content-Length: 500؟

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • POST is Create, GET is Read, PUT is Update or Replace, DELETE is Delete; PATCH (slide 19) is a partial update.POST للإنشاء، وGET للقراءة، وPUT للتحديث أو الاستبدال، وDELETE للحذف؛ وPATCH (الشريحة 19) تحديث جزئي.
  • GET is safe. GET, PUT and DELETE are idempotent: repeating them leaves the same server state. POST is not.GET آمنة. وGET وPUT وDELETE متساوية الأثر (idempotent): تكرارها يترك حالة الخادم نفسها. أما POST فليست كذلك.
  • A request is: request line (method, request target, version), header fields, empty line, optional body.الطلب يتكون من: سطر الطلب (الطريقة، وهدف الطلب، والإصدار)، ثم حقول الترويسة، ثم سطر فارغ، ثم جسم اختياري.
  • Host is mandatory in HTTP/1.1; Accept says which media type the client wants back.Host إلزامية في HTTP/1.1؛ وAccept تقول أي نوع وسائط (media type) يريد العميل استلامه.
  • A response is: status line (version, status code, reason phrase), header fields, empty line, body.الاستجابة تتكون من: سطر الحالة (الإصدار، ورمز الحالة، وعبارة السبب)، ثم حقول الترويسة، ثم سطر فارغ، ثم الجسم.
  • Status classes: 1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error, range 100 to 599.فئات الحالة: 1xx معلوماتية، 2xx نجاح، 3xx إعادة توجيه، 4xx خطأ من العميل، 5xx خطأ من الخادم، والنطاق من 100 إلى 599.
  • Content-Type names the body's media type; Content-Length is the body size in bytes, 13 for <html></html>.Content-Type تسمّي نوع وسائط الجسم؛ وContent-Length هو حجم الجسم بالبايتات، وهو 13 لـ <html></html>.

Sourcesالمصادر

Part 03: Designing endpoints: collections, items and query stringsتصميم نقاط النهاية: المجموعات والعناصر وسلاسل الاستعلام

How the shape of a URL decides what a method acts on: collection versus item endpoints in the smart-home lamp API, a full route table, the POST-on-an-ID mistake, and URL query parameters as inputs.كيف يحدد شكل عنوان URL ما تعمل عليه الطريقة: نقطة نهاية المجموعة (collection) مقابل نقطة نهاية العنصر (item) في واجهة API لمصباح المنزل الذكي، وجدول مسارات كامل، وخطأ استخدام POST على معرّف (ID)، ومعاملات الاستعلام (query parameters) في URL بوصفها مدخلات.

3 concepts3 مفاهيم, ، slides 13-16الشرائح 13-16

Why this part mattersلماذا يهمّ هذا الجزء

The previous part gave you the verbs: GET, POST, PUT, DELETE. This part is about the nouns. A method on its own is half a sentence. "DELETE" means nothing until the URL says what to delete, and in a well designed API the shape of that URL tells you, before you read any documentation, whether the request touches one thing or everything.

The running example stays the smart home. Lamps live at http://myHome.com/lamps, one lamp lives at http://myHome.com/lamps/123, and a second table about visits shows the full pattern most web frameworks generate for you. The midterm questions on this material are mechanical once the pattern clicks: given an action, write the method and URL; given a table, find the bad route; given a URL, take it apart.

أعطاك الجزء السابق الأفعال: GET وPOST وPUT وDELETE. أما هذا الجزء فعن الأسماء. الـ method وحدها نصف جملة فقط. كلمة "DELETE" لا تعني شيئًا حتى يقول الـ URL ما الذي سيُحذف، وفي API مصمَّم جيدًا يخبرك شكل هذا الـ URL، قبل أن تقرأ أي توثيق، هل الطلب يمسّ شيئًا واحدًا أم يمسّ كل شيء.

يبقى المثال المستمر هو المنزل الذكي. المصابيح موجودة على http://myHome.com/lamps، والمصباح الواحد موجود على http://myHome.com/lamps/123، وهناك جدول ثانٍ عن الزيارات (visits) يعرض النمط الكامل الذي تولّده لك معظم أطر الويب (web frameworks). أسئلة الامتحان النصفي في هذه المادة آلية بمجرد أن تفهم النمط: إذا أُعطيت إجراءً فاكتب الـ method والـ URL، وإذا أُعطيت جدولًا فجد المسار الخاطئ، وإذا أُعطيت URL ففكّكه إلى أجزائه.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Explain why the same HTTP method means different things on /lamps and on /lamps/123.تشرح لماذا تعني الـ HTTP method نفسها أشياء مختلفة على /lamps وعلى /lamps/123.
  2. Write the method and URL for an action such as listing all lamps or switching off lamp 7.تكتب الـ method والـ URL لأي إجراء، مثل عرض قائمة كل المصابيح أو إطفاء المصباح 7.
  3. Reproduce the resourceful route table for a collection and spot the bad route POST /visits/:id.تعيد كتابة جدول المسارات الموردية (resourceful routes) لمجموعة ما، وتكتشف المسار الخاطئ POST /visits/:id.
  4. Choose POST on the collection to create and PUT on the item URL to update, and say why.تختار POST على المجموعة للإنشاء وPUT على رابط العنصر للتحديث، وتشرح السبب.
  5. Split a URL into scheme, server, route, resource and query inputs, using '?' and '&' correctly.تقسّم الـ URL إلى scheme وserver وroute وresource ومدخلات الاستعلام (query inputs)، مع استخدام '?' و'&' بشكل صحيح.
  6. Recognize when query parameters that change state clash with the safety of GET.تميّز متى تتعارض معاملات الاستعلام (query parameters) التي تغيّر الحالة مع كون GET آمنًا (safe).

Picture the house with four lamps. You send GET http://myHome.com/lamps and get back a list of all four. You send GET http://myHome.com/lamps/123 and get back the details of one lamp, the one whose ID is 123. Same method, same server, one extra path segment, and the answer changes from "everything" to "this one".

تخيّل منزلًا فيه أربعة مصابيح. ترسل GET http://myHome.com/lamps فتعود إليك قائمة بالمصابيح الأربعة كلها. وترسل GET http://myHome.com/lamps/123 فتعود إليك تفاصيل مصباح واحد، هو المصباح الذي رقمه (ID) 123. الـ method نفسها، والخادم نفسه، ومقطع واحد إضافي في المسار (path segment)، فيتغير الجواب من "كل شيء" إلى "هذا العنصر بالذات".

The two URLs are two different endpoints. The first is a collection endpoint: it names the set of lamps. The second is an item endpoint: it names one member of that set by its ID. Each lamp is a resource, and so is the collection itself. Microsoft's API design guidance states the same idea directly: a collection is a separate resource from the items inside it, so it gets its own URI, and items hang underneath it, as in /customers and /customers/5.

هذان الـ URL هما endpoints مختلفان. الأول collection endpoint: يسمّي مجموعة المصابيح. والثاني item endpoint: يسمّي عضوًا واحدًا من تلك المجموعة بواسطة الـ ID الخاص به. كل مصباح هو resource (مورد)، والمجموعة نفسها مورد أيضًا. وإرشادات Microsoft لتصميم الـ API تقول الفكرة نفسها مباشرة: المجموعة مورد منفصل عن العناصر التي بداخلها، لذلك لها URI خاص بها، والعناصر تتفرّع تحتها، كما في /customers و/customers/5.

One method, two meaningsmethod واحدة ومعنيان

Now run every HTTP method against both endpoints. The slide's point, in its own highlighted words, is to pay attention to how a method executed on an endpoint is interpreted. The verb stays fixed; the scope comes from the URL.

الآن طبّق كل HTTP method على الـ endpoint الاثنين. فكرة الشريحة، بكلماتها المظلّلة، هي أن تنتبه إلى كيف يُفسَّر تنفيذ method على endpoint معيّن. الفعل ثابت لا يتغير، أما النطاق (scope) فيأتي من الـ URL.

Methodالـ MethodOn /lamps (collection)على /lamps (المجموعة)On /lamps/123 (item)على /lamps/123 (العنصر)
GETGETList all lamps in the houseعرض قائمة كل المصابيح في المنزلGet information about lamp 123جلب معلومات المصباح 123
DELETEDELETEDisable all lampsتعطيل كل المصابيحDisable lamp 123تعطيل المصباح 123
PUTPUTTurn all lamps on or off, or change the brightness of allتشغيل كل المصابيح أو إطفاؤها، أو تغيير سطوعها كلهاTurn lamp 123 on or off, or change its brightnessتشغيل المصباح 123 أو إطفاؤه، أو تغيير سطوعه
POSTPOSTCreate virtual resources, with IDs, for newly discovered lampsإنشاء موارد افتراضية (virtual resources) لها IDs للمصابيح المكتشفة حديثًاNot applicable for the exam (see the errata below)غير قابل للتطبيق (Not applicable) في الامتحان (انظر تصحيح الشرائح أدناه)
The lamp API: each method on the collection and on one itemالـ lamp API: كل method على المجموعة وعلى عنصر واحد
The same PUT fans out to every lamp on /lamps, and reaches only lamp 123 on /lamps/123طلب PUT نفسه ينتشر إلى كل مصباح على /lamps، ولا يصل إلا إلى المصباح 123 على /lamps/123

Read the rows one at a time. GET is a read in both columns; only the amount returned changes. PUT sets state: on the collection it is a bulk update (every lamp on, or every lamp at the same brightness), on the item it updates one lamp. Microsoft's conventions table has exactly this shape, listing PUT on a collection as a bulk update and PUT on an item as an update of that item. POST on the collection creates new members: when the hub discovers a lamp that was just screwed in, the server registers a virtual resource for it and assigns it an ID. That ID is then the last segment of the new lamp's item URL.

اقرأ الصفوف واحدًا واحدًا. GET قراءة في العمودين، والذي يتغير هو كمية ما يُعاد فقط. PUT تضبط الحالة: على المجموعة هي تحديث جماعي (bulk update)، أي تشغيل كل المصابيح أو جعلها كلها بالسطوع نفسه، وعلى العنصر تحدّث مصباحًا واحدًا. جدول الأعراف (conventions) عند Microsoft له هذا الشكل بالضبط، إذ يذكر PUT على المجموعة كتحديث جماعي، وPUT على العنصر كتحديث لذلك العنصر. وPOST على المجموعة تنشئ أعضاء جددًا: عندما يكتشف الـ hub مصباحًا رُكّب للتو، يسجّل الخادم له موردًا افتراضيًا (virtual resource) ويعيّن له ID. وهذا الـ ID يصبح بعد ذلك المقطع الأخير في رابط العنصر (item URL) الخاص بالمصباح الجديد.

Why DELETE means "disable" hereلماذا تعني DELETE هنا "تعطيل"

DELETE is the surprising row. A database row can be erased, but no request can remove a physical lamp from the ceiling. So this API interprets DELETE as switching the lamp's Enabled flag to false. The lamp still exists, still has ID 123, and can be enabled again later. Slide 23 makes this explicit: the response to DELETE carries {"id": 123, "Enabled": false}.

DELETE هو الصف المفاجئ. صف في قاعدة البيانات يمكن مسحه، لكن لا يوجد طلب يستطيع أن يزيل مصباحًا حقيقيًا من السقف. لذلك يفسّر هذا الـ API الـ DELETE على أنها تحويل علامة Enabled الخاصة بالمصباح إلى false. المصباح ما زال موجودًا، وما زال رقمه 123، ويمكن تفعيله مرة أخرى لاحقًا. والشريحة 23 تقول ذلك صراحة: الرد على DELETE يحمل {"id": 123, "Enabled": false}.

Worked exampleمثال محلول

From an action to a requestمن الإجراء إلى الطلب

  1. Switch off lamp 7أطفئ المصباح 7

    One lamp, so the item endpoint /lamps/7. Changing its on/off state is an update, so PUT /lamps/7 with the new state in the body.
    مصباح واحد، إذن نستخدم الـ item endpoint وهو /lamps/7. وتغيير حالة التشغيل والإطفاء تحديث، إذن PUT /lamps/7 مع الحالة الجديدة في جسم الطلب (body).
  2. List every lampاعرض قائمة كل المصابيح

    The whole set, read only: GET /lamps.
    المجموعة كاملة، وللقراءة فقط: GET /lamps.
  3. Disable every lamp before a holidayعطّل كل المصابيح قبل الإجازة

    The whole set, and disable is this API's DELETE: DELETE /lamps.
    المجموعة كاملة، والتعطيل هو معنى DELETE في هذا الـ API: DELETE /lamps.
  4. Register a lamp the hub just discoveredسجّل مصباحًا اكتشفه الـ hub للتو

    Its ID does not exist yet, so you cannot name an item URL. Ask the collection to create it: POST /lamps, and the server assigns the ID.
    الـ ID الخاص به غير موجود بعد، لذلك لا تستطيع أن تسمّي رابط عنصر (item URL). اطلب من المجموعة أن تنشئه: POST /lamps، والخادم هو من يعيّن الـ ID.
  5. Resultالنتيجة

    Two decisions every time: one or all (item or collection URL), then read, create, update or disable (GET, POST, PUT or DELETE).
    قراران في كل مرة: واحد أم الكل (رابط عنصر أم رابط مجموعة)، ثم قراءة أم إنشاء أم تحديث أم تعطيل (GET أو POST أو PUT أو DELETE).

Quick checkاختبار سريع

Which request switches off every lamp in the house at once?أي طلب يطفئ كل المصابيح في المنزل دفعة واحدة؟

Recall

Without looking, give the meaning of GET, DELETE and PUT on /lamps and on /lamps/123.دون أن تنظر، اذكر معنى GET وDELETE وPUT على /lamps وعلى /lamps/123.

GET: list all lamps versus get information about lamp 123. DELETE: disable all lamps versus disable lamp 123. PUT: turn all lamps on or off or set their brightness versus do the same for lamp 123 only.
GET: عرض قائمة كل المصابيح، مقابل جلب معلومات المصباح 123. DELETE: تعطيل كل المصابيح، مقابل تعطيل المصباح 123. PUT: تشغيل كل المصابيح أو إطفاؤها أو ضبط سطوعها، مقابل فعل الشيء نفسه للمصباح 123 فقط.

Suppose a clinic's booking system stores visits. Every operation anyone will ever ask for falls into one of a few shapes: see all visits, see one, book a new one, change one, cancel one. Write each shape as a route, a method plus a path, and you get the table below.

افترض أن نظام حجوزات في عيادة يخزّن الزيارات (visits). كل عملية سيطلبها أي شخص في أي وقت تقع في أحد أشكال قليلة: عرض كل الزيارات، عرض زيارة واحدة، حجز زيارة جديدة، تعديل زيارة، إلغاء زيارة. اكتب كل شكل منها على هيئة route (مسار)، أي method مع path، فتحصل على الجدول أدناه.

Methodالـ MethodPathالمسار (Path)PurposeالغرضScopeالنطاق
GETGET/visits/visitsList all visitsعرض قائمة كل الزياراتCollectionمجموعة (Collection)
GETGET/visits/new/visits/newShow the form for requesting a new visitعرض النموذج (form) لطلب زيارة جديدةCollectionمجموعة (Collection)
POSTPOST/visits/visitsCreate a new visit, then redirect somewhereإنشاء زيارة جديدة، ثم إعادة التوجيه (redirect) إلى مكان ماCollectionمجموعة (Collection)
GETGET/visits/:id/visits/:idShow one visitعرض زيارة واحدةItemعنصر (Item)
PUTPUT/visits/:id/visits/:idUpdate one visitتحديث زيارة واحدةItemعنصر (Item)
DELETEDELETE/visits/:id/visits/:idDelete one visitحذف زيارة واحدةItemعنصر (Item)
POSTPOST/visits/:id/visits/:idError: do not do thisخطأ (Error): لا تفعل هذاItemعنصر (Item)
The resourceful routes for visits (slide 14)المسارات الموردية (resourceful routes) للزيارات (الشريحة 14)

The :id in the path is a placeholder, not literal text. A router matches /visits/42 against /visits/:id and hands 42 to the handler as the ID. The table is nearly what Ruby on Rails generates from a single resources declaration: GET on the collection to list, GET on /new for a form, POST on the collection to create, and GET, PATCH or PUT, and DELETE on the item URL. Rails adds one route the slide leaves out, GET /visits/:id/edit for an edit form. Notice that three paths serve six valid routes here, because the router matches on the method and the path together.

الـ :id في المسار عنصر نائب (placeholder)، وليس نصًا حرفيًا. الـ router يطابق /visits/42 مع /visits/:id ويمرّر 42 إلى الـ handler على أنه الـ ID. هذا الجدول يكاد يكون ما يولّده Ruby on Rails من تعريف resources واحد: GET على المجموعة للعرض، وGET على /new لنموذج (form)، وPOST على المجموعة للإنشاء، ثم GET وPATCH أو PUT وDELETE على رابط العنصر. ويضيف Rails مسارًا واحدًا تتركه الشريحة، وهو GET /visits/:id/edit لنموذج التعديل. لاحظ أن ثلاثة مسارات (paths) فقط تخدم هنا ستة routes صحيحة، لأن الـ router يطابق على الـ method والـ path معًا.

Rows light up as valid routes; POST /visits/:id is struck out and redirected to PUT on the same URLتضيء الصفوف كمسارات صحيحة، ويُشطب POST /visits/:id ويُحوَّل إلى PUT على الـ URL نفسه

The bad route: POST on an IDالمسار الخاطئ: POST على ID

The last row is the one the slide circles in red. Why is POST /visits/:id wrong? Think about who knows the ID. When a visit is created, it does not have an ID yet; the server assigns one. So creation must be addressed to something that already exists, and that is the collection. POST on /visits says "add a new member to this set and tell me its ID". RFC 9110 lists exactly this use of POST: creating a new resource that has yet to be identified by the origin server, with a 201 Created response whose Location header names the new resource.

الصف الأخير هو الذي تحيطه الشريحة بدائرة حمراء. لماذا POST /visits/:id خاطئ؟ فكّر في من يعرف الـ ID. عندما تُنشأ زيارة لا يكون لها ID بعد، والخادم هو من يعيّنه. لذلك يجب أن يوجَّه الإنشاء إلى شيء موجود مسبقًا، وهذا الشيء هو المجموعة. POST على /visits تقول "أضف عضوًا جديدًا إلى هذه المجموعة وأخبرني بالـ ID الخاص به". ويذكر RFC 9110 هذا الاستخدام لـ POST بالضبط: إنشاء مورد جديد لم يحدد له الخادم الأصلي (origin server) معرّفًا بعد، مع رد 201 Created يسمّي الـ Location header فيه المورد الجديد.

If you already have an ID, the visit already exists, and changing it is an update. The method for replacing the state of a resource at a known URL is PUT; RFC 9110 defines PUT as a request that the state of the target resource be created or replaced with the enclosed representation. The slide's note says the same in plain words: creating a new visit, but if an ID is provided, it should update instead. Microsoft's conventions table marks POST on /customers/1 simply as an error.

إذا كان لديك ID بالفعل، فالزيارة موجودة مسبقًا، وتغييرها تحديث. والـ method المخصصة لاستبدال حالة مورد على URL معروف هي PUT، إذ يعرّف RFC 9110 الـ PUT بأنها طلب بأن تُنشأ حالة المورد المستهدف أو تُستبدل بالتمثيل (representation) المرفق. وملاحظة الشريحة تقول الشيء نفسه بكلمات بسيطة: إنشاء زيارة جديدة، لكن إذا أُعطي ID فيجب أن يكون تحديثًا بدلًا من ذلك. وجدول الأعراف عند Microsoft يعلّم POST على /customers/1 ببساطة على أنه خطأ (error).

Quick checkاختبار سريع

A client wants to change the date of visit 42. Which request is correct?يريد عميل (client) أن يغيّر تاريخ الزيارة 42. أي طلب صحيح؟

Recall

List the six valid visit routes and the one the slide marks as an error.اذكر مسارات الزيارات الستة الصحيحة، والمسار الذي تعلّمه الشريحة على أنه خطأ.

GET /visits (list), GET /visits/new (form), POST /visits (create), GET /visits/:id (show), PUT /visits/:id (update), DELETE /visits/:id (delete). The error is POST /visits/:id: with an ID you should update with PUT.
GET /visits (عرض القائمة)، GET /visits/new (النموذج)، POST /visits (إنشاء)، GET /visits/:id (عرض زيارة)، PUT /visits/:id (تحديث)، DELETE /visits/:id (حذف). والخطأ هو POST /visits/:id: إذا كان لديك ID فيجب أن تحدّث بـ PUT.

Here is a full URL from the lamp API: http://www.myhome.com/lamps/123?brightness=6&color=green. Read it left to right and every piece has a job. Some pieces get the request to the right machine, some pick the right handler, one picks the resource, and the tail carries the data the operation needs.

هذا URL كامل من الـ lamp API: http://www.myhome.com/lamps/123?brightness=6&color=green. اقرأه من اليسار إلى اليمين، وستجد أن لكل قطعة فيه وظيفة. بعض القطع توصل الطلب إلى الجهاز الصحيح، وبعضها يختار الـ handler الصحيح، وقطعة واحدة تختار المورد، والذيل يحمل البيانات التي تحتاجها العملية.

Each part of the URL lights up with its role; '?' opens the query and '&' splits the pairsيضيء كل جزء من الـ URL مع دوره، فعلامة '?' تفتح الاستعلام وعلامة '&' تفصل بين الأزواج

Slide 15's breakdown, with the scheme addedتفكيك الشريحة 15، مع إضافة الـ scheme

http://
Scheme: the protocol to use, here HTTPالـ Scheme: البروتوكول المستخدم، وهو هنا HTTP
www.myhome.com
Server: which machine receives the requestالخادم (Server): الجهاز الذي يستقبل الطلب
/lamps
Route: where on the server to deliver the request, the lamps handlerالمسار (Route): المكان على الخادم الذي يُسلَّم إليه الطلب، أي الـ handler الخاص بالمصابيح
/123
Resource: the one the method in the request operates onالمورد (Resource): المورد الذي تعمل عليه الـ method الموجودة في الطلب
?brightness=6&color=green
Query string: inputs to the operation on that resourceالـ Query string: مدخلات العملية على ذلك المورد

The standards use slightly different names for the same pieces. RFC 3986 splits every URI into scheme, authority, path, query and fragment, so www.myhome.com is the authority and /lamps/123 together is the path. The lecture splits the path in two because, for an API, the two halves do different work: /lamps picks the handler and /123 picks the resource inside it. Combined, /lamps/123 is the URI that identifies one lamp.

المعايير تستخدم أسماء مختلفة قليلًا للقطع نفسها. يقسّم RFC 3986 كل URI إلى scheme وauthority وpath وquery وfragment، لذلك www.myhome.com هو الـ authority، و/lamps/123 كله هو الـ path. والمحاضرة تقسّم الـ path إلى قسمين لأن النصفين في الـ API يؤديان عملين مختلفين: /lamps يختار الـ handler، و/123 يختار الـ resource داخله. ومعًا يكون /lamps/123 هو الـ URI الذي يعرّف مصباحًا واحدًا.

The query string: inputs, not identityالـ query string: مدخلات، وليست هوية

The query string starts at the first ?. After it come key=value pairs separated by &. So ?brightness=6&color=green carries two inputs, brightness = 6 and color = green. RFC 3986 notes that the query component is often used to carry information as key=value pairs, and MDN describes the parameters as a list of key/value pairs separated with the & symbol, which each server interprets by its own rules.

تبدأ الـ query string عند أول ?. وبعدها تأتي أزواج key=value تفصل بينها &. إذن ?brightness=6&color=green تحمل مدخلين: brightness = 6 وcolor = green. ويذكر RFC 3986 أن مكوّن الاستعلام (query component) يُستخدم كثيرًا لحمل المعلومات على هيئة أزواج key=value، ويصف MDN المعاملات (parameters) بأنها قائمة من أزواج key/value مفصولة بالرمز &، ويفسّرها كل خادم بقواعده الخاصة.

Slide 16 shows what happens on arrival. The server runs a program for resource /123, feeds it the two inputs, and the output is the new state of the lamp: brightness 6, color green. The slide's highlighted sentence is the takeaway: the inputs to the program are given explicitly in the URL.

تعرض الشريحة 16 ما يحدث عند الوصول. يشغّل الخادم برنامجًا للمورد /123، ويغذّيه بالمدخلين، والمخرج هو الحالة الجديدة للمصباح: السطوع 6 واللون أخضر. والجملة المظلّلة في الشريحة هي الخلاصة: مدخلات البرنامج تُعطى صراحة في الـ URL.

brightness=6 and color=green flow into the program for /lamps/123, which produces the lamp's new stateيتدفق brightness=6 وcolor=green إلى برنامج /lamps/123، الذي ينتج الحالة الجديدة للمصباح
  • The first ? starts the query; any later ? is just part of the query text.أول ? يبدأ الاستعلام، وأي ? يأتي بعده هو مجرد جزء من نص الاستعلام.
  • & separates pairs; = separates a key from its value.& تفصل بين الأزواج، و= تفصل المفتاح (key) عن قيمته (value).
  • Everything before the ? says where; everything after says with what.كل ما قبل ? يقول أين، وكل ما بعده يقول بماذا.

Worked exampleمثال محلول

Parse an exam URLفكّك URL من الامتحان

  1. The URLالـ URL

    http://www.myhome.com/lamps/7?brightness=3
    http://www.myhome.com/lamps/7?brightness=3
  2. Server and routeالخادم والمسار

    Server www.myhome.com; route /lamps, so the lamps handler receives it.
    الخادم www.myhome.com، والمسار (route) /lamps، إذن الـ handler الخاص بالمصابيح هو الذي يستقبله.
  3. Resourceالمورد

    /7: the method operates on lamp 7, an item, not the collection.
    /7: الـ method تعمل على المصباح 7، وهو عنصر (item) وليس المجموعة.
  4. Inputsالمدخلات

    After ? there is one pair and no &: brightness = 3.
    بعد ? يوجد زوج واحد ولا توجد &: brightness = 3.
  5. Resultالنتيجة

    Server www.myhome.com, route /lamps, resource 7, input brightness = 3. If asked for the right method to apply it, an update to one field of one lamp is PATCH (or PUT with the full state), not GET.
    الخادم www.myhome.com، والمسار /lamps، والمورد 7، والمدخل brightness = 3. وإذا سُئلت عن الـ method الصحيحة لتطبيقه، فتحديث حقل واحد لمصباح واحد يكون PATCH (أو PUT مع الحالة الكاملة)، وليس GET.

Quick checkاختبار سريع

In http://www.myhome.com/lamps/123?brightness=6&color=green, what does /123 identify?في http://www.myhome.com/lamps/123?brightness=6&color=green، ماذا يعرّف /123؟

Quick checkاختبار سريع

Slides 13 and 23 disagree about POST on /lamps/123. Which exam answer fits?تختلف الشريحتان 13 و23 حول POST على /lamps/123. أي جواب يناسب الامتحان؟

Recall

What do '?' and '&' do in a URL, and what does the server do with what follows them?ماذا تفعل '?' و'&' في الـ URL، وماذا يفعل الخادم بما يأتي بعدهما؟

'?' marks the start of the query string; '&' separates key=value pairs. The server passes the pairs as inputs to the program that handles the route and resource, which computes the new state (or, for a read, shapes the result).
'?' تعلّم بداية الـ query string، و'&' تفصل بين أزواج key=value. ويمرّر الخادم هذه الأزواج كمدخلات إلى البرنامج الذي يعالج الـ route والـ resource، فيحسب الحالة الجديدة (أو، في حالة القراءة، يشكّل النتيجة).

Recall

Why is changing a lamp's brightness with a GET query string a bad design?لماذا يُعد تغيير سطوع مصباح باستخدام query string في طلب GET تصميمًا سيئًا؟

GET is defined as safe: the client does not request any state change. Clients, crawlers and caches may repeat or prefetch GETs, so a state change hidden in one can fire unintentionally. Use PATCH or PUT with a body.
GET معرّفة بأنها آمنة (safe): العميل لا يطلب أي تغيير في الحالة. والعملاء والزواحف (crawlers) والذاكرات المؤقتة (caches) قد تكرر طلبات GET أو تجلبها مسبقًا، لذلك قد يُنفَّذ تغيير الحالة المخفي في أحدها دون قصد. استخدم PATCH أو PUT مع جسم طلب (body).

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • A collection endpoint (/lamps) names a set; an item endpoint (/lamps/123) names one member.الـ collection endpoint (/lamps) يسمّي مجموعة كاملة، والـ item endpoint (/lamps/123) يسمّي عضوًا واحدًا منها.
  • The method says what to do, the URL says to what: GET /lamps lists all, GET /lamps/123 reads one.الـ method تقول ماذا تفعل، والـ URL يقول على ماذا: GET /lamps يعرض الكل، وGET /lamps/123 يقرأ عنصرًا واحدًا.
  • In the lamp API, DELETE means disable (a soft delete), on all lamps or on one.في الـ lamp API تعني DELETE التعطيل (soft delete، أي حذف ناعم)، سواء على كل المصابيح أو على مصباح واحد.
  • Resourceful routes: GET and POST on /visits, GET, PUT and DELETE on /visits/:id, plus GET /visits/new for a form.المسارات الموردية: GET وPOST على /visits، وGET وPUT وDELETE على /visits/:id، إضافة إلى GET /visits/new لعرض نموذج (form).
  • POST /visits/:id is wrong. Create with POST on the collection (server assigns the ID); update with PUT on the item.المسار POST /visits/:id خاطئ. أنشئ بـ POST على المجموعة (والخادم يعيّن الـ ID)، وحدّث بـ PUT على العنصر.
  • Exam rule: POST on /lamps/123 is Not Applicable (slides 14 and 23), even though slide 13 lists it.قاعدة الامتحان: POST على /lamps/123 هو Not Applicable (الشريحتان 14 و23)، رغم أن الشريحة 13 تذكره.
  • URL anatomy: www.myhome.com is the server, /lamps the route, /123 the resource, ?brightness=6&color=green the inputs.تشريح الـ URL: www.myhome.com هو الخادم (server)، و/lamps هو المسار (route)، و/123 هو المورد (resource)، و?brightness=6&color=green هي المدخلات (inputs).
  • '?' starts the query string, '&' separates key=value pairs; the server runs a program on those inputs.علامة '?' تبدأ الـ query string، وعلامة '&' تفصل بين أزواج key=value، والخادم يشغّل برنامجًا على هذه المدخلات.
  • Changing state through a GET query string breaks GET's safety; slide 19 does it properly with PATCH and a JSON body.تغيير الحالة عبر query string في طلب GET يكسر كون GET آمنًا، والشريحة 19 تفعل ذلك بالطريقة الصحيحة باستخدام PATCH وجسم JSON.

Sourcesالمصادر

Part 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.

4 concepts4 مفاهيم, ، slides 17-21الشرائح 17-21

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في نهاية هذا الجزء ستستطيع

  1. State who introduced REST, when and where, and explain why it is an architectural style rather than a protocol.أن تذكر من قدّم REST ومتى وأين، وأن تشرح لماذا هو أسلوب معماري (architectural style) وليس بروتوكولًا.
  2. 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) هي القيد الاختياري الوحيد، وتعطي مثال المصباح لكل قيد.
  3. Define statelessness precisely and explain why it makes horizontal scaling and failover easy.أن تعرّف انعدام الحالة (statelessness) تعريفًا دقيقًا، وتشرح لماذا يجعل التوسع الأفقي (horizontal scaling) وتجاوز الأعطال (failover) سهلين.
  4. 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) أم يجب أن يصل إلى الخادم.
  5. Name the four uniform-interface sub-constraints and explain HATEOAS with a concrete response.أن تسمّي القيود الفرعية الأربعة للواجهة الموحدة (uniform interface)، وتشرح HATEOAS باستجابة ملموسة.
  6. 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) له شقّان، وإجابة الاختبار تحتاج إلى الاثنين:

  1. The server does not store client session data between requests.لا يخزّن الخادم بيانات جلسة العميل (session data) بين الطلبات.
  2. 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، وفي أي نوع من الوثائق، وفي أي عام؟ ثم اذكر شقّي انعدام الحالة.

Roy Thomas Fielding, in his doctoral dissertation, in 2000. Statelessness: the server stores no client session data, and every request carries all the information needed to process it.
Roy Thomas Fielding، في أطروحة الدكتوراه الخاصة به، عام 2000. انعدام الحالة: الخادم لا يخزّن أي بيانات جلسة للعميل، وكل طلب يحمل كل المعلومات اللازمة لمعالجته.

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 قيدًا بعد قيد، وكل قيد يُضاف يكسب خاصية كان الويب يحتاج إليها. إليك الخريطة كاملة قبل أن نمرّ عليها واحدًا واحدًا، ومرّر المؤشر لترى مثال المصباح على كل بطاقة.

The six REST constraints, each with its lamp example; the dashed card is the optional oneقيود REST الستة، ولكل منها مثال المصباح، والبطاقة ذات الإطار المتقطع هي القيد الاختياري

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، ثم اقرأه مرة أخرى. في ظل انعدام الحالة يحمل كل طلب من هذه الطلبات معرّف المصباح والمصادقة المطلوبة، ولا يعتمد أي منها على الطلب الذي قبله. ويجيب الخادم عن كل طلب كأنه لم يرَ هذا العميل من قبل.

Each request carries the lamp ID and a token, so any server can answer itكل طلب يحمل معرّف المصباح ورمزًا (token)، لذلك يستطيع أي خادم أن يجيب عنه

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ذاكرة لكل عميل على الخادم
Stateless versus stateful server for the lamp APIخادم عديم الحالة مقابل خادم ذي حالة في واجهة المصباح

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.

A response made at 0 s with max-age=60 is fresh until 60 s: 45 s hits the cache, 75 s goes back to the serverاستجابة صدرت عند 0 s مع max-age=60 تبقى طازجة حتى 60 s: الطلب عند 45 s يُخدم من الذاكرة المؤقتة، والطلب عند 75 s يعود إلى الخادم

Worked exampleمثال محلول

Is the cached lamp status still usable?هل ما زالت حالة المصباح المخزنة مؤقتًا صالحة للاستخدام؟

  1. 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).
  2. 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)، لذلك تجيب الذاكرة المؤقتة ولا يصل الطلب إلى الخادم أبدًا.
  3. 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)، لذلك يجب أن يعود الطلب إلى الخادم، الذي قد يرسل نسخة جديدة أو يؤكد أن النسخة القديمة ما زالت صالحة.
  4. 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؟

At 30 s and 59 s the copy is younger than 60 s, so the cache serves it. At 90 s it is stale and the request must go back to the server.
عند 30 s و59 s يكون عمر النسخة أقل من 60 s، فتخدمها الذاكرة المؤقتة. وعند 90 s تكون منتهية الصلاحية، ويجب أن يعود الطلب إلى الخادم.

Recall

Why does statelessness make a REST API easier to scale?لماذا يجعل انعدام الحالة توسيع واجهة REST API أسهل؟

The server keeps no session, so any server can answer any request. A load balancer can spread requests over many identical servers, you scale by adding servers, and a crashed server loses no client state.
الخادم لا يحتفظ بأي جلسة، لذلك يستطيع أي خادم أن يجيب عن أي طلب. فيستطيع موازن الأحمال توزيع الطلبات على خوادم متطابقة كثيرة، وتتوسع بإضافة خوادم، والخادم الذي يتعطل لا يضيع معه أي حالة للعميل.

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) تعني تفاعلًا موحدًا قياسيًا مع الموارد. يُتعامل مع كل مورد بالمجموعة الصغيرة نفسها من الحركات، فالعميل الذي يعرف كيف يتعامل مع المصابيح يعرف أيضًا كيف يتعامل مع المستخدمين أو منظمات الحرارة. ولها أربعة قيود فرعية:

One PATCH exchange shows all four uniform-interface sub-constraintsتبادل PATCH واحد يُظهر القيود الفرعية الأربعة للواجهة الموحدة
  1. Resource identification through URIs. Each resource has a URI: /lamps/123 uniquely identifies lamp 123.تعريف الموارد عبر عناوين URI. لكل مورد (resource) عنوان URI: فالعنوان /lamps/123 يعرّف المصباح 123 تعريفًا فريدًا.
  2. 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 التي تستبدل المورد كاملًا.
  3. 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).
  4. 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.سمِّ القيود الفرعية الأربعة للواجهة الموحدة، وأعطِ مثال المصباح لكل منها.

Resource identification through URIs (/lamps/123); manipulation through representations (PATCH /lamps/123 with {"brightness": 80}); self-descriptive messages (method, Content-Type and JSON body describe the update); HATEOAS (the response links to retrieve, update or delete the lamp).
تعريف الموارد عبر عناوين URI (/lamps/123)؛ والتعديل عبر التمثيلات (PATCH /lamps/123 مع {"brightness": 80})؛ والرسائل ذاتية الوصف (الطريقة وContent-Type وجسم JSON تصف التحديث)؛ وHATEOAS (الاستجابة تتضمن روابط لجلب المصباح أو تحديثه أو حذفه).

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: لا يستطيع أي مكوّن أن يرى ما وراء الطبقة المباشرة التي يتفاعل معها.

Clientالعميل
mobile appتطبيق جوال

Sends PATCH /lamps/123 to one address

يرسل PATCH /lamps/123 إلى عنوان واحد

API gatewayبوابة API

Checks token, rate limits, routes

تتحقق من الرمز (token) وحدود المعدل (rate limits) وتوجّه الطلب

Load balancerموازن الأحمال

Picks a healthy server

يختار خادمًا سليمًا

Smart-home serversخوادم المنزل الذكي

Hold the lamp state

تحفظ حالة المصباح

A lamp request through the layers; the client only knows the first hopطلب المصباح وهو يعبر الطبقات، والعميل لا يعرف إلا الطبقة الأولى

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/123Uniform interface: identification by URIالواجهة الموحدة: التعريف عبر URI
HTTP request methodطريقة طلب HTTPPATCHPATCHUniform interface: standard verbs, self-descriptiveالواجهة الموحدة: أفعال قياسية، ورسائل ذاتية الوصف
HTTP response status codeرمز حالة استجابة HTTP200 OK200 OKSelf-descriptive messagesالرسائل ذاتية الوصف (self-descriptive messages)
Header fieldsحقول الترويسة (header fields)Authorization, Content-Type, AcceptAuthorization, Content-Type, AcceptStatelessness and self-descriptive messagesانعدام الحالة والرسائل ذاتية الوصف
JSON messageرسالة JSON{"brightness": 80}{"brightness": 80}Manipulation through representationsالتعديل عبر التمثيلات (manipulation through representations)
Design elements and the constraint each one servesعناصر التصميم والقيد الذي يخدمه كل منها

Recall

Name the five design elements of a REST API, in the slide's order.سمِّ عناصر التصميم الخمسة لواجهة REST API بترتيب الشريحة.

URL, HTTP request method, HTTP response status code, header fields, JSON message.
URL، وطريقة طلب HTTP، ورمز حالة استجابة HTTP، وحقول الترويسة، ورسالة JSON.

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المصادر

Part 05: Designing REST APIs: the lamp system and the calculatorتصميم واجهات REST API: نظام المصباح والآلة الحاسبة

The five design elements applied end to end: the full smart-home lamp API with headers and JSON bodies, then the calculator addition API with status codes, and how to design the other operations yourself.عناصر التصميم الخمسة مطبقةً من البداية إلى النهاية: واجهة API الكاملة لمصباح المنزل الذكي مع الترويسات (headers) وأجسام JSON، ثم واجهة API للجمع في الآلة الحاسبة مع رموز الحالة (status codes)، وكيف تصمم بقية العمليات بنفسك.

5 concepts5 مفاهيم, ، slides 22-27الشرائح 22-27

Why this part mattersلماذا يهمّ هذا الجزء

The previous parts gave you the vocabulary: endpoints, methods, status codes, headers, JSON and the REST constraints. This part spends all of it on two complete designs. The first is a home lamp you control from a browser. The second is a calculator whose arithmetic runs on a server. Both end in a table, and that table is exactly what the midterm on 26 Oct 2026 is likely to ask you to fill in.

The order follows how you would design a real API. First, picture the system and the one resource it controls. Second, write the route table for that resource. Third, abstract the table into five design elements you can apply to anything. Fourth, apply them to the calculator, including every status code it can return. Fifth, design the remaining operations yourself. Along the way, every mistake or weak spot on the slides is flagged, so you can answer the exam in the slide's format and still know what a production API would do differently.

أعطتك الأجزاء السابقة المفردات: نقاط النهاية (endpoints)، والطرق (methods)، ورموز الحالة، والترويسات (headers)، وJSON، وقيود REST. هذا الجزء يستعمل كل ذلك في تصميمين كاملين. الأول مصباح منزلي تتحكم فيه من المتصفح. والثاني آلة حاسبة تُنفَّذ عملياتها الحسابية على خادم. كلاهما ينتهي بجدول، وهذا الجدول هو بالضبط ما يُرجَّح أن يطلب منك اختبار منتصف الفصل في 26 Oct 2026 تعبئته.

الترتيب يتبع الطريقة التي تصمم بها API حقيقيًا. أولًا، تخيّل النظام والمورد الوحيد الذي يتحكم فيه. ثانيًا، اكتب جدول المسارات (route table) لهذا المورد. ثالثًا، جرّد الجدول إلى خمسة عناصر تصميم يمكنك تطبيقها على أي شيء. رابعًا، طبّقها على الآلة الحاسبة، بما في ذلك كل رمز حالة يمكن أن تعيده. خامسًا، صمّم بقية العمليات بنفسك. وعلى طول الطريق، ننبّه إلى كل خطأ أو نقطة ضعف في الشرائح، حتى تجيب في الامتحان بصيغة الشريحة وتعرف مع ذلك ما الذي سيفعله API حقيقي في بيئة الإنتاج بشكل مختلف.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Trace one click in the lamp UI to an HTTP request, a server and a signal to device 123.تتبّع نقرة واحدة في واجهة المصباح حتى تصبح طلب HTTP يصل إلى الخادم ثم إشارة إلى الجهاز 123.
  2. Complete the lamp API table: what GET, DELETE, PUT and POST mean on /lamps and on /lamps/123, with the right JSON.أكمل جدول API المصباح: ماذا تعني GET وDELETE وPUT وPOST على /lamps وعلى /lamps/123، مع الـ JSON الصحيح.
  3. State the five REST design elements and what each one holds.اذكر عناصر تصميم REST الخمسة وما يحمله كل عنصر.
  4. Design a calculator operation in the slide's table format and pick the right status code for each failure.صمّم عملية في الآلة الحاسبة بصيغة جدول الشريحة، واختر رمز الحالة الصحيح لكل حالة فشل.
  5. Explain why the slide's GET with a JSON body is shaky, and give two correct alternatives.اشرح لماذا يُعد طلب GET مع جسم JSON في الشريحة تصميمًا مهزوزًا، واذكر بديلين صحيحين.

You are on the sofa with your phone. The browser shows a page at .../lamps/123 with an ON button, an OFF button and an intensity slider. You tap ON. A second later the lamp in the hallway lights up. What happened in between is the whole subject of this lecture in miniature.

أنت جالس على الأريكة وفي يدك هاتفك. يعرض المتصفح صفحة على .../lamps/123 فيها زر ON وزر OFF ومنزلق لشدة الإضاءة. تضغط ON. وبعد ثانية يضيء المصباح في الممر. ما حدث بين الضغطة والإضاءة هو موضوع هذه المحاضرة كله في صورة مصغّرة.

Clientالعميل
Browser UIواجهة المتصفح

ON, OFF and an intensity slider on a page for lamp 123.

زر ON وزر OFF ومنزلق لشدة الإضاءة في صفحة المصباح 123.

HTTP requestطلب HTTP
Serverالخادم
Raspberry Pi

Hosts the lamp API, checks the request, decides what to do.

يستضيف API المصباح، ويتحقق من الطلب، ويقرر ما يفعل.

signalإشارة
Deviceالجهاز
Lamp, ID 123المصباح، المعرّف 123

Turns on, off, or changes brightness.

يشتغل أو ينطفئ أو يغيّر سطوعه.

One tap travels as an HTTP request to the server, which signals the deviceضغطة واحدة تنتقل كطلب HTTP إلى الخادم، والخادم يرسل إشارة إلى الجهاز

Three components, two very different links. Between the browser and the Raspberry Pi the link is HTTP: the browser is an HTTP client, the Pi runs an HTTP server, and the tap becomes a request to an Endpoint. Between the Pi and the lamp the link is a device signal, a GPIO pin, a relay or a radio protocol, that the web never sees. The API is the HTTP side only. That is why the design problem is phrased in terms of URLs and methods and never mentions wires or voltages.

ثلاثة مكونات، ورابطان مختلفان تمامًا. بين المتصفح وRaspberry Pi الرابط هو HTTP: المتصفح هو HTTP client، والـ Pi يشغّل HTTP server، والضغطة تصبح طلبًا إلى endpoint. أما بين الـ Pi والمصباح فالرابط إشارة جهاز: منفذ GPIO، أو مُرحِّل (relay)، أو بروتوكول لاسلكي، ولا يراه الويب أبدًا. الـ API هو جانب HTTP فقط. ولهذا تُصاغ مسألة التصميم بلغة عناوين URL والطرق، ولا تذكر الأسلاك أو الجهد الكهربائي أبدًا.

A tap on ON becomes an HTTP request, the server turns it into a signal, and lamp 123 lights upالضغط على ON يصبح طلب HTTP، والخادم يحوّله إلى إشارة، فيضيء المصباح 123

The lamp is the Resource: the thing the server manages and exposes. The number 123 is its identity, and the URL that names it, /lamps/123, is its URI. Every operation the UI offers is a change to the lamp's state: whether it is on, how bright it is, whether it is enabled at all. So the design question becomes: which method on which URL changes which part of that state, and what does the data look like?

المصباح هو resource (المورد): الشيء الذي يديره الخادم ويعرضه. الرقم 123 هو هويته، والعنوان الذي يسمّيه، /lamps/123، هو URI الخاص به. كل عملية تقدمها الواجهة هي تغيير في حالة المصباح: هل هو مضاء، وما درجة سطوعه، وهل هو مفعَّل أصلًا. فيصبح سؤال التصميم: أي طريقة على أي URL تغيّر أي جزء من هذه الحالة، وكيف تبدو البيانات؟

Recall

Which link in the lamp system is the API, and which is not?أي رابط في نظام المصباح هو الـ API، وأيها ليس كذلك؟

The browser to server link is the API: HTTP requests to URLs such as /lamps/123. The server to lamp link is a device signal that the client never sees, so it is not part of the API design.

الرابط من المتصفح إلى الخادم هو الـ API: طلبات HTTP إلى عناوين مثل /lamps/123. أما الرابط من الخادم إلى المصباح فهو إشارة جهاز لا يراها العميل أبدًا، لذلك فهو ليس جزءًا من تصميم الـ API.

Start with one row. A GET request to http://myHome.com/lamps/123 returns {"id": 123, "Enabled": true, "ON": true, "brightness": 35}. Read the JSON as a sentence: lamp 123 is enabled, switched on, and at 35 percent brightness. That JSON is a Representation of the lamp's state, not the lamp itself.

ابدأ بصف واحد. طلب GET إلى http://myHome.com/lamps/123 يعيد {"id": 123, "Enabled": true, "ON": true, "brightness": 35}. اقرأ الـ JSON كجملة: المصباح 123 مفعَّل، ومضاء، وسطوعه 35 بالمئة. هذا الـ JSON هو representation (تمثيل) لحالة المصباح، وليس المصباح نفسه.

Now fill in the whole grid. The rows are the four methods. The columns are two URLs: the Collection endpoint /lamps, meaning all lamps, and the Item endpoint /lamps/123, meaning one lamp. Each cell says what that method does to that URL.

الآن املأ الشبكة كلها. الصفوف هي الطرق الأربع. والأعمدة عنوانان: collection endpoint /lamps ومعناها كل المصابيح، وitem endpoint /lamps/123 ومعناها مصباح واحد. كل خلية تقول ماذا تفعل تلك الطريقة بذلك العنوان.

Methodالطريقة/lamps/lamps/lamps/123/lamps/123JSON objectكائن JSON
GETGETList all lamps in the houseعرض قائمة بكل المصابيح في المنزلGet information about lamp 123الحصول على معلومات المصباح 123Returned in the response: {"id": 123, "Enabled": true, "ON": true, "brightness": 35}يعود في الاستجابة: {"id": 123, "Enabled": true, "ON": true, "brightness": 35}
DELETEDELETEDisable all lampsتعطيل كل المصابيحDisable lamp 123تعطيل المصباح 123Sent in the response: {"id": 123, "Enabled": false}يُرسَل في الاستجابة: {"id": 123, "Enabled": false}
PUTPUTChange brightness, turn on or off, or enable all lampsتغيير السطوع، أو التشغيل أو الإطفاء، أو تفعيل كل المصابيحChange brightness, turn on or off, or enable lamp 123تغيير السطوع، أو التشغيل أو الإطفاء، أو تفعيل المصباح 123Sent in the request: {"id": 123, "Enabled": true, "ON": true, "brightness": 60}يُرسَل في الطلب: {"id": 123, "Enabled": true, "ON": true, "brightness": 60}
POSTPOSTSearch for a newly installed lamp, assign it a unique ID, create its virtual resourceالبحث عن مصباح رُكِّب حديثًا، وإعطاؤه معرّفًا فريدًا (ID)، وإنشاء مورده الافتراضيNot applicableغير قابل للتطبيقReturned in the response: {"id": 123, "Enabled": true, "ON": false, "brightness": 0}يعود في الاستجابة: {"id": 123, "Enabled": true, "ON": false, "brightness": 0}
The lamp API from slide 23. The JSON column describes lamp 123. Header fields for every row: Cookie, authentication token, authorization token.API المصباح من الشريحة 23. عمود JSON يصف المصباح 123. حقول الترويسة في كل صف: Cookie، ورمز المصادقة (authentication token)، ورمز التفويض (authorization token).
Seven of the eight method and URL cells have a meaning; POST on a specific lamp does notسبع من الخلايا الثماني (طريقة و URL) لها معنى، أما POST على مصباح محدد فليس له معنى

Reading the table row by rowقراءة الجدول صفًا صفًا

  • GET reads and never changes anything. On /lamps it lists every lamp; on /lamps/123 it returns one lamp's state.GET يقرأ ولا يغيّر أي شيء أبدًا. على /lamps يعرض قائمة بكل المصابيح، وعلى /lamps/123 يعيد حالة مصباح واحد.
  • DELETE here means disable. The response {"id": 123, "Enabled": false} shows the lamp still exists, with its Enabled flag cleared. This is a soft delete: the record stays, so a PUT can enable the lamp again later. A physical lamp still screwed into the ceiling should not vanish from the system.DELETE هنا تعني التعطيل. الاستجابة {"id": 123, "Enabled": false} تُظهر أن المصباح ما زال موجودًا، مع إلغاء علامة Enabled. هذا حذف ناعم (soft delete): يبقى السجل، فيمكن لطلب PUT أن يعيد تفعيل المصباح لاحقًا. فالمصباح الحقيقي الذي ما زال مثبتًا في السقف لا ينبغي أن يختفي من النظام.
  • PUT sends the full new state in the request: {"id": 123, "Enabled": true, "ON": true, "brightness": 60}. Compared with the GET above, only brightness changed, from 35 to 60. Sending every field is what makes PUT a replacement, and why it is Idempotent method: sending the same body twice leaves the lamp at 60 both times.PUT يرسل الحالة الجديدة كاملة في الطلب: {"id": 123, "Enabled": true, "ON": true, "brightness": 60}. مقارنةً بـ GET أعلاه، لم يتغير إلا السطوع، من 35 إلى 60. إرسال كل الحقول هو ما يجعل PUT عملية استبدال، ولهذا هو idempotent: إرسال الجسم نفسه مرتين يترك المصباح على 60 في المرتين.
  • POST on /lamps asks the server to discover a newly installed lamp, assign it an ID and create its virtual resource. The response {"id": 123, "Enabled": true, "ON": false, "brightness": 0} carries the ID the server chose, and the new lamp starts off at zero brightness.POST على /lamps يطلب من الخادم أن يكتشف مصباحًا رُكِّب حديثًا، ويعطيه معرّفًا (ID)، وينشئ مورده الافتراضي. الاستجابة {"id": 123, "Enabled": true, "ON": false, "brightness": 0} تحمل المعرّف الذي اختاره الخادم، ويبدأ المصباح الجديد مطفأً بسطوع صفر.
  • POST on /lamps/123 is not applicable. Creating something at an ID that already names an existing lamp is contradictory: the client cannot pick the ID, and lamp 123 already exists.POST على /lamps/123 غير قابل للتطبيق. إنشاء شيء عند معرّف يسمّي أصلًا مصباحًا موجودًا أمر متناقض: العميل لا يستطيع اختيار المعرّف، والمصباح 123 موجود بالفعل.

The Header fields column is the same for every row: a Cookie, an Authentication token and an Authorization token. They do not change the operation; they tell the server who is asking and whether they may. You would not want a neighbour on the same Wi-Fi to switch off your lamps.

عمود header fields هو نفسه في كل صف: Cookie، وauthentication token، وauthorization token. هذه لا تغيّر العملية، بل تخبر الخادم من الذي يطلب وهل يُسمح له. فأنت لا تريد أن يطفئ جارك المتصل بشبكة Wi-Fi نفسها مصابيحك.

Quick checkاختبار سريع

A homeowner installs a new lamp and wants the system to register it. Which route matches the slide's table?ركّب صاحب المنزل مصباحًا جديدًا ويريد أن يسجّله النظام. أي مسار يطابق جدول الشريحة؟

Recall

Which lamp JSON is sent in a request, and which come back in responses?أي JSON للمصباح يُرسَل في الطلب، وأيها يعود في الاستجابات؟

Only the PUT JSON is sent in the request (the new state, brightness 60). GET, DELETE and POST JSON come back in responses: the current state, the disabled state, and the newly created lamp with its assigned ID.

فقط JSON الخاص بـ PUT يُرسَل في الطلب (الحالة الجديدة، السطوع 60). أما JSON الخاص بـ GET وDELETE وPOST فيعود في الاستجابات: الحالة الحالية، والحالة المعطَّلة، والمصباح المنشأ حديثًا مع المعرّف الذي أُعطي له.

Take one cell of the lamp table, "change the brightness of lamp 123", and ask what a developer needs to implement it. Which URL? Which method? What comes back if it works or fails? What metadata travels with it? What does the data look like? Those five questions are the REST design elements.

خذ خلية واحدة من جدول المصباح، "تغيير سطوع المصباح 123"، واسأل ماذا يحتاج المطوّر لتنفيذها. أي URL؟ أي طريقة؟ ماذا يعود إذا نجحت أو فشلت؟ ما البيانات الوصفية التي ترافقها؟ كيف تبدو البيانات؟ هذه الأسئلة الخمسة هي REST design elements (عناصر تصميم REST).

ElementالعنصرWhat it holdsما يحملهLamp exampleمثال المصباحQuestion it answersالسؤال الذي يجيب عنه
1. URL1. URLDomain name + pathاسم النطاق (domain) + المسار (path)http://myHome.com/lamps/123http://myHome.com/lamps/123Which resource?أي مورد؟
2. HTTP request method2. طريقة طلب HTTP (request method)GET, POST, PUT, DELETEGET, POST, PUT, DELETEPUTPUTWhat to do with it?ماذا نفعل به؟
3. HTTP response status code3. رمز حالة استجابة HTTP (status code)100 to 599من 100 إلى 599200 OK200 OKDid it work, and if not, whose fault?هل نجح الطلب، وإن لم ينجح فخطأ من؟
4. Header fields4. حقول الترويسة (header fields)Metadata: cookies, tokens, Content-Typeبيانات وصفية: ملفات تعريف الارتباط (cookies)، والرموز (tokens)، وContent-TypeContent-Type: application/jsonContent-Type: application/jsonWhat surrounds the message?ما الذي يحيط بالرسالة؟
5. JSON message5. رسالة JSONRepresentation of a resource's state or requested changeتمثيل حالة المورد أو التغيير المطلوب فيه{"id": 123, "Enabled": true, "ON": true, "brightness": 60}{"id": 123, "Enabled": true, "ON": true, "brightness": 60}What is the data?ما البيانات؟
The five design elements, each filled in for the PUT on lamp 123عناصر التصميم الخمسة، وكل منها مملوء لطلب PUT على المصباح 123

The URL names the resource: a domain name, which locates the server, plus a path, which locates the resource on it. The HTTP method names the operation. The Status code reports the outcome with a three-digit number. The header fields carry metadata about the message rather than the resource, such as cookies, tokens and Content-Type. The JSON message carries the resource representation itself: either its current state, in a response, or the change you want, in a request.

الـ URL يسمّي المورد: اسم نطاق (domain) يحدد مكان الخادم، ومسار (path) يحدد مكان المورد عليه. وHTTP method تسمّي العملية. وstatus code يبلّغ عن النتيجة برقم من ثلاث خانات. وحقول الترويسة تحمل بيانات وصفية عن الرسالة وليس عن المورد، مثل ملفات تعريف الارتباط (cookies) والرموز (tokens) وContent-Type. ورسالة JSON تحمل تمثيل المورد نفسه: إما حالته الحالية في الاستجابة، أو التغيير الذي تريده في الطلب.

Status codes: the first digit is the classرموز الحالة: الخانة الأولى هي الفئة

RFC 9110 states that all valid status codes lie between 100 and 599 inclusive, and that only the first digit carries a category. The last two digits pick a specific meaning inside the class. When you design an API, you are mostly choosing which 2xx, 4xx and 5xx codes it can return.

ينص RFC 9110 على أن كل رموز الحالة الصالحة تقع بين 100 و599 شاملةً، وأن الخانة الأولى وحدها تحمل الفئة. أما الخانتان الأخيرتان فتختاران معنى محددًا داخل الفئة. عندما تصمم API، فأنت في الغالب تختار أي رموز 2xx و4xx و5xx يمكن أن يعيدها.

ClassالفئةNameالاسمMeaningالمعنى
1xx1xxInformationalمعلوماتية (Informational)Request received, processing continuesاستُلم الطلب، والمعالجة مستمرة
2xx2xxSuccessfulناجحة (Successful)Received, understood and acceptedاستُلم الطلب وفُهم وقُبل
3xx3xxRedirectionإعادة توجيه (Redirection)The client must take further actionعلى العميل أن يتخذ إجراءً إضافيًا
4xx4xxClient errorخطأ العميل (Client error)Bad syntax or cannot be fulfilled; the client should change the requestصياغة خاطئة أو لا يمكن تلبيته، وعلى العميل أن يغيّر الطلب
5xx5xxServer errorخطأ الخادم (Server error)A valid request the server failed to fulfilطلب صالح فشل الخادم في تلبيته
The five status code classes (RFC 9110, section 15)فئات رموز الحالة الخمس (RFC 9110، القسم 15)

Recall

Name the five design elements and give a one-word role for each.اذكر عناصر التصميم الخمسة وأعطِ لكل منها دورًا بكلمة واحدة.

URL (where), HTTP request method (what action), HTTP response status code (outcome), header fields (metadata), JSON message (data, the resource representation).

URL (أين)، وHTTP request method (أي فعل)، وHTTP response status code (النتيجة)، وheader fields (بيانات وصفية)، وJSON message (البيانات، أي تمثيل المورد).

The second example is a web page titled "My Awesome Calculator!". It has a box for X, a box for Y, four radio buttons (addition, subtraction, multiplication, division) and a Submit button. The task: design a REST API for the addition operation, so that the sum is computed on the server, not in the browser.

المثال الثاني صفحة ويب عنوانها "My Awesome Calculator!". فيها خانة لـ X، وخانة لـ Y، وأربعة أزرار اختيار (جمع، طرح، ضرب، قسمة)، وزر Submit. المهمة: صمّم REST API لعملية الجمع، بحيث يُحسب المجموع على الخادم وليس في المتصفح.

Work through the five elements in order. The URL needs a domain and a path, so the slide picks http://calculator.com/add. Adding two numbers reads data and changes nothing, so the method is GET. The inputs travel as JSON, {"X": 1, "Y": 2}. The caller must be allowed to use the service, so the request carries an Authorization header. Finally, list the status codes that the server can return.

مرّ على العناصر الخمسة بالترتيب. الـ URL يحتاج نطاقًا ومسارًا، فتختار الشريحة http://calculator.com/add. جمع عددين يقرأ بيانات ولا يغيّر شيئًا، فالطريقة هي GET. المدخلات تنتقل بصيغة JSON، {"X": 1, "Y": 2}. يجب أن يكون المستدعي مسموحًا له باستخدام الخدمة، لذلك يحمل الطلب ترويسة Authorization. وأخيرًا، اذكر رموز الحالة التي يمكن أن يعيدها الخادم.

The calculator addition API, exactly as on slide 26API الجمع في الآلة الحاسبة، تمامًا كما في الشريحة 26

URL
http://calculator.com/addhttp://calculator.com/add
HTTP request methodطريقة طلب HTTP
GETGET
HTTP response status codeرمز حالة استجابة HTTP
200 (successful), 400 (bad request), 406 (not acceptable), 500 (internal server error), 503 (service unavailable)200 (نجاح، successful)، و400 (طلب خاطئ، bad request)، و406 (غير مقبول، not acceptable)، و500 (خطأ داخلي في الخادم، internal server error)، و503 (الخدمة غير متاحة، service unavailable)
Header fieldsحقول الترويسة
AuthorizationAuthorization
JSON messageرسالة JSON
{"X": 1, "Y": 2}{"X": 1, "Y": 2}

On the server, the request is unpacked into a small environment: X = 1, Y = 2, op = Addition. The handler then runs Resp.send(X op Y) and the client receives 3. The browser never adds anything; it only sends inputs and displays an answer. This is the client-server split from Lecture 1: the computation lives behind the API.

على الخادم، يُفكّ الطلب إلى بيئة صغيرة: X = 1، Y = 2، op = Addition. ثم يشغّل المعالج Resp.send(X op Y) ويستقبل العميل 3. المتصفح لا يجمع أي شيء، بل يرسل المدخلات ويعرض الإجابة فقط. هذا هو الفصل بين العميل والخادم (client-server) من المحاضرة 1: الحساب يعيش خلف الـ API.

The JSON inputs become an environment on the server, which computes X op Y and sends back 3مدخلات JSON تصبح بيئة على الخادم، فيحسب X op Y ويعيد 3

Each status code, with the request that triggers itكل رمز حالة مع الطلب الذي يسببه

A list of codes is easy to memorise and easy to confuse. Tie each to a concrete calculator request instead. The question to ask every time is: whose fault is it? A 4xx says the client must change the request. A 5xx says the request was fine and the server failed.

قائمة الرموز سهلة الحفظ وسهلة الخلط. لذلك اربط كل رمز بطلب محدد للآلة الحاسبة. والسؤال الذي تطرحه كل مرة: خطأ من هذا؟ الرمز 4xx يقول إن على العميل أن يغيّر الطلب. والرمز 5xx يقول إن الطلب كان سليمًا والخادم هو الذي فشل.

CodeالرمزRFC 9110 meaningالمعنى في RFC 9110Calculator triggerما يسببه في الآلة الحاسبةWhose faultخطأ من
200 OK200 OKThe request succeededنجح الطلبX = 1, Y = 2, server healthy, result 3X = 1، Y = 2، الخادم سليم، والنتيجة 3Nobodyلا أحد
400 Bad Request400 Bad RequestThe server will not process it because of a client errorلن يعالجه الخادم بسبب خطأ من العميلX is missing, or X is "abc"X مفقود، أو قيمة X هي "abc"Clientالعميل
406 Not Acceptable406 Not AcceptableNo representation matches the client's Accept headers, and the server will not send a defaultلا يوجد تمثيل يطابق ترويسات Accept لدى العميل، والخادم لن يرسل صيغة افتراضيةAccept: application/xml to a JSON-only calculatorAccept: application/xml إلى آلة حاسبة لا تعيد إلا JSONClientالعميل
500 Internal Server Error500 Internal Server ErrorAn unexpected condition on the serverحالة غير متوقعة على الخادمAn exception in the addition code, a null reference, a failed database callاستثناء (exception) في كود الجمع، أو مرجع فارغ (null reference)، أو فشل استدعاء قاعدة البياناتServerالخادم
503 Service Unavailable503 Service UnavailableTemporary overload or maintenance; may carry Retry-Afterحمل زائد مؤقت أو صيانة، وقد يحمل ترويسة Retry-AfterToo many requests at once, or the server is being upgradedطلبات كثيرة جدًا في وقت واحد، أو الخادم قيد الترقيةServerالخادم
The slide's five status codes, with RFC 9110 meanings and calculator triggersرموز الحالة الخمسة في الشريحة، مع معانيها في RFC 9110 والطلبات التي تسببها في الآلة الحاسبة

406 is the subtle one. It is about the response format. The client's Accept header lists the media types it can read. If the client says it will only take application/xml and the calculator only speaks JSON, the server cannot produce an acceptable answer. MDN notes that sending a default format with 200 may be preferable to a 406, so using it is a design choice: strict servers use it, lenient ones ignore the mismatch.

الرمز 406 هو الأدق. إنه يتعلق بصيغة الاستجابة. ترويسة Accept لدى العميل تسرد أنواع الوسائط (media types) التي يستطيع قراءتها. إذا قال العميل إنه لن يقبل إلا application/xml والآلة الحاسبة لا تتكلم إلا JSON، فلا يستطيع الخادم إنتاج إجابة مقبولة. وتشير MDN إلى أن إرسال صيغة افتراضية مع 200 قد يكون أفضل من 406، فاستخدامه قرار تصميمي: الخوادم الصارمة تستخدمه، والمتساهلة تتجاهل عدم التطابق.

Why the slide's GET with a body is shakyلماذا يُعد GET مع جسم في الشريحة تصميمًا مهزوزًا

The slide pairs GET with a JSON body. RFC 9110 is explicit that content received in a GET request has no generally defined semantics, cannot change the meaning of the request, and may cause some implementations to reject the request. A client should not send one unless the origin server has said it will support it. In practice, the browser's fetch throws an error if you give a GET request a body, and caches key stored responses on the method and URL, never the body, so two different sums sent to the same URL could collide in a cache.

تجمع الشريحة بين GET وجسم JSON. يصرّح RFC 9110 بأن المحتوى المستلَم في طلب GET ليس له دلالة معرّفة بشكل عام، ولا يمكنه تغيير معنى الطلب، وقد يجعل بعض التطبيقات ترفض الطلب. ولا ينبغي للعميل أن يرسله ما لم يعلن الخادم الأصلي أنه يدعمه. وعمليًا، دالة fetch في المتصفح ترمي خطأ إذا أعطيت طلب GET جسمًا، والذاكرات المؤقتة (caches) تفهرس الاستجابات المخزنة حسب الطريقة والـ URL وليس حسب الجسم أبدًا، لذلك قد يتصادم مجموعان مختلفان أُرسلا إلى الـ URL نفسه داخل الذاكرة المؤقتة.

DesignالتصميمRequestالطلبStrengthنقطة القوةWeaknessنقطة الضعف
Slide designتصميم الشريحةGET /add with body {"X": 1, "Y": 2}GET /add مع الجسم {"X": 1, "Y": 2}Matches the exam tableيطابق جدول الامتحانBody has no defined meaning for GET; proxies and libraries may drop or reject itالجسم ليس له معنى محدد مع GET، وقد تُسقطه الوسائط (proxies) والمكتبات أو ترفضه
GET with a query stringGET مع سلسلة استعلام (query string)GET /add?x=1&y=2GET /add?x=1&y=2Safe, cacheable, bookmarkable; inputs are part of the URLآمن، وقابل للتخزين المؤقت، ويمكن حفظه كإشارة مرجعية (bookmark)، والمدخلات جزء من الـ URLInputs visible in logs and history; URL length limits for large inputsالمدخلات ظاهرة في السجلات (logs) وسجل التصفح، وحدود طول الـ URL تقيّد المدخلات الكبيرة
POST with a JSON bodyPOST مع جسم JSONPOST /add with body {"X": 1, "Y": 2}POST /add مع الجسم {"X": 1, "Y": 2}Body has a defined meaning; handles large or structured inputsللجسم معنى محدد، ويتعامل مع المدخلات الكبيرة أو المنظَّمةNot cached by default; POST signals possible side effects that addition does not haveلا يُخزَّن مؤقتًا افتراضيًا، وPOST يوحي بآثار جانبية محتملة ليست موجودة في عملية الجمع
Three ways to send the inputsثلاث طرق لإرسال المدخلات

Try every case in the builder. Start with the slide's design and the answer 3. Switch Accept to XML and watch the 406. Clear X, or type abc, for a 400. Mark the server as buggy or overloaded for 500 and 503. Then switch to GET with a query string and to POST and see how the request line and headers change while the five elements stay the same.

جرّب كل حالة في أداة البناء. ابدأ بتصميم الشريحة والإجابة 3. غيّر Accept إلى XML وشاهد 406. امسح X، أو اكتب abc، لتحصل على 400. اجعل الخادم معطوبًا أو محمّلًا فوق طاقته لتحصل على 500 و503. ثم انتقل إلى GET مع سلسلة استعلام وإلى POST، وشاهد كيف يتغير سطر الطلب والترويسات بينما تبقى العناصر الخمسة كما هي.

SimulatorمحاكيREST design builder for the calculatorباني تصميم REST للآلة الحاسبة
Operationالعملية
Method and inputsالطريقة والمدخلات
Accept headerترويسة Accept
AuthorizationAuthorization
Server stateحالة الخادم
Requestالطلب
GET /add HTTP/1.1
Host: calculator.com
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.demo
Content-Type: application/json
Content-Length: 13

{"X":1,"Y":2}
Responseالاستجابة
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 12

{"result":3}
200 OK

The server builds the environment X = 1, Y = 2, op = Addition, computes 1 + 2 = 3, and returns it as JSON with 200.يبني الخادم البيئة X = 1, Y = 2, op = Addition، ويحسب 1 + 2 = 3، ثم يعيد الناتج بصيغة JSON مع الرمز 200.

1. URL1. URLhttp://calculator.com/add
2. Method2. الطريقةGET
3. Status code3. رمز الحالة200 OK
4. Header fields4. حقول الترويسةHost, Accept, Authorization, Content-Type, Content-Length
5. JSON message5. رسالة JSONrequest {"X":1,"Y":2}, response {"result":3}الطلب {"X":1,"Y":2}، والاستجابة {"result":3}

This is the slide's design. RFC 9110 says content in a GET request has no generally defined semantics, and some proxies or client libraries drop or reject it, so the server may never see X and Y.هذا هو تصميم الشريحة. يقول RFC 9110 إن المحتوى في طلب GET ليس له معنى معرّف بشكل عام، وبعض الخوادم الوسيطة (proxies) أو مكتبات العميل تحذفه أو ترفضه، فقد لا يرى الخادم X وY أبدًا.

Quick checkاختبار سريع

A client sends Accept: application/xml to the calculator, which only produces JSON. Which response fits?يرسل عميل Accept: application/xml إلى الآلة الحاسبة التي لا تنتج إلا JSON. أي استجابة تناسب؟

Quick checkاختبار سريع

Why is the slide's GET request with a JSON body a shaky design?لماذا يُعد طلب GET مع جسم JSON في الشريحة تصميمًا مهزوزًا؟

Recall

Give a calculator request that triggers each of 400, 406, 500 and 503.أعطِ طلبًا للآلة الحاسبة يسبب كلًا من 400 و406 و500 و503.

400: X missing or "abc". 406: Accept: application/xml to a JSON-only server. 500: a bug in the server's addition code. 503: the server is overloaded or in maintenance, possibly with a Retry-After header.

400: X مفقود أو قيمته "abc". 406: Accept: application/xml إلى خادم لا يعيد إلا JSON. 500: خلل في كود الجمع على الخادم. 503: الخادم محمّل فوق طاقته أو في صيانة، وربما مع ترويسة Retry-After.

The slide designs addition and stops. The natural exam follow-up is: design the same API for subtraction, multiplication or division. The good news is that four of the five elements barely change. The path names the new operation, and the JSON carries new numbers. Division adds one new failure case.

تصمم الشريحة عملية الجمع وتتوقف. والسؤال الطبيعي التالي في الامتحان: صمّم الـ API نفسه للطرح أو الضرب أو القسمة. والخبر الجيد أن أربعة من العناصر الخمسة بالكاد تتغير. المسار يسمّي العملية الجديدة، والـ JSON يحمل أعدادًا جديدة. والقسمة تضيف حالة فشل جديدة واحدة.

Worked exampleمثال محلول

Designing the division APIتصميم API القسمة

  1. URLالـ URL

    Same domain, new path: http://calculator.com/divide. With a query string the full request target is /divide?x=6&y=3.

    النطاق نفسه ومسار جديد: http://calculator.com/divide. ومع سلسلة استعلام يصبح هدف الطلب الكامل /divide?x=6&y=3.

  2. Methodالطريقة

    Division reads inputs and changes no server state, so GET, exactly as for addition. With inputs in the query string, GET is also safe and cacheable.

    القسمة تقرأ مدخلات ولا تغيّر أي حالة على الخادم، لذلك GET، تمامًا كما في الجمع. ومع وضع المدخلات في سلسلة الاستعلام، يصبح GET أيضًا آمنًا وقابلًا للتخزين المؤقت.

  3. Compute the success caseاحسب حالة النجاح

    X = 6, Y = 3, op = Division, so 6 ÷ 3 = 2. The server replies 200 OK with {"result": 2}.

    X = 6، Y = 3، op = Division، إذن 6 ÷ 3 = 2. يرد الخادم بـ 200 OK مع {"result": 2}.

  4. Find the new failureجد حالة الفشل الجديدة

    GET /divide?x=6&y=0 is well formed, but 6 ÷ 0 has no value. The fault lies in the client's input, so the server returns 400 Bad Request with {"error": "Division by zero is undefined"}, never 500.

    GET /divide?x=6&y=0 طلب سليم البنية، لكن 6 ÷ 0 ليس له قيمة. الخطأ في مدخلات العميل، لذلك يعيد الخادم 400 Bad Request مع {"error": "Division by zero is undefined"}، وليس 500 أبدًا.

  5. Headers and the rest of the codesالترويسات وبقية الرموز

    Authorization for access, Accept: application/json for the response format. The codes 406, 500 and 503 keep the same triggers as for addition.

    Authorization للوصول، وAccept: application/json لصيغة الاستجابة. والرموز 406 و500 و503 تبقى بأسبابها نفسها كما في الجمع.

  6. Resultالنتيجة

    Division is addition's design with path /divide, plus one extra 400 trigger: Y = 0.
    القسمة هي تصميم الجمع مع المسار /divide، بالإضافة إلى سبب إضافي واحد لـ 400: Y = 0.

Division API in the slide's table format, query string versionAPI القسمة بصيغة جدول الشريحة، نسخة سلسلة الاستعلام (query string)

URL
http://calculator.com/divide?x=6&y=3http://calculator.com/divide?x=6&y=3
HTTP request methodطريقة طلب HTTP
GETGET
HTTP response status codeرمز حالة استجابة HTTP
200, 400 (missing or non-numeric input, or y = 0), 406, 500, 503200، و400 (مدخلات مفقودة أو غير رقمية، أو y = 0)، و406، و500، و503
Header fieldsحقول الترويسة
Authorization, Accept: application/jsonAuthorization, Accept: application/json
JSON messageرسالة JSON
Response {"result": 2}, or {"error": "Division by zero is undefined"}الاستجابة {"result": 2}، أو {"error": "Division by zero is undefined"}
OperationالعمليةURLURLMethodالطريقةStatus codesرموز الحالةHeader fieldsحقول الترويسةJSON messageرسالة JSONResultالنتيجة
Addition (slide)الجمع (الشريحة)http://calculator.com/addhttp://calculator.com/addGETGET200, 400, 406, 500, 503200, 400, 406, 500, 503AuthorizationAuthorization{"X": 1, "Y": 2}{"X": 1, "Y": 2}1 + 2 = 31 + 2 = 3
Subtractionالطرحhttp://calculator.com/subtracthttp://calculator.com/subtractGETGET200, 400, 406, 500, 503200, 400, 406, 500, 503AuthorizationAuthorization{"X": 5, "Y": 2}{"X": 5, "Y": 2}5 - 2 = 35 - 2 = 3
Multiplicationالضربhttp://calculator.com/multiplyhttp://calculator.com/multiplyGETGET200, 400, 406, 500, 503200, 400, 406, 500, 503AuthorizationAuthorization{"X": 4, "Y": 3}{"X": 4, "Y": 3}4 × 3 = 124 × 3 = 12
Divisionالقسمةhttp://calculator.com/dividehttp://calculator.com/divideGETGET200, 400 (also Y = 0), 406, 500, 503200، 400 (وأيضًا Y = 0)، 406، 500، 503AuthorizationAuthorization{"X": 6, "Y": 3}{"X": 6, "Y": 3}6 ÷ 3 = 26 ÷ 3 = 2
All four operations in the slide's style (GET with a JSON message). Swap in a query string or POST as discussed above.العمليات الأربع كلها بأسلوب الشريحة (GET مع رسالة JSON). يمكنك استبدالها بسلسلة استعلام أو POST كما نوقش أعلاه.

The slide closes with references. The deck cites Henry Cocos's Cloud Computing lecture series (summer term 2025), four cloud textbooks and uptime.is, an availability calculator from Lecture 2 that this lecture does not use. They appear under Sources below, next to the standards that back the status code and GET claims in this part.

تُختَم الشريحة بالمراجع. يستشهد العرض بسلسلة محاضرات Cloud Computing لـ Henry Cocos (الفصل الصيفي 2025)، وأربعة كتب في الحوسبة السحابية، وuptime.is، وهي حاسبة توافر (availability) من المحاضرة 2 لا تستخدمها هذه المحاضرة. تظهر كلها تحت المصادر أدناه، بجانب المعايير التي تدعم ادعاءات رموز الحالة وGET في هذا الجزء.

Recall

Design the multiplication API in the slide's format for X = 4, Y = 3.صمّم API الضرب بصيغة الشريحة لـ X = 4 وY = 3.

URL http://calculator.com/multiply; method GET; status codes 200, 400, 406, 500, 503; header Authorization; JSON {"X": 4, "Y": 3}. The server computes 4 × 3 = 12 and returns it with 200.

URL هو http://calculator.com/multiply، والطريقة GET، ورموز الحالة 200 و400 و406 و500 و503، والترويسة Authorization، والـ JSON هو {"X": 4, "Y": 3}. يحسب الخادم 4 × 3 = 12 ويعيده مع 200.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • The lamp system: browser UI, HTTP request, server (a Raspberry Pi), signal to the lamp with ID 123.نظام المصباح: واجهة المتصفح، ثم طلب HTTP، ثم الخادم (Raspberry Pi)، ثم إشارة إلى المصباح ذي المعرّف 123.
  • /lamps is the collection, /lamps/123 one item. POST creates on the collection only; POST on /lamps/123 is not applicable./lamps هي المجموعة و/lamps/123 عنصر واحد. POST ينشئ على المجموعة فقط، أما POST على /lamps/123 فغير قابل للتطبيق.
  • DELETE disables the lamp (Enabled: false) instead of erasing it: a soft delete. PUT sends the full new state.DELETE يعطّل المصباح (Enabled: false) بدل محوه: حذف ناعم (soft delete). أما PUT فيرسل الحالة الجديدة كاملة.
  • Five design elements: URL (domain + path), method, status code (100 to 599), header fields (metadata), JSON message (representation).عناصر التصميم الخمسة: URL (النطاق + المسار)، والطريقة (method)، ورمز الحالة (من 100 إلى 599)، وحقول الترويسة (بيانات وصفية)، ورسالة JSON (التمثيل).
  • Calculator: http://calculator.com/add, GET, codes 200, 400, 406, 500, 503, Authorization header, JSON {"X": 1, "Y": 2}, result 3.الآلة الحاسبة: http://calculator.com/add، وGET، والرموز 200 و400 و406 و500 و503، وترويسة Authorization، وJSON {"X": 1, "Y": 2}، والنتيجة 3.
  • 4xx means the client is at fault (400 bad input, 406 unproducible format). 5xx means the server is (500 bug, 503 overloaded).4xx تعني أن الخطأ من العميل (400 مدخلات خاطئة، 406 صيغة لا يمكن إنتاجها). 5xx تعني أن الخطأ من الخادم (500 خلل برمجي، 503 حمل زائد).
  • GET with a body has no defined meaning (RFC 9110). Use GET /add?x=1&y=2 or POST /add with the JSON body.GET مع جسم ليس له معنى محدد (RFC 9110). استخدم GET /add?x=1&y=2 أو POST /add مع جسم JSON.

Sourcesالمصادر