Sample code for 30+ languages & platforms
PureBasic

GetHarvest - Get Client Contacts

See more GetHarvest Examples

Returns a list of your contacts.

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

    ; Implements the following CURL command:

    ; curl "https://api.harvestapp.com/v2/contacts" \
    ;   -H "Authorization: Bearer ACCESS_TOKEN" \
    ;   -H "Harvest-Account-Id: ACCOUNT_ID" \
    ;   -H "User-Agent: MyApp (yourname@example.com)"

    CkHttp::ckSetRequestHeader(http,"User-Agent","MyApp (yourname@example.com)")
    CkHttp::ckSetRequestHeader(http,"Authorization","Bearer ACCESS_TOKEN")
    CkHttp::ckSetRequestHeader(http,"Harvest-Account-Id","ACCOUNT_ID")

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

    success = CkHttp::ckQuickGetSb(http,"https://api.harvestapp.com/v2/contacts",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        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)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; Sample JSON response:

    ; {
    ;   "contacts": [
    ;     {
    ;       "id": 4706479,
    ;       "title": "Owner",
    ;       "first_name": "Jane",
    ;       "last_name": "Doe",
    ;       "email": "janedoe@example.com",
    ;       "phone_office": "(203) 697-8885",
    ;       "phone_mobile": "(203) 697-8886",
    ;       "fax": "(203) 697-8887",
    ;       "created_at": "2017-06-26T21:20:07Z",
    ;       "updated_at": "2017-06-26T21:27:07Z",
    ;       "client": {
    ;         "id": 5735774,
    ;         "name": "ABC Corp"
    ;       }
    ;     },
    ;     {
    ;       "id": 4706453,
    ;       "title": "Manager",
    ;       "first_name": "Richard",
    ;       "last_name": "Roe",
    ;       "email": "richardroe@example.com",
    ;       "phone_office": "(318) 515-5905",
    ;       "phone_mobile": "(318) 515-5906",
    ;       "fax": "(318) 515-5907",
    ;       "created_at": "2017-06-26T21:06:55Z",
    ;       "updated_at": "2017-06-26T21:27:20Z",
    ;       "client": {
    ;         "id": 5735776,
    ;         "name": "123 Industries"
    ;       }
    ;     }
    ;   ],
    ;   "per_page": 100,
    ;   "total_pages": 1,
    ;   "total_entries": 2,
    ;   "next_page": null,
    ;   "previous_page": null,
    ;   "page": 1,
    ;   "links": {
    ;     "first": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100",
    ;     "next": null,
    ;     "previous": null,
    ;     "last": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100"
    ;   }
    ; }

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

    per_page.i
    total_pages.i
    total_entries.i
    next_page.s
    previous_page.s
    page.i
    linksFirst.s
    linksNext.s
    linksPrevious.s
    linksLast.s
    i.i
    count_i.i
    id.i
    title.s
    first_name.s
    last_name.s
    email.s
    phone_office.s
    phone_mobile.s
    fax.s
    created_at.s
    updated_at.s
    clientId.i
    clientName.s

    per_page = CkJsonObject::ckIntOf(jResp,"per_page")
    total_pages = CkJsonObject::ckIntOf(jResp,"total_pages")
    total_entries = CkJsonObject::ckIntOf(jResp,"total_entries")
    next_page = CkJsonObject::ckStringOf(jResp,"next_page")
    previous_page = CkJsonObject::ckStringOf(jResp,"previous_page")
    page = CkJsonObject::ckIntOf(jResp,"page")
    linksFirst = CkJsonObject::ckStringOf(jResp,"links.first")
    linksNext = CkJsonObject::ckStringOf(jResp,"links.next")
    linksPrevious = CkJsonObject::ckStringOf(jResp,"links.previous")
    linksLast = CkJsonObject::ckStringOf(jResp,"links.last")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jResp,"contacts")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckIntOf(jResp,"contacts[i].id")
        title = CkJsonObject::ckStringOf(jResp,"contacts[i].title")
        first_name = CkJsonObject::ckStringOf(jResp,"contacts[i].first_name")
        last_name = CkJsonObject::ckStringOf(jResp,"contacts[i].last_name")
        email = CkJsonObject::ckStringOf(jResp,"contacts[i].email")
        phone_office = CkJsonObject::ckStringOf(jResp,"contacts[i].phone_office")
        phone_mobile = CkJsonObject::ckStringOf(jResp,"contacts[i].phone_mobile")
        fax = CkJsonObject::ckStringOf(jResp,"contacts[i].fax")
        created_at = CkJsonObject::ckStringOf(jResp,"contacts[i].created_at")
        updated_at = CkJsonObject::ckStringOf(jResp,"contacts[i].updated_at")
        clientId = CkJsonObject::ckIntOf(jResp,"contacts[i].client.id")
        clientName = CkJsonObject::ckStringOf(jResp,"contacts[i].client.name")
        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure