Jump to content

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
 

Link to post
Share on other sites
  • Moderators

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

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": []
        }
    },
    ......................................................................................
    }
}

 

 

Link to post
Share on other sites
  • Moderators

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

 

 

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);
    }
});

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...