PureBasic
PureBasic
GetHarvest - List Clients
See more GetHarvest Examples
Returns a list of your clients.Chilkat PureBasic Downloads
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/clients" \
; -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/clients",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:
; {
; "clients": [
; {
; "id": 5735776,
; "name": "123 Industries",
; "is_active": true,
; "address": "123 Main St.\r\nAnytown, LA 71223",
; "created_at": "2017-06-26T21:02:12Z",
; "updated_at": "2017-06-26T21:34:11Z",
; "currency": "EUR"
; },
; {
; "id": 5735774,
; "name": "ABC Corp",
; "is_active": true,
; "address": "456 Main St.\r\nAnytown, CT 06467",
; "created_at": "2017-06-26T21:01:52Z",
; "updated_at": "2017-06-26T21:27:07Z",
; "currency": "USD"
; }
; ],
; "per_page": 100,
; "total_pages": 1,
; "total_entries": 2,
; "next_page": null,
; "previous_page": null,
; "page": 1,
; "links": {
; "first": "https://api.harvestapp.com/v2/clients?page=1&per_page=100",
; "next": null,
; "previous": null,
; "last": "https://api.harvestapp.com/v2/clients?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
name.s
is_active.i
address.s
created_at.s
updated_at.s
currency.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,"clients")
While i < count_i
CkJsonObject::setCkI(jResp, i)
id = CkJsonObject::ckIntOf(jResp,"clients[i].id")
name = CkJsonObject::ckStringOf(jResp,"clients[i].name")
is_active = CkJsonObject::ckBoolOf(jResp,"clients[i].is_active")
address = CkJsonObject::ckStringOf(jResp,"clients[i].address")
created_at = CkJsonObject::ckStringOf(jResp,"clients[i].created_at")
updated_at = CkJsonObject::ckStringOf(jResp,"clients[i].updated_at")
currency = CkJsonObject::ckStringOf(jResp,"clients[i].currency")
i = i + 1
Wend
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure