Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Shopify Create New ProductCreate a new product
#include <C_CkRestW.h> #include <C_CkJsonObjectW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkRestW rest; BOOL success; HCkJsonObjectW jsonReq; HCkStringBuilderW sbReq; HCkStringBuilderW sbJson; HCkJsonObjectW json; int productId; const wchar_t *productTitle; const wchar_t *productBody_html; const wchar_t *productVendor; const wchar_t *productProduct_type; const wchar_t *productCreated_at; const wchar_t *productHandle; const wchar_t *productUpdated_at; const wchar_t *productPublished_at; BOOL productTemplate_suffix; const wchar_t *productPublished_scope; const wchar_t *productTags; int productImageId; int productImageProduct_id; int productImagePosition; const wchar_t *productImageCreated_at; const wchar_t *productImageUpdated_at; int productImageWidth; int productImageHeight; const wchar_t *productImageSrc; int i; int count_i; int id; int product_id; const wchar_t *title; const wchar_t *price; const wchar_t *sku; int position; int grams; const wchar_t *inventory_policy; BOOL compare_at_price; const wchar_t *fulfillment_service; BOOL inventory_management; const wchar_t *option1; BOOL option2; BOOL option3; const wchar_t *created_at; const wchar_t *updated_at; BOOL taxable; BOOL barcode; BOOL image_id; int inventory_quantity; int weight; const wchar_t *weight_unit; int old_inventory_quantity; BOOL requires_shipping; const wchar_t *name; int j; int count_j; const wchar_t *strVal; int width; int height; const wchar_t *src; rest = CkRestW_Create(); CkRestW_SetAuthBasic(rest,L"SHOPIFY_PRIVATE_API_KEY",L"SHOPIFY_PRIVATE_API_SECRET_KEY"); success = CkRestW_Connect(rest,L"chilkat.myshopify.com",443,TRUE,TRUE); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); return; } // The following code creates the JSON request body. // The JSON created by this code is shown below. jsonReq = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonReq,L"product.title",L"Burton Custom Freestyle 151"); CkJsonObjectW_UpdateString(jsonReq,L"product.body_html",L"<strong>Good snowboard!</strong>"); CkJsonObjectW_UpdateString(jsonReq,L"product.vendor",L"Burton"); CkJsonObjectW_UpdateString(jsonReq,L"product.product_type",L"Snowboard"); CkJsonObjectW_UpdateString(jsonReq,L"product.images[0].attachment",L"R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="); // The JSON request body created by the above code: // { // "product": { // "title": "Burton Custom Freestyle 151", // "body_html": "<strong>Good snowboard!<\/strong>", // "vendor": "Burton", // "product_type": "Snowboard", // "images": [ // { // "attachment": "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" // } // ] // } // } sbReq = CkStringBuilderW_Create(); CkJsonObjectW_EmitSb(jsonReq,sbReq); CkRestW_AddHeader(rest,L"Content-Type",L"application/json"); sbJson = CkStringBuilderW_Create(); success = CkRestW_FullRequestSb(rest,L"POST",L"/admin/products.json",sbReq,sbJson); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbJson); return; } if (CkRestW_getResponseStatusCode(rest) != 201) { wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest)); wprintf(L"Response body:\n"); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbJson)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbJson); return; } json = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(json,sbJson); // The following code parses the JSON response. // A sample JSON response is shown below the sample code. productId = CkJsonObjectW_IntOf(json,L"product.id"); productTitle = CkJsonObjectW_stringOf(json,L"product.title"); productBody_html = CkJsonObjectW_stringOf(json,L"product.body_html"); productVendor = CkJsonObjectW_stringOf(json,L"product.vendor"); productProduct_type = CkJsonObjectW_stringOf(json,L"product.product_type"); productCreated_at = CkJsonObjectW_stringOf(json,L"product.created_at"); productHandle = CkJsonObjectW_stringOf(json,L"product.handle"); productUpdated_at = CkJsonObjectW_stringOf(json,L"product.updated_at"); productPublished_at = CkJsonObjectW_stringOf(json,L"product.published_at"); productTemplate_suffix = CkJsonObjectW_IsNullOf(json,L"product.template_suffix"); productPublished_scope = CkJsonObjectW_stringOf(json,L"product.published_scope"); productTags = CkJsonObjectW_stringOf(json,L"product.tags"); productImageId = CkJsonObjectW_IntOf(json,L"product.image.id"); productImageProduct_id = CkJsonObjectW_IntOf(json,L"product.image.product_id"); productImagePosition = CkJsonObjectW_IntOf(json,L"product.image.position"); productImageCreated_at = CkJsonObjectW_stringOf(json,L"product.image.created_at"); productImageUpdated_at = CkJsonObjectW_stringOf(json,L"product.image.updated_at"); productImageWidth = CkJsonObjectW_IntOf(json,L"product.image.width"); productImageHeight = CkJsonObjectW_IntOf(json,L"product.image.height"); productImageSrc = CkJsonObjectW_stringOf(json,L"product.image.src"); i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"product.variants"); while (i < count_i) { CkJsonObjectW_putI(json,i); id = CkJsonObjectW_IntOf(json,L"product.variants[i].id"); product_id = CkJsonObjectW_IntOf(json,L"product.variants[i].product_id"); title = CkJsonObjectW_stringOf(json,L"product.variants[i].title"); price = CkJsonObjectW_stringOf(json,L"product.variants[i].price"); sku = CkJsonObjectW_stringOf(json,L"product.variants[i].sku"); position = CkJsonObjectW_IntOf(json,L"product.variants[i].position"); grams = CkJsonObjectW_IntOf(json,L"product.variants[i].grams"); inventory_policy = CkJsonObjectW_stringOf(json,L"product.variants[i].inventory_policy"); compare_at_price = CkJsonObjectW_IsNullOf(json,L"product.variants[i].compare_at_price"); fulfillment_service = CkJsonObjectW_stringOf(json,L"product.variants[i].fulfillment_service"); inventory_management = CkJsonObjectW_IsNullOf(json,L"product.variants[i].inventory_management"); option1 = CkJsonObjectW_stringOf(json,L"product.variants[i].option1"); option2 = CkJsonObjectW_IsNullOf(json,L"product.variants[i].option2"); option3 = CkJsonObjectW_IsNullOf(json,L"product.variants[i].option3"); created_at = CkJsonObjectW_stringOf(json,L"product.variants[i].created_at"); updated_at = CkJsonObjectW_stringOf(json,L"product.variants[i].updated_at"); taxable = CkJsonObjectW_BoolOf(json,L"product.variants[i].taxable"); barcode = CkJsonObjectW_IsNullOf(json,L"product.variants[i].barcode"); image_id = CkJsonObjectW_IsNullOf(json,L"product.variants[i].image_id"); inventory_quantity = CkJsonObjectW_IntOf(json,L"product.variants[i].inventory_quantity"); weight = CkJsonObjectW_IntOf(json,L"product.variants[i].weight"); weight_unit = CkJsonObjectW_stringOf(json,L"product.variants[i].weight_unit"); old_inventory_quantity = CkJsonObjectW_IntOf(json,L"product.variants[i].old_inventory_quantity"); requires_shipping = CkJsonObjectW_BoolOf(json,L"product.variants[i].requires_shipping"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"product.options"); while (i < count_i) { CkJsonObjectW_putI(json,i); id = CkJsonObjectW_IntOf(json,L"product.options[i].id"); product_id = CkJsonObjectW_IntOf(json,L"product.options[i].product_id"); name = CkJsonObjectW_stringOf(json,L"product.options[i].name"); position = CkJsonObjectW_IntOf(json,L"product.options[i].position"); j = 0; count_j = CkJsonObjectW_SizeOfArray(json,L"product.options[i].values"); while (j < count_j) { CkJsonObjectW_putJ(json,j); strVal = CkJsonObjectW_stringOf(json,L"product.options[i].values[j]"); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"product.images"); while (i < count_i) { CkJsonObjectW_putI(json,i); id = CkJsonObjectW_IntOf(json,L"product.images[i].id"); product_id = CkJsonObjectW_IntOf(json,L"product.images[i].product_id"); position = CkJsonObjectW_IntOf(json,L"product.images[i].position"); created_at = CkJsonObjectW_stringOf(json,L"product.images[i].created_at"); updated_at = CkJsonObjectW_stringOf(json,L"product.images[i].updated_at"); width = CkJsonObjectW_IntOf(json,L"product.images[i].width"); height = CkJsonObjectW_IntOf(json,L"product.images[i].height"); src = CkJsonObjectW_stringOf(json,L"product.images[i].src"); j = 0; count_j = CkJsonObjectW_SizeOfArray(json,L"product.images[i].variant_ids"); while (j < count_j) { CkJsonObjectW_putJ(json,j); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"image.product.variant_ids"); while (i < count_i) { CkJsonObjectW_putI(json,i); i = i + 1; } // A sample JSON response body that is parsed by the above code: // { // "product": { // "id": 1071559747, // "title": "Burton Custom Freestyle 151", // "body_html": "<strong>Good snowboard!<\/strong>", // "vendor": "Burton", // "product_type": "Snowboard", // "created_at": "2017-09-22T14:48:43-04:00", // "handle": "burton-custom-freestyle-151", // "updated_at": "2017-09-22T14:48:44-04:00", // "published_at": "2017-09-22T14:48:43-04:00", // "template_suffix": null, // "published_scope": "global", // "tags": "", // "variants": [ // { // "id": 1070325218, // "product_id": 1071559747, // "title": "Default Title", // "price": "0.00", // "sku": "", // "position": 1, // "grams": 0, // "inventory_policy": "deny", // "compare_at_price": null, // "fulfillment_service": "manual", // "inventory_management": null, // "option1": "Default Title", // "option2": null, // "option3": null, // "created_at": "2017-09-22T14:48:44-04:00", // "updated_at": "2017-09-22T14:48:44-04:00", // "taxable": true, // "barcode": null, // "image_id": null, // "inventory_quantity": 1, // "weight": 0.0, // "weight_unit": "lb", // "old_inventory_quantity": 1, // "requires_shipping": true // } // ], // "options": [ // { // "id": 1022828903, // "product_id": 1071559747, // "name": "Title", // "position": 1, // "values": [ // "Default Title" // ] // } // ], // "images": [ // { // "id": 1001473945, // "product_id": 1071559747, // "position": 1, // "created_at": "2017-09-22T14:48:43-04:00", // "updated_at": "2017-09-22T14:48:43-04:00", // "width": 1, // "height": 1, // "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/df3e567d6f16d040326c7a0ea29a4f41.gif?v=1506106123", // "variant_ids": [ // ] // } // ], // "image": { // "id": 1001473945, // "product_id": 1071559747, // "position": 1, // "created_at": "2017-09-22T14:48:43-04:00", // "updated_at": "2017-09-22T14:48:43-04:00", // "width": 1, // "height": 1, // "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/df3e567d6f16d040326c7a0ea29a4f41.gif?v=1506106123", // "variant_ids": [ // ] // } // } // } wprintf(L"Example Completed.\n"); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbReq); CkStringBuilderW_Dispose(sbJson); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.