def wrap_http_for_auth(credentials, http):
orig_request_method = http.request<EOL>def new_request(uri, method='<STR_LIT:GET>', body=None, headers=None,<EOL>redirections=httplib2.DEFAULT_MAX_REDIRECTS,<EOL>connection_type=None):<EOL><INDENT>if not credentials.access_token:<EOL><INDENT>_LOGGER.info('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>credentials._refresh(orig_request_method)<EOL><DEDENT>headers = _initialize_headers(headers)<EOL>credentials.apply(headers)<EOL>_apply_user_agent(headers, credentials.user_agent)<EOL>body_stream_position = None<EOL>if all(getattr(body, stream_prop, None) for stream_prop in<EOL>_STREAM_PROPERTIES):<EOL><INDENT>body_stream_position = body.tell()<EOL><DEDENT>resp, content = request(orig_request_method, uri, method, body,<EOL>clean_headers(headers),<EOL>redirections, connection_type)<EOL>max_refresh_attempts = <NUM_LIT:2><EOL>for refresh_attempt in range(max_refresh_attempts):<EOL><INDENT>if resp.status not in REFRESH_STATUS_CODES:<EOL><INDENT>break<EOL><DEDENT>_LOGGER.info('<STR_LIT>',<EOL>resp.status, refresh_attempt + <NUM_LIT:1>,<EOL>max_refresh_attempts)<EOL>credentials._refresh(orig_request_method)<EOL>credentials.apply(headers)<EOL>if body_stream_position is not None:<EOL><INDENT>body.seek(body_stream_position)<EOL><DEDENT>resp, content = request(orig_request_method, uri, method, body,<EOL>clean_headers(headers),<EOL>redirections, connection_type)<EOL><DEDENT>return resp, content<EOL><DEDENT>http.request = new_request<EOL>http.request.credentials = credentials<EOL>
Prepares an HTTP object's request method for auth.

    Wraps HTTP requests with logic to catch auth failures (typically
    identified via a 401 status code). In the event of failure, tries
    to refresh the token used and then retry the original request.

    Args:
        credentials: Credentials, the credentials used to identify
                     the authenticated user.
        http: httplib2.Http, an http object to be used to make
              auth requests.