import requests
import logging


def post_peripheral(api_url: str, body: dict) -> dict:
    """ Posts a new peripheral into Nuvla, via the Agent API

    :param body: content of the peripheral
    :param api_url: URL of the Agent API for peripherals
    :return: Nuvla resource
    """

    try:
        r = requests.post(api_url, json=body)
        r.raise_for_status()
        return r.json()
    except:
        logging.error(f'Cannot create new peripheral in Nuvla. See agent logs for more details on the problem')
        # this will be caught by the calling block
        raise