Ejemplos de implementación

TABLA DE CONTENIDOS

  1. Obtener Token
    1. Consumo mediante curl en Bash
    2. Consumo mediante PHP
    3. Consumo mediante Java usando java.net
    4. Consumo mediante Python
    5. Consumo en NodeJS usando node-rest-client
  2. Renovar un Token
    1. Consumo mediante curl en Bash
    2. Consumo mediante PHP
    3. Consumo mediante Java usando java.net
    4. Consumo mediante Python
    5. Consumo en NodeJS usando node-rest-client
  3. Realizar Consulta
    1. Consumo mediante curl en Bash
    2. Consumo mediante PHP
    3. Consumo mediante Java usando java.net
    4. Consumo mediante Python
    5. Consumo en NodeJS usando node-rest-client
  4. Usando Postman

Algunos ejemplos para el consumo de la API REST de OpenDataCollector.

Es importante recordar que nuestro servidor solo soporta HTTPS

Peticiones sobre HTTP seran ignoradas, por favor verifica que estes conectando con el host https://opendatacollector.com.

Obtener Token

Consumo mediante curl en Bash

curl -s https://opendatacollector.com/api/token/ -d 'grant_type=token&client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>' -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30"

Consumo mediante PHP

try {
  $clientId = "ejemplo-de-clientid";
  $secretKey = "ejemplo-de-secretkey";
  $url = "https://opendatacollector.com/api/token";
  $userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30";
  $body = "grant_type=client_credentials&client_id=".$clientId."&client_secret=".$secretKey;

  $curl	= curl_init();
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

  $rest	= curl_exec($curl);
  print_r($rest);
  curl_close($curl);
} catch (Exception $e) {
  print_r($e);
}

Consumo mediante Java usando java.net

try {
  String clientId = "ejemplo-de-clientid";
  String secretKey = "ejemplo-de-secretkey";
  String input = String.format("grant_type=client_credentials&client_id=%s&client_secret=%s", clientId, secretKey);

  URL url = new URL("https://opendatacollector.com/api/token");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoOutput(true);
  conn.setRequestMethod("POST");
  conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30");

  OutputStream os = conn.getOutputStream();
  os.write(input.getBytes());
  os.flush();

  if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new RuntimeException("Failed : HTTP error code : "
      + conn.getResponseCode());
  }

  BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));

  String output = null;

  while ((output = br.readLine()) != null) {
    System.out.println(output);
  }

  conn.disconnect();

} catch (Exception e) {
  e.printStackTrace();
}

Consumo mediante Python

#!/usr/bin/env python3

import requests

clientId = "ejemplo-de-clientid"
secretKey = "ejemplo-de-secretkey"
url = "https://opendatacollector.com/api/token"
headers = {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "content-type":"application/x-www-form-urlencoded"}
payload = {"grant_type":"client_credentials", "client_id":clientId, "client_secret":secretKey}
response = requests.post(url, headers=headers, data=payload)

if (response.status_code != 200):
	print("Failed! HTTP error code: " + str(response.status_code))
else:
	print(response.text)

Consumo en NodeJS usando node-rest-client

var Client = require('node-rest-client').Client;

var clientId = "ejemplo-de-clientid";
var secretKey = "ejemplo-de-secretkey";
var client = new Client();

var args = {
  data: {"grant_type":"client_credentials", "client_id":clientId, "client_secret":secretKey},
  headers: {"Accept":"application/json", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "Content-Type":"application/x-www-form-urlencoded"}
};

client.post("https://opendatacollector.com/api/token", args, function (data, response) {
  if (response.statusCode != 200) {
    console.log("Failed! HTTP Error code: " + response.statusCode);
  } else {
    console.log(data);
  }
});

Renovar un Token

Es importante recordar que nuestro servidor solo soporta HTTPS

Peticiones sobre HTTP seran ignoradas, por favor verifica que estes conectando con el host https://opendatacollector.com.

Consumo mediante curl en Bash

curl -X POST -s https://opendatacollector.com/api/check/ -d 'access_token=<TOKEN>' -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30"

Consumo mediante PHP

try {
  $token = "ejemplo-de-token";
  $url = "https://opendatacollector.com/api/check/";
  $userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30";
  $body = "access_token=".$token;

  $curl	= curl_init();
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

  $rest	= curl_exec($curl);
  print_r($rest);
  curl_close($curl);
} catch (Exception $e) {
  print_r($e);
}

Consumo mediante Java usando java.net

