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
(PureBasic) Shopify Create New ProductCreate a new product
IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i CkRest::ckSetAuthBasic(rest,"SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_SECRET_KEY") success = CkRest::ckConnect(rest,"chilkat.myshopify.com",443,1,1) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) ProcedureReturn EndIf ; The following code creates the JSON request body. ; The JSON created by this code is shown below. jsonReq.i = CkJsonObject::ckCreate() If jsonReq.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(jsonReq,"product.title","Burton Custom Freestyle 151") CkJsonObject::ckUpdateString(jsonReq,"product.body_html","<strong>Good snowboard!</strong>") CkJsonObject::ckUpdateString(jsonReq,"product.vendor","Burton") CkJsonObject::ckUpdateString(jsonReq,"product.product_type","Snowboard") CkJsonObject::ckUpdateString(jsonReq,"product.images[0].attachment","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.i = CkStringBuilder::ckCreate() If sbReq.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckEmitSb(jsonReq,sbReq) CkRest::ckAddHeader(rest,"Content-Type","application/json") sbJson.i = CkStringBuilder::ckCreate() If sbJson.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRest::ckFullRequestSb(rest,"POST","/admin/products.json",sbReq,sbJson) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkJsonObject::ckDispose(jsonReq) CkStringBuilder::ckDispose(sbReq) CkStringBuilder::ckDispose(sbJson) ProcedureReturn EndIf If CkRest::ckResponseStatusCode(rest) <> 201 Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest)) Debug "Response body:" Debug CkStringBuilder::ckGetAsString(sbJson) CkRest::ckDispose(rest) CkJsonObject::ckDispose(jsonReq) CkStringBuilder::ckDispose(sbReq) CkStringBuilder::ckDispose(sbJson) ProcedureReturn EndIf json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(json,sbJson) ; The following code parses the JSON response. ; A sample JSON response is shown below the sample code. productId.i productTitle.s productBody_html.s productVendor.s productProduct_type.s productCreated_at.s productHandle.s productUpdated_at.s productPublished_at.s productTemplate_suffix.i productPublished_scope.s productTags.s productImageId.i productImageProduct_id.i productImagePosition.i productImageCreated_at.s productImageUpdated_at.s productImageWidth.i productImageHeight.i productImageSrc.s i.i count_i.i id.i product_id.i title.s price.s sku.s position.i grams.i inventory_policy.s compare_at_price.i fulfillment_service.s inventory_management.i option1.s option2.i option3.i created_at.s updated_at.s taxable.i barcode.i image_id.i inventory_quantity.i weight.i weight_unit.s old_inventory_quantity.i requires_shipping.i name.s j.i count_j.i strVal.s width.i height.i src.s productId = CkJsonObject::ckIntOf(json,"product.id") productTitle = CkJsonObject::ckStringOf(json,"product.title") productBody_html = CkJsonObject::ckStringOf(json,"product.body_html") productVendor = CkJsonObject::ckStringOf(json,"product.vendor") productProduct_type = CkJsonObject::ckStringOf(json,"product.product_type") productCreated_at = CkJsonObject::ckStringOf(json,"product.created_at") productHandle = CkJsonObject::ckStringOf(json,"product.handle") productUpdated_at = CkJsonObject::ckStringOf(json,"product.updated_at") productPublished_at = CkJsonObject::ckStringOf(json,"product.published_at") productTemplate_suffix = CkJsonObject::ckIsNullOf(json,"product.template_suffix") productPublished_scope = CkJsonObject::ckStringOf(json,"product.published_scope") productTags = CkJsonObject::ckStringOf(json,"product.tags") productImageId = CkJsonObject::ckIntOf(json,"product.image.id") productImageProduct_id = CkJsonObject::ckIntOf(json,"product.image.product_id") productImagePosition = CkJsonObject::ckIntOf(json,"product.image.position") productImageCreated_at = CkJsonObject::ckStringOf(json,"product.image.created_at") productImageUpdated_at = CkJsonObject::ckStringOf(json,"product.image.updated_at") productImageWidth = CkJsonObject::ckIntOf(json,"product.image.width") productImageHeight = CkJsonObject::ckIntOf(json,"product.image.height") productImageSrc = CkJsonObject::ckStringOf(json,"product.image.src") i = 0 count_i = CkJsonObject::ckSizeOfArray(json,"product.variants") While i < count_i CkJsonObject::setCkI(json, i) id = CkJsonObject::ckIntOf(json,"product.variants[i].id") product_id = CkJsonObject::ckIntOf(json,"product.variants[i].product_id") title = CkJsonObject::ckStringOf(json,"product.variants[i].title") price = CkJsonObject::ckStringOf(json,"product.variants[i].price") sku = CkJsonObject::ckStringOf(json,"product.variants[i].sku") position = CkJsonObject::ckIntOf(json,"product.variants[i].position") grams = CkJsonObject::ckIntOf(json,"product.variants[i].grams") inventory_policy = CkJsonObject::ckStringOf(json,"product.variants[i].inventory_policy") compare_at_price = CkJsonObject::ckIsNullOf(json,"product.variants[i].compare_at_price") fulfillment_service = CkJsonObject::ckStringOf(json,"product.variants[i].fulfillment_service") inventory_management = CkJsonObject::ckIsNullOf(json,"product.variants[i].inventory_management") option1 = CkJsonObject::ckStringOf(json,"product.variants[i].option1") option2 = CkJsonObject::ckIsNullOf(json,"product.variants[i].option2") option3 = CkJsonObject::ckIsNullOf(json,"product.variants[i].option3") created_at = CkJsonObject::ckStringOf(json,"product.variants[i].created_at") updated_at = CkJsonObject::ckStringOf(json,"product.variants[i].updated_at") taxable = CkJsonObject::ckBoolOf(json,"product.variants[i].taxable") barcode = CkJsonObject::ckIsNullOf(json,"product.variants[i].barcode") image_id = CkJsonObject::ckIsNullOf(json,"product.variants[i].image_id") inventory_quantity = CkJsonObject::ckIntOf(json,"product.variants[i].inventory_quantity") weight = CkJsonObject::ckIntOf(json,"product.variants[i].weight") weight_unit = CkJsonObject::ckStringOf(json,"product.variants[i].weight_unit") old_inventory_quantity = CkJsonObject::ckIntOf(json,"product.variants[i].old_inventory_quantity") requires_shipping = CkJsonObject::ckBoolOf(json,"product.variants[i].requires_shipping") i = i + 1 Wend i = 0 count_i = CkJsonObject::ckSizeOfArray(json,"product.options") While i < count_i CkJsonObject::setCkI(json, i) id = CkJsonObject::ckIntOf(json,"product.options[i].id") product_id = CkJsonObject::ckIntOf(json,"product.options[i].product_id") name = CkJsonObject::ckStringOf(json,"product.options[i].name") position = CkJsonObject::ckIntOf(json,"product.options[i].position") j = 0 count_j = CkJsonObject::ckSizeOfArray(json,"product.options[i].values") While j < count_j CkJsonObject::setCkJ(json, j) strVal = CkJsonObject::ckStringOf(json,"product.options[i].values[j]") j = j + 1 Wend i = i + 1 Wend i = 0 count_i = CkJsonObject::ckSizeOfArray(json,"product.images") While i < count_i CkJsonObject::setCkI(json, i) id = CkJsonObject::ckIntOf(json,"product.images[i].id") product_id = CkJsonObject::ckIntOf(json,"product.images[i].product_id") position = CkJsonObject::ckIntOf(json,"product.images[i].position") created_at = CkJsonObject::ckStringOf(json,"product.images[i].created_at") updated_at = CkJsonObject::ckStringOf(json,"product.images[i].updated_at") width = CkJsonObject::ckIntOf(json,"product.images[i].width") height = CkJsonObject::ckIntOf(json,"product.images[i].height") src = CkJsonObject::ckStringOf(json,"product.images[i].src") j = 0 count_j = CkJsonObject::ckSizeOfArray(json,"product.images[i].variant_ids") While j < count_j CkJsonObject::setCkJ(json, j) j = j + 1 Wend i = i + 1 Wend i = 0 count_i = CkJsonObject::ckSizeOfArray(json,"image.product.variant_ids") While i < count_i CkJsonObject::setCkI(json, i) i = i + 1 Wend ; 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": [ ; ] ; } ; } ; } Debug "Example Completed." CkRest::ckDispose(rest) CkJsonObject::ckDispose(jsonReq) CkStringBuilder::ckDispose(sbReq) CkStringBuilder::ckDispose(sbJson) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.