<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def put_resource(collection, key):
 """Replace the resource identified by the given key and return the appropriate response. :param string collection: a :class:`sandman.model.Model` endpoint :rtype: :class:`flask.Response` """

    resource = retrieve_resource(collection, key)

    _validate(endpoint_class(collection), request.method, resource)

    resource.replace(get_resource_data(request))
    try:
        _perform_database_action('add', resource)
    except IntegrityError as exception:
        raise InvalidAPIUsage(422, FORWARDED_EXCEPTION_MESSAGE.format(
            exception))
    return no_content_response()