try {
  String token = "ejemplo-de-token";
  String input = "access_token=" + token;

  URL url = new URL("https://opendatacollector.com/api/check/");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoOutput(true);
  conn.setRequestMethod("POST");
  conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30");

  OutputStream os = conn.getOutputStream();
  os.write(input.getBytes());
  os.flush();

  if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new RuntimeException("Failed : HTTP error code : "
      + conn.getResponseCode());
  }

  BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));

  String output = null;

  while ((output = br.readLine()) != null) {
    System.out.println(output);
  }

  conn.disconnect();

} catch (Exception e) {
  e.printStackTrace();
}

Consumo mediante Python

#!/usr/bin/env python3

import requests

token = "ejemplo-de-token"
url = "https://opendatacollector.com/api/check"
headers = {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "content-type":"application/x-www-form-urlencoded"}
payload = {"access_token": token}
response = requests.post(url, headers=headers, data=payload)

if (response.status_code != 200):
	print("Failed! HTTP error code: " + str(response.status_code))
else:
	print(response.text)

Consumo en NodeJS usando node-rest-client

var Client = require('node-rest-client').Client;

var token = "ejemplo-de-token";
var client = new Client();

var args = {
  data: {"access_token": token},
  headers: {"Accept":"application/json", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "Content-Type":"application/x-www-form-urlencoded"}
};

client.post("https://opendatacollector.com/api/check", args, function (data, response) {
  if (response.statusCode != 200) {
    console.log("Failed! HTTP Error code: " + response.statusCode);
  } else {
    console.log(data);
  }
});

Realizar Consulta

En todos los ejemplos a continuacion, debe reemplazar CONTROLLER por el ID del Data Collector usado, INPUT por el valor a buscar (patente, rut sin digito verificador, etc.) y TOKEN con el valor obtenido anteriormente.

Es importante recordar que nuestro servidor solo soporta HTTPS

Peticiones sobre HTTP seran ignoradas, por favor verifica que estes conectando con el host https://opendatacollector.com.

Peticiones deben realizarse mediante POST. Peticiones GET son ignoradas.

Consumo mediante curl en Bash

curl -X POST -s https://opendatacollector.com/api/exec/<CONTROLLER>/<INPUT>/ -d 'access_token=<TOKEN>' -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30"

Consumo mediante PHP

try {
  $token = "ejemplo-de-token";
  $controller = "ejemplo-de-colector";
  $input = "ejemplo-de-input";
  $url = "https://opendatacollector.com/api/exec/".$controller."/".$input."/";
  $userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30";
  $body = "access_token=".$token;

  $curl	= curl_init();
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

  $rest	= curl_exec($curl);
  print_r($rest);
  curl_close($curl);
} catch (Exception $e) {
  print_r($e);
}

Consumo mediante Java usando java.net

try {
  String controller = "ejemplo-de-controller";
  String input = "ejemplo-de-input";
  String token = "ejemplo-de-token";
  String data = "access_token=" + token;

  URL url = new URL(String.format("https://opendatacollector.com/api/exec/%s/%s/", controller, input));
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoOutput(true);
  conn.setRequestMethod("POST");
  conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30");

  OutputStream os = conn.getOutputStream();
  os.write(data.getBytes());
  os.flush();

  if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new RuntimeException("Failed : HTTP error code : "
      + conn.getResponseCode());
  }

  BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));

  String output = null;

  while ((output = br.readLine()) != null) {
    System.out.println(output);
  }

  conn.disconnect();

} catch (Exception e) {
  e.printStackTrace();
}

Consumo mediante Python

#!/usr/bin/env python3

import requests

controller = "ejemplo-de-controller";
inputValue = "ejemplo-de-input";
token = "ejemplo-de-token"
url = "https://opendatacollector.com/api/exec/" + controller + "/" + inputValue
headers = {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "content-type":"application/x-www-form-urlencoded"}
payload = {"access_token": token}
response = requests.post(url, headers=headers, data=payload)

if (response.status_code != 200):
	print("Failed! HTTP error code: " + str(response.status_code))
else:
	print(response.text)

Consumo en NodeJS usando node-rest-client

var Client = require('node-rest-client').Client;

var token = "ejemplo-de-token";
var controller = "ejemplo-de-controller";
var inputValue = "ejemplo-de-input";
var url = "https://opendatacollector.com/api/exec/" + controller + "/" + inputValue;
var client = new Client();

var args = {
  data: {"access_token": token},
  headers: {"Accept":"application/json", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30", "Content-Type":"application/x-www-form-urlencoded"}
};

client.post(url, args, function (data, response) {
  if (response.statusCode != 200) {
    console.log("Failed! HTTP Error code: " + response.statusCode);
  } else {
    console.log(data);
  }
});

Usando Postman

Usted puede utilizar la herramienta Postman para ejecutar los ejemplos y obtener codigo funcional.

Por favor visite el siguiente enlace:


OpenDataCollector © Xpertians - Desarrolladores - Soporte