Code Samples

In this section you will find some Code sample that will help you to understand API functionality. If you need an specific code sample you can use the “ICITY DEVELOPER API” in “DOCUMENTATION” area where different languages ( Curl, Node, Python, PHP, Ruby, Objective or Java) of all type calls are exposed.

GET - List all Cities

PHP Code:

$ch = curl_init();
$url = 'http://icity-gw.icityproject.com:8080/developer/api/cities';
$queryParams = '?' . urlencode('apikey') . '=' . urlencode('Token App');
curl_setopt($ch, CURLOPT_URL, $url . $queryParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

Response:

[
     {   "cityID":"3",
         "name":"London", 
         "longitude":"-0.119800", 
         "latitude":"51.511189", 
     }
  
     {   "cityID":"7", 
          "name":"Barcelona", 
          "longitude":"2.170030",
          "latitude":"41.387058",
     } 
 
     {   "cityID":"8", 
         "name":"Genova", 
         "longitude":"8.949722",
         "latitude":"41.416672",
     }  
  
     {   
         "cityID":"9", 
         "name":"Bologna", 
         "longitude":"11.351389",  
         "latitude":"44.507500", 
      }
]

GET- Describe Devices

Java Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class ApiExplorer {   
    public static void main(String[] args) throws IOException {
         StringBuilder urlBuilder = new StringBuilder("http://icity-gw.icityproject.com:8080/developer/api/devices/{id}".replace("{id}", URLEncoder.encode("759", "UTF-8"))); 
         urlBuilder.append("?"); 
         urlBuilder.append(URLEncoder.encode("apikey","UTF-8") + "=" + URLEncoder.encode("Token App", "UTF-8"));
         URL url = new URL(urlBuilder.toString());  
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET"); 
         System.out.println("Response code: " + conn.getResponseCode());
         BufferedReader rd; 
         if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {           
             rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));      
         } else {   
             rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
         }        
         StringBuilder sb = new StringBuilder(); 
         String line;
         while ((line = rd.readLine()) != null) { 
             sb.append(line);        
         }       
         rd.close(); 
         conn.disconnect();    
         System.out.println(sb.toString()); 
     } 
}

Python Code:

from urllib2 import Request, urlopen
from urllib import urlencode, quote_plus
url = 'http://icity-gw.icityproject.com:8080/developer/api/devices/{id}'.replace('{id}',quote_plus('759'))
queryParams = '?' + urlencode({ quote_plus('apikey') : 'Token App'  })
request = Request(url + queryParams)
request.get_method = lambda: 'GET'
response_body = urlopen(request).read()
print response_body

Response:

[ 
 {
  "deviceID": "759",
   "name": "SNSSEN3",
   "cityID": "8",
   "infrastructureID": "15",
   "manufacturerID": "13",
   "longitude": "8.946590",
   "latitude": "44.398327",
   "properties": ["urn:parking"]
 }
]

GET- List Observations by samples

Objective C Code:

#import <foundation>
#import <corefoundation>

    @implementation NSString (URLEncoding)
        //function used to encode query and matrix parameters
        -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
            return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                (CFStringRef)self,
                NULL,
                (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                CFStringConvertNSStringEncodingToEncoding(encoding));
        }
    @end

    int main (int argc, const char * argv[])
    {
        @autoreleasepool {
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            NSString *endpoint = [NSString stringWithFormat:@"http://icity-gw.icityproject.com:8080/developer/api/observations/last"];
            NSString *queryParams = [NSString stringWithFormat:@"?%@&%@&%@&%@",
                [NSString stringWithFormat:@"%@=%@", [@"id" urlEncodeUsingEncoding:NSUTF8StringEncoding], [@"759" urlEncodeUsingEncoding:NSUTF8StringEncoding]],
                [NSString stringWithFormat:@"%@=%@", [@"property" urlEncodeUsingEncoding:NSUTF8StringEncoding], [@"urn:parking" urlEncodeUsingEncoding:NSUTF8StringEncoding]],
                [NSString stringWithFormat:@"%@=%@", [@"n" urlEncodeUsingEncoding:NSUTF8StringEncoding], [@"1" urlEncodeUsingEncoding:NSUTF8StringEncoding]],
                [NSString stringWithFormat:@"%@=%@", [@"apikey" urlEncodeUsingEncoding:NSUTF8StringEncoding], [@"l7xx5d907cab477a494e8b9b2ef45f8d6b52" urlEncodeUsingEncoding:NSUTF8StringEncoding]]];
            [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",endpoint,queryParams]]];
            [request setHTTPMethod:@"GET"];

            NSHTTPURLResponse *urlResponse = nil;
            NSError *error = [[NSError alloc] init];
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
            NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
            NSLog(@"Response Code: %d", [urlResponse statusCode]);
            if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
                NSLog(@"Response: %@", result);
            }
            return 0;
        }
    }


Response:

[
  {
    "time": "2013-10-16T12:14:27",
    "value": "free",
    "units": "none"
  }
]