Package: llamazure.azrest
llamazure.azrest : A REST client for the Azure APIUsagellamazure.azrest has 3 primary components:an Engine for running requestsan OpenAPI Generator for creating bindings for Azure servicesthe Bindings created by the Generator and submitted to the EngineThe general workflow is:use the Generator to codegen Bindingscreate an Engineuse the Bindings to create requestsrun those requests with the EngineYou only need to run regenrate the bindings to upgrade the version of llamazure or to change the version of the Azure API you're targeting.
If you're using the shell, you can use:python3llamazure-azrest-openapi'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/''specification/authorization/resource-manager/Microsoft.Authorization/stable/2022-04-01/authorization-RoleAssignmentsCalls.json''my/module/role_asn.py'If you're using the Pants build system, try something like:adhoc_tool(name="role_asn",runnable="//llamazure/azrest/openapi.py",# TODO: adapt for out-of-treeargs=["https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/","specification/authorization/resource-manager/Microsoft.Authorization/stable/2022-04-01/authorization-RoleAssignmentsCalls.json","my/module/role_asn.py",],output_files=["my/module/role_asn.py"],root_output_directory=".",)experimental_wrap_as_python_sources(name="azgen",inputs=[":role_asn",...],)And then in your code:fromazure.identityimportDefaultAzureCredentialfromllamazure.azrest.azrestimportAzRestfrommy.module.role_asnimportAzRoleAssignments# create the Engineaz=AzRest.from_credential(DefaultAzureCredential())# use the Bindings to create the requestreq=AzRoleAssignments.ListForScope(scope="/")# run the requestrole_assignments=az.call(req)Using the secret batching APIAzure lets you batch several requests into one request. This can save you round-trip time.fromazure.identityimportDefaultAzureCredentialfromllamazure.azrest.azrestimportAzRestfromllamazure.azrest.modelsimportBatchReqfrommy.module.role_asnimportAzRoleAssignmentsaz=AzRest.from_credential(DefaultAzureCredential())query_subscriptions:list[str]=[...]# use the bindings to create multiple requestsreqs=[AzRoleAssignments.ListForSubscription(sub)forsubinquery_subscriptions]# gather those into a batch# the `BatchReq.gather` method automatically assigns them IDsbatch_req=BatchReq.gather(reqs)# you can also name them explicitlynamed_reqs={sub:AzRoleAssignments.ListForSubscription(sub)forsubinquery_subscriptions}batch_req=BatchReq(named_reqs)az.call_batch(batch_req)Services with other domainSome services need different hosts. For example, keyvaults usehttps://myvault.vault.azure.net.
Using these services requires creating a separate AzRest instance to execute those requests:fromazure.identityimportDefaultAzureCredentialfromllamazure.azrest.azrestimportAzResta=AzRest.from_credential(DefaultAzureCredential(),token_scope="https://vault.azure.net/.default",base_url="https://myvault.vault.azure.net")Manually constructing requestsYou can build requests yourselffromllamazure.azrest.modelsimportReqreq=Req.get(name="List my spacecraft",# this is a meaningful name for you. It will appear in logspath="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/spaaaaaaaace/providers/Microsoft.Orbital/spacecrafts/potato",apiv="2022-11-01",ret_t=dict,# This is the return type you expect. You can use a Pydantic BaseModel. A Pydantic TypeAdapter is used to deserialise the return.)For requests that expect a body, use a Pydantic BaseModel.You can also modify existing requests. There are even some builtin helpers:frommy.module.role_asnimportAzRoleAssignmentssecure_rg="secure-rg"my_req=AzRoleAssignments.ListForResourceGroup(my_subscription,secure_rg).named("list role assignments on secure rg")TODOOdata parameters (x-ms-odata) (I'd have to think through the common elements with llamazure.msgraph)Better retries of Batch elementsEnums (x-ms-enum)Nullable (x-nullable)Known IssuesPagination doesn't work if the nextLinkName is not "nextLink"X-MS Supportsupportextensioncommentsnox-ms-skip-url-encodingpriority:low really, supporting url encoding is the prioritynox-ms-enumpriority:hignox-ms-parameter-groupingpriority:lownox-ms-parameter-locationpriority:lowyesx-ms-pathsnox-ms-client-namepriority:med will cause breaking changes in object namesnox-ms-externalpriority:lownox-ms-discriminator-valuepriority:low The resources that require this are not priorities for meneverx-ms-client-flattenIn all my experience with the API, this makes it harder to use in any contextmostlyx-ms-parameterized-hostsupported, requires a separate AzRest instancenox-ms-mutabilitypriority:lowneverx-ms-examplesNo need for examples in codeyesx-ms-error-responseGeneral error supportnox-ms-textpriority:low (only in file and blob operations)nox-ms-client-defaultpriority:low You can do this with filtersmostlyx-ms-pageablenox-ms-long-running-operationpriority:midnox-ms-long-running-operation-optionspriority:midnox-nullablepriority:higneverx-ms-header-collection-prefixI think this isn't for meneverx-ms-internalRude (although it's not used... in the public api)nox-ms-odatapriority:hig actually usefulneverx-ms-azure-resourceI'm not generating Terraform providers... yetnox-ms-request-idpriority:low I think this might be necessary for some long operationsnox-ms-client-request-idpriority:low I think this might be necessary for some long operationsnox-ms-arm-id-detailspriority:mid Might be nice to accept/convert the real resourcenox-ms-secretpriority:low seems more design-by-contractnox-ms-identifierspriority:lowneverx-ms-azure-rbac-permissions-requiredNot for me