Jump to content
IElite

Wrapping javascript library or ASM blocks ?

Recommended Posts


I am trying to use the API of the following website

The API uses JSON-RPC 2.0 protocol.

The API documentation is here

They also provide a javascript library

If I utilize this library in my SMS project, I can make the calls and get it to work, utlizing ASM block

asm
 var loginClient = new JSONRpcClient({
    'url': 'https://user-api.simplybook.me' + '/login',
    'onerror': function (error) {},
});

var token = loginClient.getToken('ACME', '88e24ae6c5cb30d9205ffc05b490daef8ddd189fefc');

var client = new JSONRpcClient({
    'url': '//user-api.simplybook.me',
    'headers': {
        'X-Company-Login': 'ACME',
        'X-Token': token
    },
    'onerror': function (error) {
        alert(error);
    }
});
console.log(JSON.stringify(client));

end;

However, I am wondering if it is possible to create a Smart Pascal wrapper for this unit, so that I can call these in pascal and not have to worry about jumping back and forth between ASM block and pascal blocks of code?

If so, can some one provide me an example of one of these?

Are these two the only thing needing wrapper for?

 

var JSONRpcClientException = function (code, message) {
    this.code = code;
    this.message = message;
}
var JSONRpcClient = function (options) {
    this.setOptions(options);
    this.init();
}

Thanks
 

Share this post


Link to post
Share on other sites

Having a cursory view on this library, what you probably want to end up with is a pascal variable called 'client', which you can use outside asm blocks.

This will allow you to issue calls like client.getEventList() etc.

to do that you can do something like this

var client : variant := new JObject;

asm
@client = new JSONRpcClient({
    'url': '//user-api.simplybook.me',
    'headers': {
        'X-Company-Login': 'ACME',
        'X-Token': token
    },
    'onerror': function (error) {
        alert(error);
    }
});
end;

var EventList: variant := client.getEventList(-params-);

rule of thumb: the creation of js objects via the 'new' constructor need to be kept in an asm block

Share this post


Link to post
Share on other sites

I see to still be having trouble with that last line

var EventList: variant := client.getEventList(-params-);

in their example online, you do not have to pass params, they are optional

var client = new JSONRpcClient({
'url': '//user-api.simplybook.me',
    'headers': {
    'X-Company-Login': '{companyLogin}',
    'X-Token': '{token}'

},

'onerror': function (error) {

   alert(error);
}

});


var events = client.getEventList();

I have this code outside my asm block

 writeln('ok');
  var EventList: variant := client.getEventList();
  writeln('ok');

 

the first OK is written, but the second is not

 

The EventList should be filled with a large block of JSON

e.g.

{
    "1": {
        "id": "1",
        "name": "Часовая фотосесcия",
        "duration": "60",
        "buffertime_before": "0",
        "buffertime_after": "0",
        "hide_duration": "0",
        "description": "Если Вы хотите попробовать себя в роли модели, но не определились с образом. 10 отретушированных и готовых к печати фотографий + отснятый материал на диске",
        "picture": "03661dd2903a3b3707807e30676821ae.jpg",
        "is_public": "1",
        "is_active": "1",
        "position": "0",
        "file_id": "1",
        "seo_url": null,
        "is_recurring": "0",
        "picture_sub_path": "/image_files",
        "is_visible": "1",
        "picture_path": "/uploads/mib/image_files/preview/03661dd2903a3b3707807e30676821ae.jpg",
        "unit_map": {
            "1": null
        },
        "classes_plugin_info": {
            "unit_group_binded_in_class": null,
            "unit_groups_binded_in_classes": []
        }
    },
    ......................................................................................
    }
}

 

 

Share this post


Link to post
Share on other sites

it works fine

you have to add these lines to your index.html file

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="json-rpc-client.js"></script>

and get this json-rpc-client.js file physically in the same directory as your index file

code I used :

procedure TForm1.W3Button2Click(Sender: TObject);
begin
var client : variant := new JObject;
asm
@client = new JSONRpcClient({
    'url': 'https://user-api.simplybook.me',
    'headers': {
        'X-Company-Login': 'lynkfs',
        'X-Token': '89d85d820355.....................57eae4a72579bb'
    },
    'onerror': function (error) {}
});
end;
var services : variant := client.getEventList();
writeln(JSON.stringify(services));
//{"1":{"id":"1","name":"LynkFS","duration":"60","buffertime_before":"0","buffertime_after":"0","hide_duratio....
end;

use either 'http' or 'https' for both the links to '//user-api.simplybook.me' and '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js' , depending on what your server is set up for

 

 

Share this post


Link to post
Share on other sites

hmm, ok i will give your code a try.

I added theose to javascript files to my project and then added them to a unit called api

unit api;

interface

{$R "jquery-3.3.1.js"}
{$R "json-rpc-client.js"}

and then included the api unit in my main form

 

Share this post


Link to post
Share on other sites

Seems like what i posted in my last post, works as well...i.e., using my api unit

The issue was with the first section of code and the token I guess

I assumed you had to login first, and get the token, and then use the token to get the Client object ????

like this:

 var loginClient = new JSONRpcClient({
    'url': 'https://user-api.simplybook.me' + '/login',
    'onerror': function (error) {},
});

var token = loginClient.getToken('ACME', '88e24ae6c5cb30d9205ffc05b.........d189fefc8d09eb64752d5433994d0');

is that not correct?

It looks like you hardcoded the token ?

i was assigning that token variable instead

like

@client = new JSONRpcClient({
    'url': '//user-api.simplybook.me',
    'headers': {
        'X-Company-Login': 'Acme',
        'X-Token': token
    },
    'onerror': function (error) {
        alert(error);
    }
});

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×