Sample code for 30+ languages & platforms
Zig

Azure List Storage Accounts

See more Azure Storage Accounts Examples

Demonstrates how to list Azure storage accounts.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Load an OAuth2 access token previously fetched by this example:  Get Azure OAuth2 Access Token
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/azureToken.json");
    // Assuming success..
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
    std.debug.print("AuthToken: {s}\n", .{try http.getAuthToken(alloc)});

    http.setAccept("application/json");

    const url = "https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Storage/storageAccounts?api-version=2018-11-01";

    const json_resp = http.quickGetStr(alloc, url) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{http.getLastStatus()});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(json_resp) catch {};
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    if (http.getLastStatus() != 200) {
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample output...
    // (See the parsing code below..)
    //
    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "value": [
    //     {
    //       "sku": {
    //         "name": "Standard_LRS",
    //         "tier": "Standard"
    //       },
    //       "kind": "Storage",
    //       "id": "/subscriptions/6c42643b-ebef-45e0-b917-b3583b84a57f/resourceGroups/gchilkat/providers/Microsoft.Storage/storageAccounts/chilkat",
    //       "name": "chilkat",
    //       "type": "Microsoft.Storage/storageAccounts",
    //       "location": "eastus",
    //       "tags": {},
    //       "properties": {
    //         "networkAcls": {
    //           "bypass": "AzureServices",
    //           "virtualNetworkRules": [
    //           ],
    //           "ipRules": [
    //           ],
    //           "defaultAction": "Allow"
    //         },
    //         "supportsHttpsTrafficOnly": true,
    //         "encryption": {
    //           "services": {
    //             "file": {
    //               "enabled": true,
    //               "lastEnabledTime": "2017-12-28T11:02:10.6840887Z"
    //             },
    //             "blob": {
    //               "enabled": true,
    //               "lastEnabledTime": "2017-12-28T11:02:10.6840887Z"
    //             }
    //           },
    //           "keySource": "Microsoft.Storage"
    //         },
    //         "provisioningState": "Succeeded",
    //         "creationTime": "2016-04-18T22:57:36.5377065Z",
    //         "primaryEndpoints": {
    //           "blob": "https://chilkat.blob.core.windows.net/",
    //           "queue": "https://chilkat.queue.core.windows.net/",
    //           "table": "https://chilkat.table.core.windows.net/",
    //           "file": "https://chilkat.file.core.windows.net/"
    //         },
    //         "primaryLocation": "eastus",
    //         "statusOfPrimary": "available"
    //       }
    //     }
    //   ]
    // }
    //

    var i: i32 = 0;
    var sku_name: [:0]const u8 = "";
    var sku_tier: [:0]const u8 = "";
    var kind: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var location: [:0]const u8 = "";
    var properties_network_acls_bypass: [:0]const u8 = "";
    var properties_network_acls_default_action: [:0]const u8 = "";
    var properties_supports_https_traffic_only: bool = false;
    var properties_encryption_services_file_enabled: bool = false;
    var properties_encryption_services_file_last_enabled_time: [:0]const u8 = "";
    var properties_encryption_services_blob_enabled: bool = false;
    var properties_encryption_services_blob_last_enabled_time: [:0]const u8 = "";
    var properties_encryption_key_source: [:0]const u8 = "";
    var properties_provisioning_state: [:0]const u8 = "";
    var properties_creation_time: [:0]const u8 = "";
    var properties_primary_endpoints_blob: [:0]const u8 = "";
    var properties_primary_endpoints_queue: [:0]const u8 = "";
    var properties_primary_endpoints_table: [:0]const u8 = "";
    var properties_primary_endpoints_file: [:0]const u8 = "";
    var properties_primary_location: [:0]const u8 = "";
    var properties_status_of_primary: [:0]const u8 = "";

    i = 0;
    const count_i = json.sizeOfArray("value");
    while (i < count_i) {
        json.setI(i);
        sku_name = try json.stringOf(alloc, "value[i].sku.name");
        sku_tier = try json.stringOf(alloc, "value[i].sku.tier");
        kind = try json.stringOf(alloc, "value[i].kind");
        id = try json.stringOf(alloc, "value[i].id");
        name = try json.stringOf(alloc, "value[i].name");
        v_type = try json.stringOf(alloc, "value[i].type");
        location = try json.stringOf(alloc, "value[i].location");
        properties_network_acls_bypass = try json.stringOf(alloc, "value[i].properties.networkAcls.bypass");
        properties_network_acls_default_action = try json.stringOf(alloc, "value[i].properties.networkAcls.defaultAction");
        properties_supports_https_traffic_only = json.boolOf("value[i].properties.supportsHttpsTrafficOnly");
        properties_encryption_services_file_enabled = json.boolOf("value[i].properties.encryption.services.file.enabled");
        properties_encryption_services_file_last_enabled_time = try json.stringOf(alloc, "value[i].properties.encryption.services.file.lastEnabledTime");
        properties_encryption_services_blob_enabled = json.boolOf("value[i].properties.encryption.services.blob.enabled");
        properties_encryption_services_blob_last_enabled_time = try json.stringOf(alloc, "value[i].properties.encryption.services.blob.lastEnabledTime");
        properties_encryption_key_source = try json.stringOf(alloc, "value[i].properties.encryption.keySource");
        properties_provisioning_state = try json.stringOf(alloc, "value[i].properties.provisioningState");
        properties_creation_time = try json.stringOf(alloc, "value[i].properties.creationTime");
        properties_primary_endpoints_blob = try json.stringOf(alloc, "value[i].properties.primaryEndpoints.blob");
        properties_primary_endpoints_queue = try json.stringOf(alloc, "value[i].properties.primaryEndpoints.queue");
        properties_primary_endpoints_table = try json.stringOf(alloc, "value[i].properties.primaryEndpoints.table");
        properties_primary_endpoints_file = try json.stringOf(alloc, "value[i].properties.primaryEndpoints.file");
        properties_primary_location = try json.stringOf(alloc, "value[i].properties.primaryLocation");
        properties_status_of_primary = try json.stringOf(alloc, "value[i].properties.statusOfPrimary");
        i = i + 1;
    }
}