def _upsert_user(request_session, api_url, user_doc):
    """
    Insert user, or update if insert fails due to user already existing.

    Returns:
        requests.Response: API response.

    Args:
        request_session (requests.Session): Session to use for the request.
        api_url (str): Base url for the API eg. 'https://localhost:8443/api'
        user_doc (dict): Valid user doc defined in user input schema.
    """
    new_user_resp = request_session.post(api_url + '/users', json=user_doc)
    if new_user_resp.status_code != 409:
        return new_user_resp

    # Already exists, update instead
    return request_session.put(api_url + '/users/' + user_doc['_id'], json=user_doc)