Esempio n. 1
0
    def test_get_document_analysis_client(self, formrecognizer_test_endpoint, formrecognizer_test_api_key):
        transport = RequestsTransport()
        dtc = DocumentModelAdministrationClient(endpoint=formrecognizer_test_endpoint, credential=AzureKeyCredential(formrecognizer_test_api_key), transport=transport)

        with dtc:
            dtc.get_account_info()
            assert transport.session is not None
            with dtc.get_document_analysis_client() as dac:
                assert transport.session is not None
                dac.begin_analyze_document_from_url("prebuilt-receipt", self.receipt_url_jpg).wait()
                assert dac._api_version == DocumentAnalysisApiVersion.V2021_09_30_PREVIEW
            dtc.get_account_info()
            assert transport.session is not None
Esempio n. 2
0
    def test_dmac_auth_bad_key(self, formrecognizer_test_endpoint,
                               formrecognizer_test_api_key, **kwargs):
        client = DocumentModelAdministrationClient(
            formrecognizer_test_endpoint, AzureKeyCredential("xxxx"))
        with pytest.raises(ClientAuthenticationError):
            result = client.get_account_info()

        return {}
Esempio n. 3
0
    def test_get_document_analysis_client(self, formrecognizer_test_endpoint, formrecognizer_test_api_key, **kwargs):
        # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
        set_custom_default_matcher(
            compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
        )  
        transport = RequestsTransport()
        dtc = DocumentModelAdministrationClient(endpoint=formrecognizer_test_endpoint, credential=AzureKeyCredential(formrecognizer_test_api_key), transport=transport)

        with dtc:
            dtc.get_account_info()
            assert transport.session is not None
            with dtc.get_document_analysis_client() as dac:
                assert transport.session is not None
                dac.begin_analyze_document_from_url("prebuilt-receipt", self.receipt_url_jpg).wait()
                assert dac._api_version == DocumentAnalysisApiVersion.V2022_01_30_PREVIEW
            dtc.get_account_info()
            assert transport.session is not None
Esempio n. 4
0
def authentication_with_api_key_credential_document_model_admin_client():
    # [START create_dt_client_with_key]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentModelAdministrationClient

    endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
    key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

    document_model_admin_client = DocumentModelAdministrationClient(endpoint, AzureKeyCredential(key))
    # [END create_dt_client_with_key]
    info = document_model_admin_client.get_account_info()
Esempio n. 5
0
def sample_manage_models():
    from azure.core.credentials import AzureKeyCredential
    from azure.core.exceptions import ResourceNotFoundError
    from azure.ai.formrecognizer import DocumentModelAdministrationClient, DocumentBuildMode

    endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
    key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
    container_sas_url = os.environ["CONTAINER_SAS_URL"]

    # [START get_account_info]
    document_model_admin_client = DocumentModelAdministrationClient(
        endpoint=endpoint, credential=AzureKeyCredential(key))

    account_info = document_model_admin_client.get_account_info()
    print(
        "Our account has {} custom models, and we can have at most {} custom models\n"
        .format(account_info.document_model_count,
                account_info.document_model_limit))
    # [END get_account_info]

    # Next, we get a paged list of all of our custom models
    # [START list_models]
    models = document_model_admin_client.list_models()

    print("We have the following 'ready' models with IDs and descriptions:")
    for model in models:
        print("{} | {}".format(model.model_id, model.description))
    # [END list_models]

    # let's build a model to use for this sample
    poller = document_model_admin_client.begin_build_model(
        container_sas_url,
        DocumentBuildMode.TEMPLATE,
        description="model for sample")
    model = poller.result()

    # [START get_model]
    my_model = document_model_admin_client.get_model(model_id=model.model_id)
    print("\nModel ID: {}".format(my_model.model_id))
    print("Description: {}".format(my_model.description))
    print("Model created on: {}".format(my_model.created_on))
    # [END get_model]

    # Finally, we will delete this model by ID
    # [START delete_model]
    document_model_admin_client.delete_model(model_id=my_model.model_id)

    try:
        document_model_admin_client.get_model(model_id=my_model.model_id)
    except ResourceNotFoundError:
        print("Successfully deleted model with ID {}".format(
            my_model.model_id))
Esempio n. 6
0
def authentication_with_azure_active_directory_document_model_admin_client():
    # [START create_dt_client_with_aad]
    """DefaultAzureCredential will use the values from these environment
    variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
    """
    from azure.ai.formrecognizer import DocumentModelAdministrationClient
    from azure.identity import DefaultAzureCredential

    endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
    credential = DefaultAzureCredential()

    document_model_admin_client = DocumentModelAdministrationClient(endpoint, credential)
    # [END create_dt_client_with_aad]
    info = document_model_admin_client.get_account_info()
Esempio n. 7
0
    def test_logging_info_dmac_client(self, formrecognizer_test_endpoint,
                                      formrecognizer_test_api_key):
        client = DocumentModelAdministrationClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key))
        mock_handler = MockHandler()

        logger = logging.getLogger("azure")
        logger.addHandler(mock_handler)
        logger.setLevel(logging.INFO)

        result = client.get_account_info()

        for message in mock_handler.messages:
            if message.levelname == "INFO":
                # not able to use json.loads here. At INFO level only API key should be REDACTED
                if message.message.find("Ocp-Apim-Subscription-Key") != -1:
                    assert message.message.find("REDACTED") != -1
                else:
                    assert message.message.find("REDACTED") == -1
Esempio n. 8
0
 def test_active_directory_auth(self):
     token = self.generate_oauth_token()
     endpoint = self.get_oauth_endpoint()
     client = DocumentModelAdministrationClient(endpoint, token)
     info = client.get_account_info()
     assert info