Package: azure-iot-modelsrepository
Azure IoT Models Repository client library for PythonThe Azure IoT Models Repository Library for Python provides functionality for working with the Azure IoT Models RepositoryGetting startedInstall packageInstall the Azure IoT Models Repository library for Python withpip:pipinstallazure-iot-modelsrepositoryPrerequisitesA models repository followingAzure IoT conventionsThe models repository can be hosted on the local filesystem or hosted on a webserverAzure IoT hosts the globalAzure IoT Models Repositorywhich the client will use if no custom location is providedPublishing ModelsFollow theguideto publish models to the global Azure IoT Models Repository.If using a custom local or remote repository, you can simply add your model files to a directory structure in the repository location, e.g.dtmi/com/example/thermostat-1.jsonAuthenticationCurrently, no authentication mechanisms are supported. The global endpoint is not tied to an Azure subscription and does not support authentication. All models published are meant for anonymous public consumption.Key conceptsThe Azure IoT Models Repository enables builders to manage and share digital twin models. The models areJSON-LDdocuments defined using the Digital Twins Definition Language (DTDL).The repository defines a pattern to store DTDL interfaces in a directory structure based on the Digital Twin Model Identifier (DTMI). You can locate an interface in the repository by converting the DTMI to a relative path. For example, the DTMIdtmi:com:example:Thermostat;1translates to/dtmi/com/example/thermostat-1.json.ExamplesThe following sections provide several snippets covering common Models Repository tasks:Initializing the ModelsRepositoryClientGet ModelsDTMI ConventionsInitializing the ModelsRepositoryClientRepository LocationWhen no repository location is provided during instantiation, the Azure IoT Models Repository global endpoint (https://devicemodels.azure.com/) is usedclient=ModelsRepositoryClient()Alternatively, you can provide a custom location for where your repository is located via the optionalrepository_locationkeyword. The client accepts the following location formats:Web URL - e.g."https://contoso.com/models/"Local Filesystem URI - e.g."file:///path/to/repository/"POSIX filepath - e.g."/path/to/repository/"Drive letter filepath - e.g."C:/path/to/repository/"client=ModelsRepositoryClient(repository_location="https://contoso.com/models/")Dependency Resolution ModeThe client can be configured with an optionaldependency_resolutionmode at instantiation, using one of the following values:'disabled'- The client will not resolve model dependencies'enabled'- The client will resolve any model dependencies'tryFromExpanded'- The client will attempt to resolve models using an expanded model definition (falling back on'enabled'mode if not possible)client=ModelsRepositoryClient(dependency_resolution="enabled")If thedependency_resolutionmode is not specified:Clients configured for the Azure IoT Models Repository global endpoint will default to using'tryFromExpanded'Clients configured for a custom location (remote or local) will default to using'enabled'Additional OptionsIf you need to override default pipeline behavior from theazure-core library, you can provide variouskeyword argumentsduring instantiation.Client cleanupWhen you are finished with your client, make sure to call.close()in order to free up resourcesclient=ModelsRepositoryClient()# Do thingsclient.close()In order to avoid having to do this, it is recommended that you use your client from within a context manager whenever possible, which will automatically close for youwithModelsRepositoryClient()asclient:# Do thingsModelsRepositoryClient - Get ModelsNote that you must firstpublish models to your repositorybefore you can fetch them. The following examples assume you are using the global Azure IoT Models Repository.Calling.get_models()will fetch the model at the provided DTMI and potentially its dependencies (depending on the dependency resolution mode). It will return adictthat maps DTMIs to model definitions.dtmi="dtmi:com:example:TemperatureController;1"withModelsRepositoryClient()asclient:models=get_models(dtmi)print("{}resolved in{}interfaces".format(dtmi,len(models)))If you provide multiple DTMIs to the method, you can retrieve multiple models (and potentially their dependencies) at oncedtmis=["dtmi:com:example:TemperatureController;1","dtmi:com:example:azuresphere:sampledevice;1"]withModelsRepositoryClient()asclient:models=get_models(dtmis)print("{}resolved in{}interfaces".format(dtmi,len(models)))By default the client will use whicheverdependency resolution modeit was configured with at instantiation when retrieving models. However, this behavior can be overridden by passing any of the valid options in as an optional keyword argument to.get_models()dtmi="dtmi:com:example:TemperatureController;1"withModelsRepositoryClient(dependency_resolution="disabled")asclient:models=get_models(dtmi,dependency_resolution="enabled")DTMI ConventionsThe package contains a module calleddtmi_conventions, which, when imported provides a series of utility operations for working with DTMIs# Returns True - this is a valid DTMIdtmi_conventions.is_valid_dtmi("dtmi:com:example:Thermostat;1")# Returns False - this is NOT a valid DTMIdtmi_conventions.is_valid_dtmi("dtmi:com:example:Thermostat")dtmi="dtmi:com:example:Thermostat;1"# Local repository examplerepo_uri="file:///path/to/repository"print(dtmi_conventions.get_model_uri(dtmi,repo_uri))# Prints: "file:///path/to/repository/dtmi/com/example/thermostat-1.json"print(dtmi_conventions.get_model_uri(dtmi,repo_uri,expanded=True))# Prints: "file:///path/to/repository/dtmi/com/example/thermostat-1.expanded.json"# Remote repository examplerepo_uri="https://contoso.com/models/"print(dtmi_conventions.get_model_uri(dtmi,repo_uri))# Prints: "https://contoso/com/models/dtmi/com/example/thermostat-1.json"print(dtmi_conventions.get_model_uri(dtmi,repo_uri,expanded=True))# Prints: "https://contoso/com/models/dtmi/com/example/thermostat-1.expanded.json"TroubleshootingLoggingThis library uses the standardlogginglibrary for logging. Information about HTTP sessions (URLs, headers, etc.) is logged atDEBUGlevel.ExceptionsModels Repository APIs may raise exceptions defined inazure-core.Additionally, they may raise exceptions defined in theazure-iot-modelsrepository:ModelError- Indicates an error occurred while trying to parse/resolve a model definition. This generally means that there is a malformed model that does not comply with themodel DTDL specificationProvide FeedbackIf you encounter bugs or have suggestions, pleaseopen an issue.Next stepsSamplesAdditional samples are available in thesamples repository.ContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visithttps://cla.microsoft.com.When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct. For more information see theCode of Conduct FAQor contactopencode@microsoft.comwith any additional questions or comments.