def client_with_custom_session_sample():
    """Sample client with a custom Session object"""
    session = Session()
    my_client = HTTPClientFactory(session=session
                                  ).create_with_default_middleware(browser_credential)
    result = my_client.get('/me')
    pprint(result.json())
def test_create_with_default_middleware():
    """Test creation of HTTP Client using default middleware"""
    credential = _CustomTokenCredential()
    client = HTTPClientFactory().create_with_default_middleware(
        credential=credential)
    middleware = client.get_adapter('https://')

    assert isinstance(middleware, HTTPAdapter)
def test_register_middleware():
    credential = _CustomTokenCredential()
    middleware = [
        AuthorizationHandler(credential),
    ]
    client = HTTPClientFactory()
    client._register(middleware)

    assert isinstance(client.session.get_adapter('https://'), HTTPAdapter)
def test_create_with_custom_middleware():
    """Test creation of HTTP Clients with custom middleware"""
    credential = _CustomTokenCredential()
    middleware = [
        AuthorizationHandler(credential),
    ]
    client = HTTPClientFactory().create_with_custom_middleware(
        middleware=middleware)
    custom_middleware = client.get_adapter('https://')

    assert isinstance(custom_middleware, HTTPAdapter)
def test_client_factory_with_custom_settings():
    """
    Test that the client works with user provided configuration
    """
    credential = _CustomTokenCredential()
    client = HTTPClientFactory(
        api_version=APIVersion.beta).create_with_default_middleware(credential)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
def test_client_factory_with_default_middleware():
    """
    Test that a client created from client factory with default middleware
    works as expected.
    """
    credential = _CustomTokenCredential()
    client = HTTPClientFactory().create_with_default_middleware(credential)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
def test_client_factory_with_custom_middleware():
    """
    Test client factory works with user provided middleware
    """
    credential = _CustomTokenCredential()
    middleware = [
        AuthorizationHandler(credential),
    ]
    client = HTTPClientFactory().create_with_custom_middleware(middleware)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
def test_client_factory_with_user_provided_session():
    """
    Test that the client works with a user provided session object
    """

    session = Session()
    credential = _CustomTokenCredential()
    client = HTTPClientFactory(
        session=session).create_with_default_middleware(credential)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
def test_context_object_is_attached_to_requests_from_client_factory():
    """
    Test that requests from a native HTTP client have a context object attached
    """
    credential = _CustomTokenCredential()
    middleware = [
        AuthorizationHandler(credential),
    ]
    client = HTTPClientFactory().create_with_custom_middleware(middleware)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
    assert hasattr(response.request, 'context')
def test_middleware_control_is_empty_for_requests_from_client_factory():
    """
    Test that requests from a native HTTP client have no middlware options in the middleware
    control
    """
    credential = _CustomTokenCredential()
    middleware = [
        AuthorizationHandler(credential),
    ]
    client = HTTPClientFactory().create_with_custom_middleware(middleware)
    response = client.get(
        'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
    )
    assert response.status_code == 200
    assert response.request.context.middleware_control == {}
def client_with_custom_settings_sample():
    """Sample client that makes requests against the beta api on a specified cloud endpoint"""
    my_client = HTTPClientFactory(
        credential=browser_credential,
        api_version=APIVersion.beta,
        cloud=NationalClouds.Germany,
    ).create_with_default_middleware(browser_credential)
    result = my_client.get(
        '/users',
        params={
            '$select': 'displayName',
            '$top': '10'
        },
    )
    pprint(result.json())
def client_with_custom_middleware():
    """Sample client with a custom middleware chain"""
    middleware = [
        CustomAuthorizationHandler(),
        MyCustomMiddleware(),
    ]

    my_client = HTTPClientFactory().create_with_custom_middleware(middleware)
    result = my_client.get(
        'https://graph.microsoft.com/v1.0/users',
        params={
            '$select': 'displayName',
            '$top': '10'
        },
    )
    pprint(result.json())
def test_get_base_url():
    """
    Test base url is formed by combining the national cloud endpoint with
    Api version
    """
    client = HTTPClientFactory(api_version=APIVersion.beta,
                               cloud=NationalClouds.Germany)
    assert client.session.base_url == client.endpoint + '/' + client.api_version
def test_initialize_with_custom_config():
    """Test creation of HTTP Client will use custom configuration if they are passed"""
    client = HTTPClientFactory(api_version=APIVersion.beta, timeout=(5, 5))

    assert client.api_version == APIVersion.beta
    assert client.endpoint == NationalClouds.Global
    assert client.timeout == (5, 5)
    assert isinstance(client.session, Session)
def test_initialize_with_default_config():
    """Test creation of HTTP Client will use the default configuration
    if none are passed"""
    client = HTTPClientFactory()

    assert client.api_version == APIVersion.v1
    assert client.endpoint == NationalClouds.Global
    assert client.timeout == (DEFAULT_CONNECTION_TIMEOUT,
                              DEFAULT_REQUEST_TIMEOUT)
    assert isinstance(client.session, Session)
"""
import json
from pprint import pprint

# This sample uses InteractiveBrowserCredential only for demonstration.
# Any azure-identity TokenCredential class will work the same.
from azure.identity import InteractiveBrowserCredential
from requests import Session

from msgraph.core import APIVersion, HTTPClientFactory, NationalClouds

scopes = ['user.read']
browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID')

# Create client with default middleware
client = HTTPClientFactory().create_with_default_middleware(browser_credential)


def get_sample():
    """Sample HTTP GET request using the client"""
    result = client.get(
        '/users',
        params={
            '$select': 'displayName',
            '$top': '10'
        },
    )
    pprint(result.json())


def post_sample():