Bird
Raised Fist0
IOT Protocolsdevops~30 mins

RESTful API design for devices in IOT Protocols - Mini Project: Build & Apply

Choose your learning style10 modes available
RESTful API Design for Devices
📖 Scenario: You are working for a smart home company. You need to create a simple RESTful API design to manage devices like lights and thermostats. This API will help the mobile app control these devices remotely.
🎯 Goal: Build a basic RESTful API design using Python Flask that can list devices, add a new device, update device status, and show device details.
📋 What You'll Learn
Create a dictionary called devices with initial device data
Add a configuration variable allowed_statuses listing valid device states
Implement API endpoints for GET all devices, POST new device, PUT update device status, and GET device by ID
Print the JSON response of the GET all devices endpoint
💡 Why This Matters
🌍 Real World
Smart home systems use RESTful APIs to let apps control devices like lights and thermostats remotely.
💼 Career
Understanding RESTful API design is essential for IoT developers and DevOps engineers managing device communication and automation.
Progress0 / 4 steps
1
Create initial device data
Create a dictionary called devices with these exact entries: 1 mapped to {'name': 'Living Room Light', 'status': 'off'} and 2 mapped to {'name': 'Thermostat', 'status': 'on'}.
IOT Protocols
Hint

Use a dictionary with integer keys 1 and 2. Each value is another dictionary with keys 'name' and 'status'.

2
Add allowed device statuses configuration
Add a list called allowed_statuses containing the strings 'on' and 'off'.
IOT Protocols
Hint

Define a list with two strings: 'on' and 'off'.

3
Implement core RESTful API endpoints
Using Flask, create an app with these endpoints: @app.route('/devices', methods=['GET']) to return all devices as JSON, @app.route('/devices', methods=['POST']) to add a new device with JSON body containing id, name, and status (only if status is in allowed_statuses), @app.route('/devices/<int:device_id>', methods=['PUT']) to update the status of a device by device_id, and @app.route('/devices/<int:device_id>', methods=['GET']) to get device details by device_id. Use Flask's request and jsonify. Assume from flask import Flask, request, jsonify and app = Flask(__name__) are already defined.
IOT Protocols
Hint

Use Flask decorators for routes. Use request.get_json() to get JSON data. Validate status against allowed_statuses. Return JSON responses with jsonify().

4
Print all devices JSON output
Write a line to print the JSON string of the devices dictionary using Flask's jsonify and app.test_request_context() to simulate a request context. Print the JSON string of all devices.
IOT Protocols
Hint

Use app.test_request_context() to create a context, then call get_devices() and print its data as text.

Practice

(1/5)
1. Which HTTP method is typically used to retrieve data from a device in a RESTful API?
easy
A. DELETE
B. GET
C. PUT
D. POST

Solution

  1. Step 1: Understand HTTP methods in REST

    GET is used to request data without changing it, ideal for reading device info.
  2. Step 2: Match method to action

    Since retrieving data means reading, GET fits best over POST, PUT, or DELETE.
  3. Final Answer:

    GET -> Option B
  4. Quick Check:

    Retrieve data = GET [OK]
Hint: GET always fetches data without changing it [OK]
Common Mistakes:
  • Confusing POST with GET for data retrieval
  • Using DELETE to get data
  • Thinking PUT retrieves data
2. Which of the following is the correct RESTful URL to update the settings of a device with ID 123?
easy
A. /update/device/123
B. /device/123/update
C. /devices/settings/123
D. /devices/123/settings

Solution

  1. Step 1: Identify RESTful URL structure

    RESTful URLs use nouns and hierarchy: resource then ID then sub-resource.
  2. Step 2: Check options for correct pattern

    /devices/123/settings correctly shows device resource, ID, then settings sub-resource.
  3. Final Answer:

    /devices/123/settings -> Option D
  4. Quick Check:

    Resource/ID/sub-resource = /devices/123/settings [OK]
Hint: Use resource/ID/sub-resource format for REST URLs [OK]
Common Mistakes:
  • Placing ID after sub-resource
  • Using verbs like 'update' in URL
  • Mixing singular and plural resource names
3. Given this RESTful API call: DELETE /devices/456, what is the expected result?
medium
A. Remove device 456 from the system
B. Update device 456 information
C. Retrieve device 456 details
D. Create a new device with ID 456

Solution

  1. Step 1: Understand DELETE method purpose

    DELETE removes the specified resource from the server.
  2. Step 2: Apply to device 456

    DELETE /devices/456 means remove device with ID 456 from the system.
  3. Final Answer:

    Remove device 456 from the system -> Option A
  4. Quick Check:

    DELETE method = remove resource [OK]
Hint: DELETE removes resources, not fetch or update [OK]
Common Mistakes:
  • Thinking DELETE retrieves data
  • Confusing DELETE with POST or PUT
  • Assuming DELETE creates resources
4. You wrote this API call to update a device: POST /devices/789 but it creates a new device instead. What is the likely mistake?
medium
A. Using POST instead of PUT for update
B. Missing device ID in URL
C. Using GET method instead of POST
D. Incorrect device ID format

Solution

  1. Step 1: Understand POST vs PUT

    POST usually creates new resources; PUT updates existing ones.
  2. Step 2: Analyze the API call

    POST /devices/789 creates new device; to update, use PUT /devices/789.
  3. Final Answer:

    Using POST instead of PUT for update -> Option A
  4. Quick Check:

    Update needs PUT, not POST [OK]
Hint: Use PUT to update, POST to create [OK]
Common Mistakes:
  • Using POST for updates
  • Confusing GET with POST
  • Ignoring HTTP method meaning
5. You want to design a RESTful API to control multiple devices' power state at once. Which design is best?
hard
A. PUT /device/{id}/power for each device separately
B. GET /devices/power to fetch all power states
C. POST /devices/power with JSON list of device IDs and states
D. DELETE /devices/power to reset all devices

Solution

  1. Step 1: Identify batch update needs

    To control many devices at once, send a batch request with data.
  2. Step 2: Match HTTP method and URL

    POST /devices/power with JSON list allows updating multiple devices in one call.
  3. Step 3: Evaluate other options

    GET fetches data, PUT is per device, DELETE resets devices--none fit batch control well.
  4. Final Answer:

    POST /devices/power with JSON list of device IDs and states -> Option C
  5. Quick Check:

    Batch update = POST with data list [OK]
Hint: Use POST with data list for batch device updates [OK]
Common Mistakes:
  • Using GET to change device states
  • Trying PUT for batch updates
  • Using DELETE to control power
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard