Accessing Provisioning Service with jquery

It’s been a while, at least 5 years, but I’ve eventually ended up back in the bottomless pit of frustration that is javascript. Actually it’s not that bad these days thanks to jquery although it’s still far from what I’d call pleasurable. It wasn’t an entirely voluntary decision for me, my day job requires me to get down and dirty with a web application and that means javascript and jquery.

Anyway I have whipped up a quick little sample to query the SIP Sorcery provisioning service to retireve a list of SIP accounts using jquery. It might be useful to someone and as with the previous samples if there is any interest I’m happy to flesh out the remainder of the REST interface to make it usable.

<html>
 <head>
  <title>SIP Sorcery jquery sample</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   var PROVISIONING_URL = "https://www.sipsorcery.com/provisioning.svc/rest/";
   
   $(document).ready(function() {
     login("yourusername", "yourpassword");
   });

   function login(user, pass) {
     $.get(
      PROVISIONING_URL + "customer/login", 
      { username: user, password: pass },
      function(data){
        getSIPAccounts(data);
      });
   }

   function getSIPAccounts(authid) {
     $.ajax({
      beforeSend: function(req) {
        req.setRequestHeader("AuthID", authid);
      },
      url: PROVISIONING_URL + "sipaccounts?count=3",
      dataType: 'json',
      success: function(data){
        $.each(data, function(index, sipAccount) {
          alert("Username: " + sipAccount.SIPUsername);
        })
      },
      error: function(xhr) {
        alert ("Oopsie: " + xhr.statusText);
      }
      });
   }

  </script>
 </head>
 <body>
  SIP Sorcery jquery test
 </body>
</html>
  1. avimar’s avatar

    I’ve barely used jquery or REST, but I expected something to happen when I put my username & pass in and the path to the jquery.js script.
    I have the full jquery file locally. In chrome I actually see it contacting the server. But nothing at all appears on the screen.
    I’d really like to use this for the CDRs (as I asked here: http://forum.sipsorcery.com/viewtopic.php?f=6&t=2454&p=15048) so if you could get me rolling…

    Thanks!

    Reply

    1. sipsorcery’s avatar

      That’s one of the big turn offs for javascript: every browser behaves differently and one can spend a huge amount of time trying to sort out cross browser issues. As an aside the highest paid job I ever had was in 2001 (before the dotcom bubble burst) as a contract programmer working solely on resolving javascript compatability issues between IE and Netscape. I only lasted 4 months but thankfully that was enough for a six week holiday in South America :).

      I’m yet to install chrome and a quick test with Firefox fails as well so looks like IE is the only browser the sample will work with.

      Reply

    2. avimar’s avatar

      Isn’t the point of jQuery to be cross-browser compatible?
      Anyway, it works in MSIE.
      However, I’m pretty clumsy in Javascript. Is SOAP using JSON/xml? a POST? I’m trying to use PHP to do stuff, but after about 2 hours of googling and trying stuff, I’ve got nothing. Can’t even get it to login.

      Reply

      1. sipsorcery’s avatar

        I thought that was the point of jquery as well but I’m no expert in it and have spent around a total of 3 hours mucking around with it in my life.

        SOAP uses XML and requires that the HTTP request be sent as a POST (in most cases a SOAP request would be too big for a GET).

        However for interop with non .Net languages such as PHP you are better off using the REST interface. It’s an alternative to SOAP and can use HTTP GET or POST requests and the format of the responses is JSON.

        Reply

      2. avimar’s avatar

        ” if there is any interest Iā€™m happy to flesh out the remainder of the REST interface to make it usable.”
        Hmm, so can you skip the actual jquery/ruby code and just tell me what URL needs to be contacted with what information in POSTed/or GET so I can give it a try? For login, with auth-code, for CDR… If you get me started, I’ll put up actual documentation in the wiki.
        Thanks!
        -Avi Marcus

        Reply

        1. sipsorcery’s avatar

          The complete SOAP service is https://www.sipsorcery.com/provisioning.svc (the service comes with WSDL metadata which fully describes the methods but it’s designed for machines not humans).

          The REST interface is not complete. Two of the methods that are available are the ones in the above jquery sample:

          Login: https://www.sipsorcery.com/provisioning.svc/rest/customer/login?username=user&password=pass

          Get SIP Accounts: https://www.sipsorcery.com/provisioning.svc/rest/sipaccounts?count=3 (the authid value from the login request needs to be set as an HTTP authid header or as a cookie).

          The REST requests can be GET or POST.

          Reply

        2. avimar’s avatar

          Yey, I got it working with PHP. Apparently my initial sever had cURL but it wasn’t configured properly for SSL and didn’t give an error message.
          Both those functions work. Can you enable CDR and current call listings?
          Thanks!

          Reply

        3. avimar’s avatar

          So I was looking here: http://sipsorcery.codeplex.com/sourcecontrol/changeset/view/25763?projectName=sipsorcery#474955
          And apparently getcdrs works in the REST interface. XML, rather than JSON, but php can deal with it. (The i:nil=”true” and the xmlns:a=”http://schemas.datacontract.org/2004/07/System”> it kinda chocked on, though)

          So I guess I’m all set with the CDRs. There’s so much data for each row, I’m having a hard time figuring out exactly what I wanted to do with it!

          Reply

          1. sipsorcery’s avatar

            I think you may be able to add an format=json parameter to get the data back as JSON instead of XML. I wil set the default format to JSON next time I update the web service.

            Can’t help you with what to do with the data :).

            Reply

          2. avimar’s avatar

            https://www.sipsorcery.com/provisioning.svc/rest/getcdrs?format=json&count=10
            Is the same XML as
            https://www.sipsorcery.com/provisioning.svc/rest/getcdrs?count=10

            so, uh, what did you mean?

            And I think I got it – I’ve been trying to save debug info & create a nice CDR list at the same time. If I just try to do one at a time, it will be much easier. And look better šŸ™‚

            Reply

          3. sipsorcery’s avatar

            Ok the format=json parameter doesn’t seem to work after all, I faintly recalled it was provided for automatically by the .Net’s WCF. I’ll do some more investigation.

            Reply

Reply

Your email address will not be published. Required fields are marked *