from flask import Flask, jsonify

server = Flask(__name__)


def get_relays(relays):
    return jsonify({"relays": relays}), 200


def get_relay(relays, relay_name):
    code = 200
    try:
        relay = relays[relay_name]
    except KeyError:
        code = 404
        return "", code

    return jsonify({"relay": relay}), code

from flask import Flask, jsonify
# import json

server = Flask(__name__)


def __serialize_relay(relays):
    if type(relays).__name__ == "relay":
        return jsonify({"gpio": relays.gpio,
                           "NC": relays.nc,
                           "state": relays.state})
    di = {}
    for r in relays:
        di[r] = {"gpio": relays[r].gpio,
                 "NC": relays[r].nc,
                 "state": relays[r].state}
    return jsonify(di)


def get_relays(relays_dict):
    return __serialize_relay(relays_dict), 200


def get_relay(relay):
    code = 200
    if not relay:
        code = 404
        return "", code
    return __serialize_relay(relay), code

Change to get a dict with the relay instances
Change to get a dict with the relay instances

  from flask import Flask, jsonify
+ # import json
  
  server = Flask(__name__)
  
  
- def get_relays(relays):
+ def __serialize_relay(relays):
-     return jsonify({"relays": relays}), 200
+     if type(relays).__name__ == "relay":
+         return jsonify({"gpio": relays.gpio,
+                            "NC": relays.nc,
+                            "state": relays.state})
+     di = {}
+     for r in relays:
+         di[r] = {"gpio": relays[r].gpio,
+                  "NC": relays[r].nc,
+                  "state": relays[r].state}
+     return jsonify(di)
  
  
+ def get_relays(relays_dict):
+     return __serialize_relay(relays_dict), 200
+ 
+ 
- def get_relay(relays, relay_name):
+ def get_relay(relay):
      code = 200
+     if not relay:
-     try:
-         relay = relays[relay_name]
-     except KeyError:
          code = 404
          return "", code
+     return __serialize_relay(relay), code
  
-     return jsonify({"relay": relay}), code
- 
Change to get a dict with the relay instances
## Code Before:
from flask import Flask, jsonify

server = Flask(__name__)


def get_relays(relays):
    return jsonify({"relays": relays}), 200


def get_relay(relays, relay_name):
    code = 200
    try:
        relay = relays[relay_name]
    except KeyError:
        code = 404
        return "", code

    return jsonify({"relay": relay}), code

## Instruction:
Change to get a dict with the relay instances
## Code After:
from flask import Flask, jsonify
# import json

server = Flask(__name__)


def __serialize_relay(relays):
    if type(relays).__name__ == "relay":
        return jsonify({"gpio": relays.gpio,
                           "NC": relays.nc,
                           "state": relays.state})
    di = {}
    for r in relays:
        di[r] = {"gpio": relays[r].gpio,
                 "NC": relays[r].nc,
                 "state": relays[r].state}
    return jsonify(di)


def get_relays(relays_dict):
    return __serialize_relay(relays_dict), 200


def get_relay(relay):
    code = 200
    if not relay:
        code = 404
        return "", code
    return __serialize_relay(relay), code
