Robert Hamilton wrote:
Here’s an answer from a fellow on the internet. It’s a much better answer (IMO). I’ll toss it up here and perhaps it can help someone else in future.
for the book-method, we should both look at: https://help.simplybook.me/index.php...e_methods#book
@
Parameters:
$eventId Integer
$unitId Integer
$date String - in Y-m-d format
$time String - in H:i:s format
$clientData Object - eg. {name: ‘Name’, email: ‘test@gmail.com’, phone: ‘+38099999999’}
$additional array|Object - additional params and fields.
$count Integer - bookings count, min. 1 (using for group bookings batch). (optional)
$batchId Integer - add booking to multiple bookings batch. (optional)
$recurringData Array - make booking recurrent. (optional)
Returns Object.
@
You can (and should) translate this Param-List quite “literally” to the Method in your appropriate “EndPoint-Class” like:
Code:
@
Public Function book( _
ByVal eventId As Long, _
ByVal unitId As Long, _
ByRef sdate As String, _
ByRef stime As String, _
ByRef clientData As cCollection, _
Optional ByRef additional As cCollection, _
Optional ByVal count As Long, _
Optional ByVal batchId As Long, _
Optional ByRef recurringData As cCollection _
) As cCollection
End Function
@
So you see, that you can translate their API-definitions quite directly to VB6, keeping in mind that:
- “Integer” type translates to “ByVal”-Longs
- “String” remains String (Dates are given as ISO-Date-Strings, ISO-time or combined ISO-DateTime-strings as e.g. ‘2018-02-23 23:15:30’
- Object or Array both translate to cCollection (since a cCollection can hold both of these “JSON-NodeTypes”, you can verify what it is via cCollection.IsJSONArray or .IsJSONObject
As for the Optional Params above (additional, count, batchId, recurringData):
- make sure (inside the above VB-method), that you “either fill them with a decent default” (in case they were not passed on the VB-caller-side)
- or simply “don’t pass them along” (into the final JSONRPC-call)
Here the implementation (with the appropriate “validation-checks”):
Code:
@
Public Function book( _
ByVal eventId As Long, _
ByVal unitId As Long, _
ByRef sdate As String, _
ByRef stime As String, _
ByRef clientData As cCollection, _
Optional ByRef additional As cCollection, _
Optional ByVal count As Long, _
Optional ByVal batchId As Long, _
Optional ByRef recurringData As cCollection _
) As cCollection
If clientData Is Nothing Then Err.Raise vbObjectError, , “clientData may not be nothing”
If Not clientData.IsJSONObject Then Err.Raise vbObjectError, , “clientData needs to be passed as a JSON-Object”
If additional Is Nothing Then Set additional = New_c.JSONObject 'ensure an empty JSON-Object
If count = 0 Then 'the optional count-Param was left out, so it is not a batch-call, so we leave the last 3 Params out in the RPC-call below
Set book = New_c.JSONDecodeToCollection(JSONRPC(EndPoint, “book”, eventId, unitId, sdate, stime, clientData, additional))
Else 'it is a Batch-call, so all 3 of the remaining optional Params need to be passed (and be valid)
If recurringData Is Nothing Then Err.Raise vbObjectError, , “recurringData may not be nothing”
If Not recurringData.IsJSONArray Then Err.Raise vbObjectError, , “recurringData needs to be passed as a JSON-Array”
Set book = New_c.JSONDecodeToCollection(JSONRPC(EndPoint, "book", eventId, unitId, sdate, stime, clientData, additional, count, batchId, recurringData))
End If
End Function
@
How to properly fill the arguments which are of type “cCollection” (which as said, can either be a JSON-Object or a JSON-array)
on the outside (from your UserCode) is up to you…
E.g. you could write a “Helper-class” cClientData to build (and later hold) client-relevant Data,
which you filled in from a DB for example (for multiple Clients) - and if you want to pass such a cClientData-instance along into the above defined Method,
you simply have to ensure a “GetJSONObject”-method inside your cClientData-Class, which builds such a thing:
Code:
@
Option Explicit 'a cClientData-Class Helper
Public Name As String, Email As String, Phone As String
Public Sub Init(Name As String, Email As String, Phone As String)
Me.Name = Name
Me.Email = Email
Me.Phone = Phone
End Sub
Public Function GetJSONObject() As cCollection
Set GetJSONObject = new_c.JSONObject
GetJSONObject .Prop(“name”) = Name
GetJSONObject .Prop(“email”) = Email
GetJSONObject .Prop(“phone”) = Phone
End Function
@
You can then fill such a little cClientData Class-Instance by e.g. using the Init-Method (to do it in one line) -
or by setting the Public Properties “Name, Email, Phone” directly - even store multiple “set-up” client-instances in your own VB-Collection or something…
If it comes to passing these Client-Instances to the book-API-method, you will then not pass
the cClientData-reference directly, but instead:
Code:
@
Dim oClientData As New cClientData
oClientData.Init “Fred”, “a@b.com”, “1234567”
Set result = SimplyBook.book(eventId, unitId, sdate, stime, oClientData.GetJSONObject, …)
@
Hope that helps somebody down the line. Full discussion thread can be found here:
http://www.vbforums.com/showthread.php?859239-RESOLVED-Json