[#117] Book Response

Migrated from Redmine #117 | Author: Robert Hamilton
Status: Closed | Priority: Normal | Created: 2018-02-22


I’m reading the documentation for the Book API and I see it says “See [[#book response|example]] of book API method response.”

It’s not very clear and I can’t find the response that’s mentioned.

Where would I find the #book response example mentioned in the documentation?

Do you have a code example for using the Book api? Something like you have for the GetToken? What are you expecting in the body?

@var loginClient = new JSONRpcClient({
‘url’: ‘//user-api.simplybook.me/login’,
‘onerror’: function (error) {
alert(error);
}
});
var token = loginClient.getToken(‘mib’, ‘f43618e37b82004066d60db3431f4a06392599a6cfcafa8268bf25becc0ec7d7’);@

A bit more detail than just cutting and pasting a link to the documentation would be appreciated.

Redmine Admin wrote:

You can check example here API documentation | SimplyBook.me Online Scheduling

Robert Hamilton wrote:

Wow. That’s probably the worst and most lazy answer to a customer support question since the last time I asked you for help.

Did you even read the question? Because the last line was:

“A bit more detail than just cutting and pasting a link to the documentation would be appreciated.”

Your documentation says: “See example of book API method response.”

Where can I find that information?

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

Redmine Admin wrote:

There is full raw http responce of all “basic” API methods we have is available by this link. Please check it again and go trough wizard we made.

Redmine Admin wrote:

Code examples and responce examples are available by link I mention in first answer link Screenshot by Lightshot
It will guide you trough all login (getting tokens) and book process (with book function example and its responce example).
We do not have VB examples yet unfortunately.

Robert Hamilton wrote:

The code examples stop short of the Book method. GetAvailableTimeSlots is the last code example on your explorer. Or if there is an example of the Book method and it’s response example I can’t find it. If you can find it please let me know where.

Robert Hamilton wrote:

I’m trying to get the Book method to work to mark off a spot on the calendar for a person who has phoned the office directly and not booked in through the web site. I want my web calendar to be updated to show the spot is no longer available to avoid double bookings.

I know you don’t have VB examples but I’m sending my company name and company token and using the public URL https://user-api.simplybook.me/

I’m passing the following items:

eventId = 1
unitId = 1
sdate = “2018-02-23”
stime = “09:00”

Params “Fred Henderson”, “test@gmail.com”, “4805551212”

@URL = https://user-api.simplybook.me/
http.Open “POST”, URL, False
http.SetRequestHeader “Content-Type”, “application/json; charset=UTF-8”
http.SetRequestHeader “Accept”, “application/json”

http.SetRequestHeader “X-Company-Login”, MyCompanyHere
http.SetRequestHeader “X-Token”, mytokenhere

JSONBody.Prop(“jsonrpc”) = “2.0”
JSONBody.Prop(“method”) = “book”
JSONBody.Prop(“id”) = id
JSONBody.Prop(“params”) = JSONParams@

I know the company name and token are correct because they work with the getbooking method. I also have a user name and user token but I don’t think I should be using them for the public URL.

The error I’m getting is:

{“error”:{“code”:-32068,“message”:“Client authorization required”,“data”:},“id”:“4”,“jsonrpc”:“2.0”}

Does that error mean my token is wrong or does it mean I need the authorization of the person (Fred Henderson)? I just thought I’d use a dummy person to mark the spot as full. Any ideas?

Redmine Admin wrote:

Robert Hamilton wrote:

The code examples stop short of the Book method. GetAvailableTimeSlots is the last code example on your explorer. Or if there is an example of the Book method and it’s response example I can’t find it. If you can find it please let me know where.

You need to pick day and time to go to next steps and reach book step Screenshot by Lightshot

Redmine Admin wrote:

Robert Hamilton wrote:

Does that error mean my token is wrong or does it mean I need the authorization of the person (Fred Henderson)? I just thought I’d use a dummy person to mark the spot as full. Any ideas?

It seems you have client login plugin enabled and need client’s authorization. Turn off client login feature or use administrative API book function Company administration service methods - Company administration service methods - SimplyBook.me

Robert Hamilton wrote:

This office needs the user to set up an account so they aren’t having abuse on the booking site.

The admin function you described above worked very well.

When a client calls the office directly and books with the receptionist my software will make a call out to the simplybook web site and block the spot with a dummy booking for a made-up person. In my case clientid 4 is the dummy client named “Booked”.

Later when my program calls to the web sites to look for bookings it will ignore all bookings for clientid 4 and sync the calendar.

Will it cause a problem on your system if one specific client (clientID 4) has hundreds or possibly thousands of bookings all acting as a place holder?

Redmine Admin wrote:

Robert Hamilton wrote:

Will it cause a problem on your system if one specific client (clientID 4) has hundreds or possibly thousands of bookings all acting as a place holder?

No, should be fine