Ejemplo n.º 1
0
async def device_by_id(device_id: str) -> Device:
    """Similar to byserial but does lookup by Device ID"""
    url = f"hardware/bytag/{device_id}"
    device = await inventory.response(url)

    if device.get("status", "") == "error":
        raise CustomException(errors=[device["messages"]], status_code=404)

    return Device.serialize(device)
Ejemplo n.º 2
0
async def device_by_serial(serial: str) -> Optional[Device]:
    """Retrieve metadata about a device based on its serial code."""
    url = f"hardware/byserial/{serial}"
    res = await inventory.response(url)
    rows = res["rows"]

    if len(rows) == 0:
        raise CustomException(errors=["No device with that code."],
                              status_code=404)

    # Note: multiple devices may exist with same serial,
    # e.g. DRMDAX2S4 and DRM-DAX2S4. As such, sort by use.
    # TODO: typing throws as `rows` is not promised to be a `SupportsLessThan` Iterable
    sorted_rows = sorted(rows,
                         key=lambda k: k["checkout_counter"],
                         reverse=True)  # type: ignore
    return Device.serialize(sorted_rows[0]) if len(sorted_rows) > 0 else None
Ejemplo n.º 3
0
 async def mock_device_by_id(device_id: str):
     return Device.serialize(response_row)
Ejemplo n.º 4
0
def test_device_serial_optional_params(key, child, response_row) -> None:
    del response_row[key][child]

    result = Device.serialize(response_row)

    assert not result.dict()[key]
Ejemplo n.º 5
0
def test_serialize_device_required_params(key, response_row) -> None:
    del response_row[key]

    Device.serialize(response_row)  # Act
Ejemplo n.º 6
0
async def devices_by_type(model_id: int) -> List[Device]:
    """Retrieve metadata about ALL devices for by a specific model."""
    url = f"hardware?limit=500&model_id={model_id}"
    res = await inventory.response(url)
    return [Device.serialize(i) for i in res["rows"]]