code: def encode_with(string, encoding):
      """Encoding ``string`` with ``encoding`` if necessary.
  
      :param str string: If string is a bytes object, it will not encode it.
          Otherwise, this function will encode it with the provided encoding.
      :param str encoding: The encoding with which to encode string.
      :returns: encoded bytes object
      """
      if not (string is None or isinstance(string, bytes)):
          return string.encode(encoding)
      return string
  
imports: import contextlib
  import io
  import os
  from uuid import uuid4
  import requests
  from .._compat import fields
label_desc: Use image node_id 2 for calling a global function with example usage: encode_with(string, encoding) and returns: string, string