import six


def to_bytes(value):
    """
    Converts value to its bytes representation:

    * Uses value`s toWire method if it has one
    * Encodes to utf-8 if the value is a unicode string
    * Otherwise wraps value into bytes()
    """
    if hasattr(value, 'toWire'):
        return value.toWire()
    if isinstance(value, six.integer_types):
        return str(value).encode("utf-8")
    if isinstance(value, six.text_type):
        return value.encode('utf-8')
    return bytes(value)