Majid Al-RaimiDesigning endpoints: collections, items and query stringsتصميم نقاط النهاية: المجموعات والعناصر وسلاسل الاستعلام

COE 558Lecture 03المحاضرة 03Part 03الجزء 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 بوصفها مدخلات.

Conceptsالمفاهيم
3
Slidesالشرائح
13-16
Readingمدة القراءة
18 min18 دقيقة
Understoodما فهمته
0/3 conceptsمفاهيم

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