A QA Engineer’s perspective on API examples

To piggyback on our Halo API Overview posts I’ll periodically add some examples on how to leverage the Halo API using Ruby and Python. Before I begin, I’d like to say that being a QA engineer I’m biased towards using testing frameworks in support of my scripts. Because of that bias these examples will use different Ruby gems and Python modules such as minitest and unittest respectively to create the necessary scaffolding to exercise these Halo API examples.

** NOTE ** Halo API has changed since this blog post was written – please see Halo API Developer Guide and Using the Halo API for details

Using these test frameworks are not necessary when exercising the Halo API when used in production but with my QA hat perpetually on, I like the comfort provided by being able to assert that my API calls contain the data expected before continuing on with whatever task I’m attempting to complete. With having said that, onto the examples!

First, let’s create a Ruby library we’ll call api.rb to simplify multiple GET, POST, PUT and DELETE calls. We’ll reference this api.rb library in future posts so keep it handy. Depending on your system you might have to install some/all of the required gems below.

require 'rest_client'
 require 'minitest/autorun'
 require 'parseconfig'
 require 'json'
class ApiCalls
 # create a base URL and apikey by reading the provided
 # configuration file
 def initialize
 # file containing various params
 my_config ='/usr/src/qa/tests/configs/default.ini')
 my_apikey = "#{my_config.params['test_env']['api_key']}"
 my_grid = "#{my_config.params['test_env']['grid']}"
 my_apiver = "#{my_config.params['test_env']['api_ver']}"
 # setup the full URL
 @base_url =  "https://#{my_grid}/api/#{my_apiver}"
 @apikey = my_apikey
 # return the GET response to the provided URL
 def get(url)
 @resp = RestClient.get("#{@base_url}/#{url}", {:"x-cpauth-access" => @apikey})
 # return the POST response to the provided URL and body
 def post(url, body)
 @resp ="#{@base_url}/#{url}", body, {:content_type => :json,
 :"x-cpauth-access" => @apikey})
 # return the PUT response to the provided URL and body
 def put(url, body)
 @resp = RestClient.put("#{@base_url}/#{url}", body, {:content_type => :json,
 :"x-cpauth-access" => @apikey})
 # return the DELETE response to the provided URL
 def delete(url)
 @resp = RestClient.delete("#{@base_url}/#{url}", {:content_type => :json,
 :"x-cpauth-access" => @apikey})

Notice that I’m using the parseconfig gem to read a file containing a couple of parameters so each script doesn’t need to contain duplicate information. The referenced default.ini file looks like this:

 api_key = 70df40d239c9d42c86b8b7b5f5ba28fc
 grid =
 api_ver = 1

The above api_key value comes from the portal UI under the Settings > Site Administration > API Keys tab and is specific to your Halo account.

Next, let’s create an overly simplified example script to confirm the api.rb lib can be referenced. It may make sense to keep the api.rb file in a separate directory called libs. In my example, it’s two directories removed. (‘../../libs’) We’ll just check that we get a valid response from a simple GET /servers request. Let’s call it get_example.rb

require File.expand_path('../../libs/api', __FILE__)
class TestSampleApiCalls < MiniTest::Unit::TestCase
 def setup
 @api =
 # grab all active servers and
 # assert the GET response code
 # is 200 (OK)
 def test_sample_get
 resp = @api.get("/servers")
 assert_equal 200, resp.code

Running the script should display something similar to the following:

Congratulations! You’ve successfully used Ruby to leverage the Halo API. In future posts we’ll explore using both Ruby and Python with different and more complex ways to explore the Halo API.

— Eric

Stay up to date

Get the latest news and tips on protecting critical business assets.

Related Posts