Share this link

    Code Samples

    2471 0 Created on 2020-10-09 07:19:31; Last updated on 2022-07-01 13:01:14

    Below, find examples written in different programming languages to show how to use the Babelway REST API within your application. These are just starting points to build upon.

    Java

    Download the last 25 messages in ERROR. It requires using Apache Commons httpclient, codec and logging for HTTP calls and SAX for XML parsing

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class RESTClient {
    
     public static void main(String[] args) throws Exception {
    
      String baseUrl = "https://www.babelway.net/SelfService3/rest/v2/";
      Long myHub = 0L; // You can find this on the My Account page
      String user = "";// the username you use to connect to Babelway
      String pwd = ""; // the password you use to connect to Babelway
    
      // Download the last 25 messages in ERROR
      String request  = baseUrl   "hub-"   myHub   "/messages.xml?status=ERROR";
      HttpClient client = new HttpClient();
      client.getState().setCredentials(AuthScope.ANY_REALM, "www.babelway.net", new UsernamePasswordCredentials(user, pwd));
      GetMethod method = new GetMethod(request);
    
      // Send the request
      int statusCode = client.executeMethod(method);
    
      // read the result
      if (statusCode == 200) {
       InputStream rstream = null;
       rstream = method.getResponseBodyAsStream();
       BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
    
       SAXParser sxp = SAXParserFactory.newInstance().newSAXParser();
       sxp.parse(rstream, new SaxHandler());
    
      }
     }
     private static final class SaxHandler extends DefaultHandler {
      // invoked when document-parsing is started:
      public void startDocument() throws SAXException {
       System.out.println("Document processing started");
      }
    
      // notifies about finish of parsing:
      public void endDocument() throws SAXException {
       System.out.println("Document processing finished");
      }
    
      // we enter to element 'qName':
      public void startElement(String uri, String localName,
        String qName, Attributes attrs) throws SAXException {
       // Do your processing
       System.out.println("Element "   qName);
      }
    
      // we leave element 'qName' without any actions:
      public void endElement(String uri, String localName, String qName)
      throws SAXException {
       // do nothing;
      }
     }
    }
    C#

    using System;
    using System.Text;
    using System.Net;
    using System.IO;
    
    namespace RESTClient
    {
    class Program
    {
    static void Main(string[] args)
    {
    string baseURL = "https://www.babelway.net/SelfService3/rest/v2/";
    long myHub = 0; // You can find this on the My Account page
    String user = "";// the username you use to connect to Babelway
    String pwd = ""; // the password you use to connect to Babelway            
    
    // Download the last 25 messages in ERROR
    String request = baseURL   "hub-"   myHub   "/messages.xml?status=ERROR";
    
    HttpWebRequest webRequest = WebRequest.Create(request) as HttpWebRequest;
    
    // Add authentication to request
    webRequest.Credentials = new NetworkCredential(user, pwd);
    
    // Get Response
    HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
    
    // Read the result
    if (webRequest.HaveResponse == true && response != null)
    {
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());
    
    // Console application output  
    Console.WriteLine(reader.ReadToEnd());
    }
    }
    }
    }

    Python

    This code snippet requires Python 2.6 or newer. No extra modules are required

    import json
     import urllib2
    
     base_url = "https://www.babelway.net/SelfService3/rest/v2/";
     my_hub = 0 #You can find this on the My Account page
     user = "" #the username you use to connect to Babelway
     pwd = "" #the password you use to connect to Babelway
       
     request = "%shub-%s/messages.json" %(base_url, my_hub)
    
     # Create a pwd manager that handles any realm (default realm)
     pwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
    
     # Create the authentication handler, add the login/pwd to it
     auth_handler = urllib2.HTTPBasicAuthHandler(pwd_manager)
     auth_handler.add_password(None, request, user, pwd)
    
     # Build the URL Opener
     opener = urllib2.build_opener(auth_handler)
     urllib2.install_opener(opener)
    
     # Read and parse the JSON response
     try:
      f = urllib2.urlopen(request)
      print json.load(f)
     except urllib2.HTTPError as e:
      print e
     
    PHP

    This code snippet requires PHP version 5.2.0 or newer. No extra modules are required.

    $base_url = "https://www.babelway.net/SelfService3/rest/v2/";
     $my_hub = 0; // You can find this on the My Account page : 
     $user = ""; // the username you use to connect to Babelway
     $pwd = ""; // the password you use to connect to Babelway
    
     // Download all your tickets for the month of July 2010
     $from = mktime(0, 0, 0, 7, 1, 2010); 
     $to = mktime(23, 59, 59, 7, 31, 2010);
     
     // Generate the URL for the tickets:
     $url = $base_url . "hub-" . $my_hub . "/messages.json?since=" . $from . "&before=" . $to;
     
     // Make the REST query
     $context = stream_context_create(array( 
        'http' => array( 
          'method'  => 'GET', 
          'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($user.':'.$pwd))."\r\n", 
          'timeout' => 5, 
     ), 
     )); 
     $content = file_get_contents($url, false, $context);
     
     // Decode the JSON
     $data = json_decode($content);
    
     // Display them
     echo '
    ';
     print_r($data);
     echo '
    '; ?>

    

    

    

    

    0 people found this helpful.

    Related Articles