Sample code for 30+ languages & platforms
PureBasic

Shopify Get all products, showing only some attributes

See more Shopify Examples

Get all products, showing only some attributes

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_KEY")

    success = CkRest::ckConnect(rest,"chilkat.myshopify.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/admin/products.json?fields=id,images,title",sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response body:"
        Debug CkStringBuilder::ckGetAsString(sbJson)
        CkRest::ckDispose(rest)
        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.
    i.i
    count_i.i
    id.i
    title.s
    j.i
    count_j.i
    product_id.i
    position.i
    created_at.s
    updated_at.s
    width.i
    height.i
    src.s
    k.i
    count_k.i
    intVal.i

    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"products")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        id = CkJsonObject::ckIntOf(json,"products[i].id")
        title = CkJsonObject::ckStringOf(json,"products[i].title")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"products[i].images")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            id = CkJsonObject::ckIntOf(json,"products[i].images[j].id")
            product_id = CkJsonObject::ckIntOf(json,"products[i].images[j].product_id")
            position = CkJsonObject::ckIntOf(json,"products[i].images[j].position")
            created_at = CkJsonObject::ckStringOf(json,"products[i].images[j].created_at")
            updated_at = CkJsonObject::ckStringOf(json,"products[i].images[j].updated_at")
            width = CkJsonObject::ckIntOf(json,"products[i].images[j].width")
            height = CkJsonObject::ckIntOf(json,"products[i].images[j].height")
            src = CkJsonObject::ckStringOf(json,"products[i].images[j].src")
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(json,"products[i].images[j].variant_ids")
            While k < count_k
                CkJsonObject::setCkK(json, k)
                intVal = CkJsonObject::ckIntOf(json,"products[i].images[j].variant_ids[k]")
                k = k + 1
            Wend
            j = j + 1
        Wend
        i = i + 1
    Wend

    ; A sample JSON response body that is parsed by the above code:

    ; {
    ;   "products": [
    ;     {
    ;       "id": 632910392,
    ;       "title": "IPod Nano - 8GB",
    ;       "images": [
    ;         {
    ;           "id": 850703190,
    ;           "product_id": 632910392,
    ;           "position": 1,
    ;           "created_at": "2017-09-22T14:08:02-04:00",
    ;           "updated_at": "2017-09-22T14:08:02-04:00",
    ;           "width": 123,
    ;           "height": 456,
    ;           "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
    ;           "variant_ids": [
    ;           ]
    ;         },
    ;         {
    ;           "id": 562641783,
    ;           "product_id": 632910392,
    ;           "position": 2,
    ;           "created_at": "2017-09-22T14:08:02-04:00",
    ;           "updated_at": "2017-09-22T14:08:02-04:00",
    ;           "width": 123,
    ;           "height": 456,
    ;           "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano-2.png?v=1506103682",
    ;           "variant_ids": [
    ;             808950810
    ;           ]
    ;         }
    ;       ]
    ;     },
    ;     {
    ;       "id": 921728736,
    ;       "title": "IPod Touch 8GB",
    ;       "images": [
    ;       ]
    ;     }
    ;   ]
    ; }

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure