Connect to the DX API with...
  • 29 Mar 2024
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

Connect to the DX API with...

  • Dark
    Light
  • PDF

Article Summary

Invoke the Webtrends Data Extraction API with Ruby

A Ruby code sample and method call for the Webtrends REST Data Extraction API.

def query(username, password, query)

     hostname = "myDomain.com"
    
     https =  Net::HTTP.new(hostname, Net::HTTP.https_default_port)
     https.use_ssl = true
     https.ca_file = "/usr/share/curl/curl-ca-bundle.crt"

     resp = https.start { |http|
      
       request = Net::HTTP::Get.new("/v2_0/ReportService/" + query)
     
       request.basic_auth username, password
           
        res = http.request(request)
 
        case res
 
        when Net::HTTPSuccess, Net::HTTPRedirection
         
          return  res.body
       
        else
       
          res.error!
       
        end
       
      }
     
end

The method call would look something like this:

@foo = query("My User Name", "My Password", "profile/?format=json")

Connecting to the Data Extraction API with .NET

Asynchronous calls are recommended, because they minimize wait time for users.

In an asynchronous call, the calling thread initiates the call and continues executing without waiting for a response. When the response is returned, an event fires and the returned data can then be handled in some way. For client service applications, server based applications, or anything that needs to return control to an application thread, this is the preferred call to use because it provides smoother, more continuous access to the end user.

Synchronous calls should only be used for processing that doesn't involve user interaction. A synchronous call executes on the application thread and holds it until a response is returned, so the user interface appears to hang or freeze.

Here's an example of an asynchronous call in C# against the Webtrends Data Extraction API (also showing how to set up authentication credentials).

Note: This is an example only, and not intended to endorse writing tests with Thread.Sleep calls in them!

[TestClass]
public class GetRestServices
{
  private XDocument doc;
 
  private void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  {
     doc = XDocument.Parse(e.Result);
  }
 
  [TestMethod]
  public void TestAsynchronousCallWithSecurity()
  {
    const string baseUri = "https://myDomain.com/v2_0/ReportService/profiles/?format=xml";
 
    var svc = new WebClient();
    svc.Credentials = new NetworkCredential("WebTrendsUserName", "yourSuperSecretPassword");
    svc.DownloadStringCompleted += svc_DownloadStringCompleted;
    svc.DownloadStringAsync(new Uri(baseUri));
 
    Thread.Sleep(3000);
    Assert.IsNotNull(doc);
    var docTest = new XDocument();
    Assert.IsInstanceOfType(doc, docTest.GetType());
  }
}

Was this article helpful?

What's Next