Sample code for 30+ languages & platforms
Zig

Shopware List Media

See more Shopware Examples

Demonstrates how to get a list of media in Shopware.

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();

    var success: bool = false;

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

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

    http.setLogin("api_username");
    http.setPassword("api_key");
    http.setBasicAuth(true);

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://my-shopware-shop.com/api/media?limit=10", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "data": [
    //     {
    //       "id": 6708,
    //       "albumId": -9,
    //       "name": "sonnenblume",
    //       "description": "",
    //       "path": "https:\/\/my-shopware-shop.com\/media\/pdf\/2f\/26\/52\/sonnenblume.zip",
    //       "type": "ARCHIVE",
    //       "extension": "zip",
    //       "userId": 5,
    //       "created": "2021-03-01T00:00:00+0100",
    //       "fileSize": 216905,
    //       "width": null,
    //       "height": null,
    //       "attribute": null
    //     },
    //     {
    //       "id": 6709,
    //       "albumId": -9,
    //       "name": "csinventur_anleitung",
    //       "description": "",
    //       "path": "https:\/\/my-shopware-shop.com\/media\/pdf\/19\/21\/86\/csinventur_anleitung.pdf",
    //       "type": "PDF",
    //       "extension": "pdf",
    //       "userId": 5,
    //       "created": "2021-03-01T00:00:00+0100",
    //       "fileSize": 837131,
    //       "width": null,
    //       "height": null,
    //       "attribute": null
    //     },
    //     {
    //       "id": 6710,
    //       "albumId": -9,
    //       "name": "photos_fre_carousel_elevatorpitch_620x252",
    //       "description": "",
    //       "path": "https:\/\/my-shopware-shop.com\/media\/pdf\/d8\/d7\/b4\/photos_fre_carousel_elevatorpitch_620x252.mp4",
    //       "type": "VIDEO",
    //       "extension": "mp4",
    //       "userId": 5,
    //       "created": "2021-03-01T00:00:00+0100",
    //       "fileSize": 2499157,
    //       "width": null,
    //       "height": null,
    //       "attribute": null
    //     },
    //
    //     ...
    //   ],
    //   "total": 31,
    //   "success": true
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var id: i32 = 0;
    var album_id: i32 = 0;
    var name: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var path: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var extension: [:0]const u8 = "";
    var user_id: i32 = 0;
    var created: [:0]const u8 = "";
    var file_size: i32 = 0;
    var width: [:0]const u8 = "";
    var height: [:0]const u8 = "";
    var attribute: [:0]const u8 = "";

    _ = j_resp.intOf("total");
    success = j_resp.boolOf("success");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("data");
    while (i < count_i) {
        j_resp.setI(i);
        id = j_resp.intOf("data[i].id");
        album_id = j_resp.intOf("data[i].albumId");
        name = try j_resp.stringOf(alloc, "data[i].name");
        description = try j_resp.stringOf(alloc, "data[i].description");
        path = try j_resp.stringOf(alloc, "data[i].path");
        v_type = try j_resp.stringOf(alloc, "data[i].type");
        extension = try j_resp.stringOf(alloc, "data[i].extension");
        user_id = j_resp.intOf("data[i].userId");
        created = try j_resp.stringOf(alloc, "data[i].created");
        file_size = j_resp.intOf("data[i].fileSize");
        width = try j_resp.stringOf(alloc, "data[i].width");
        height = try j_resp.stringOf(alloc, "data[i].height");
        attribute = try j_resp.stringOf(alloc, "data[i].attribute");
        i = i + 1;
    }
}