Sample code for 30+ languages & platforms
PureBasic

MercadoLibre - Consultar mis datos personales

See more MercadoLibre Examples

Consultar mis datos personales

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    ; First get our previously obtained OAuth2 access token.
    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/mercadolibre.json")

    ; Implements the following CURL command:

    ; curl - X GET https://api.mercadolibre.com/users/me?access_token=$ACCESS_TOKEN

    ; Use the following online tool to generate HTTP code from a CURL command
    ; Convert a cURL Command to HTTP Source Code

    CkHttp::ckSetUrlVar(http,"access_token",CkJsonObject::ckStringOf(jsonToken,"access_token"))

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

    success = CkHttp::ckQuickGetSb(http,"https://api.mercadolibre.com/users/me?access_token={$access_token}",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

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

    ; {
    ;   "id": 202593498,
    ;   "nickname": "TETE2870021",
    ;   "registration_date": "2016-01-06T11: 31: 42.000-04: 00",
    ;   "country_id": "AR",
    ;   "address": {
    ;     "state": "AR-C",
    ;     "city": "Palermo"
    ;   },
    ;   "user_type": "normal",
    ;   "tags": [
    ;     "normal",
    ;     "test_user",
    ;     "user_info_verified"
    ;   ],
    ;   "logo": null,
    ;   "points": 100,
    ;   "site_id": "MLA",
    ;   "permalink": "http://perfil.mercadolibre.com.ar/TETE2870021",
    ;   "seller_reputation": {
    ;     "level_id": null,
    ;     "power_seller_status": null,
    ;     "transactions": {
    ;       "period": "historic",
    ;       "total": 0,
    ;       "completed": 0,
    ;       "canceled": 0,
    ;       "ratings": {
    ;         "positive": 0,
    ;         "negative": 0,
    ;         "neutral": 0
    ;       }
    ;     }
    ;   },
    ;   "buyer_reputation": {
    ;     "tags": [
    ;     ]
    ;   },
    ;   "status": {
    ;     "site_status": "active"
    ;   }
    ; }

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

    strVal.s

    id.i = CkJsonObject::ckIntOf(jResp,"id")
    nickname.s = CkJsonObject::ckStringOf(jResp,"nickname")
    registration_date.s = CkJsonObject::ckStringOf(jResp,"registration_date")
    country_id.s = CkJsonObject::ckStringOf(jResp,"country_id")
    addressState.s = CkJsonObject::ckStringOf(jResp,"address.state")
    addressCity.s = CkJsonObject::ckStringOf(jResp,"address.city")
    user_type.s = CkJsonObject::ckStringOf(jResp,"user_type")
    logo.s = CkJsonObject::ckStringOf(jResp,"logo")
    points.i = CkJsonObject::ckIntOf(jResp,"points")
    site_id.s = CkJsonObject::ckStringOf(jResp,"site_id")
    permalink.s = CkJsonObject::ckStringOf(jResp,"permalink")
    seller_reputationLevel_id.s = CkJsonObject::ckStringOf(jResp,"seller_reputation.level_id")
    seller_reputationPower_seller_status.s = CkJsonObject::ckStringOf(jResp,"seller_reputation.power_seller_status")
    seller_reputationTransactionsPeriod.s = CkJsonObject::ckStringOf(jResp,"seller_reputation.transactions.period")
    seller_reputationTransactionsTotal.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.total")
    seller_reputationTransactionsCompleted.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.completed")
    seller_reputationTransactionsCanceled.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.canceled")
    seller_reputationTransactionsRatingsPositive.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.ratings.positive")
    seller_reputationTransactionsRatingsNegative.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.ratings.negative")
    seller_reputationTransactionsRatingsNeutral.i = CkJsonObject::ckIntOf(jResp,"seller_reputation.transactions.ratings.neutral")
    statusSite_status.s = CkJsonObject::ckStringOf(jResp,"status.site_status")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"tags")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        strVal = CkJsonObject::ckStringOf(jResp,"tags[i]")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jResp,"buyer_reputation.tags")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonToken)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure