Integration Code Samples

C

This example uses libcurl, the well-known c library for transferring data using http protocol.

//Initialize CURL
CURL* c;
c = curl_easy_init();

//this will make curl report an error when http code greater or equal than 400 is returned
curl_easy_setopt(c, CURLOPT_FAILONERROR, 1);

//AlertGrid http endpoint for saving signals
curl_easy_setopt(c, CURLOPT_URL, "http://hq.alert-grid.com/save-signal/"); 

//this will follow redirections e.g. 302 'moved permanently' instead of returning immediately 
curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1);

//this will make curl report an error when http code greater or equal than 400 is returned
curl_easy_setopt(c, CURLOPT_FAILONERROR, 1);  

//curl_easy_setopt(c, CURLOPT_VERBOSE, 1); //uncomment this if you want to see all details of http request and response printed to stdout
	
//setting POST data with three extra parameters: city, temperature and snow: 
curl_easy_setopt(c, CURLOPT_POSTFIELDS, "api_id=4a19d491-d012-43c8-80a1-49a7e86ab814&receiver_name=myrcv"); 

//do the http request. Curl will print response to standard output.
CURLcode result = curl_easy_perform(c);  

C#

C# offers classes for making HTTP requests “out of the box”. HttpWebRequest class has all what you need. First you call a GetRequestStream(). It returns a Stream object to which you Write() content that you want to send. Then you call GetResponse() object that returns HttpWebResponse object, which has GetResponseStream(). This method returns Stream on which you call Read() to get the data back from the server.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

static string BuildParamString(IDictionary<string, string> parameters)
{
    StringBuilder sb = new StringBuilder();
    bool first = true;
    foreach (KeyValuePair<string, string> kv in parameters)
    {
        if (!first)
            sb.Append('&');
        else
            first = false;
        sb.Append(HttpUtility.UrlEncode(kv.Key));
        sb.Append('=');
        sb.Append(HttpUtility.UrlEncode(kv.Value));
    }
    return sb.ToString();
}


static void Main(string[] args)
{
    IDictionary<string, string> parameters = new Dictionary<string, string>();
    string alertGridEndpoint = "http://hq.alert-grid.com/save-signal/";
    string api_id = "3243c8ae-bfa3-4312-a226-1cf332d6bf0b"; //your AlertGrid API id
    string receiverName = "myrcv"; //default Receiver

    // always include the mandatory params
    parameters.Add("api_id", api_id);
    parameters.Add("receiver_name", receiverName);

    // build POST data string and convert it to byte array, using ASCII encoding
    byte[] postData = new ASCIIEncoding().GetBytes(BuildParamString(parameters));

    // prepare the requerst object and set its properties
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(alertGridEndpoint);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postData.Length;

    //write the post data to the request
    using (Stream newStream = request.GetRequestStream())
    {
        newStream.Write(postData, 0, postData.Length);
    }

    // execute the request
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

}

PHP

PHP offers very easy way of sending http request, you just need to have a http extension enabled. Use HttpRequest class to build a request object, then addPostFields to add parameters, then send().

// You must install the HTTP extension
// See http://www.php.net/manual/en/book.http.php

// Create the request object. We'll use POST method
$r = new HttpRequest('http://hq.alert-grid.com/save-signal/', HttpRequest::METH_POST);
// Add POST fields. company_key and receiver_name are mandatory, you can pass your own values as well. 
// Just remember to url-encode them with urlencode() function

$r->addPostFields(array('api_id' => urlencode('4a19d491-d012-43c8-80a1-49a7e86ab814'), 'receiver_name' => 'myrcv'));
$r->setContentType("application/x-www-form-urlencoded");
// Make the request and print the response
try {
    echo $r->send()->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

Python

Python has a nice and easy to use HTTPConnection class that you can find in httplib package. Urlencode function allows for easy encoding of the parameter values.

import httplib
import urllib

api_id = "4a19d491-d012-43c8-80a1-49a7e86ab814"
receiver_name = "myrcv"
host = "hq.alert-grid.com"
url = "/save-signal/"
data = {}

conn = None
try:
    #Always include two mandatory parameters
    data['api_id'] = api_id
    data['receiver_name'] = receiver_name
    #You can also pass extra parameters
    #data['my_param'] = my_value

    #URL-encode the data
    params = urllib.urlencode(data)
    
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
    conn = httplib.HTTPConnection(host)
    conn.request("POST", url, params, headers)
    response = conn.getresponse()
    response_text = response.read()   
except Exception, ex:
    print 'Paste your exception handling code'
finally:
    if conn:
        conn.close()

Ruby

Ruby offers an easy-to use HTTP class to send requests, include it from ‘net/http’, and call post_form() method. Use URI.escape() function to url-encode variable values.

    
require 'net/http'
require 'uri'

#Sending Signal to AlertGrid. 
api_id = '4a19d491-d012-43c8-80a1-49a7e86ab814'
receiver_name = 'myrcv'
res = Net::HTTP.post_form(URI.parse('http://hq.alert-grid.com/save-signal/'),
{'api_id'=>URI.escape(api_id),
'receiver_name'=>URI.escape(receiver_name)})

puts res.body
    
#Apart from company_key and receiver_name (which must always be passed) you can include any number of parameters in the Signal. Just remember to url-encode them with URI.escape

res = Net::HTTP.post_form(URI.parse('http://hq.alert-grid.com/save-signal/'),
{'api_id'=>URI.escape(api_id),
  'receiver_name'=>URI.escape(receiver_name)})

puts res.body