Mineful has a REST API which allows you to query and add data to your surveys and data tables.
Java
These examples use Apache HttpComponents. They use the HttpClient and HttpCore packages.
Here is an example GET request:
1 package com.numbersinsight.api.rest.commons40; 2 3 import java.net.URI; 4 import java.util.ArrayList; 5 import java.util.List; 6 import org.apache.http.NameValuePair; // http core jar 7 import org.apache.http.client.HttpClient; // http client jar 8 import org.apache.http.client.ResponseHandler; // http client jar 9 import org.apache.http.client.methods.HttpGet; // http client jar 10 import org.apache.http.client.methods.HttpPost; // http client jar 11 import org.apache.http.client.utils.URIUtils; // http client jar 12 import org.apache.http.client.utils.URLEncodedUtils; // http client jar 13 import org.apache.http.impl.client.BasicResponseHandler; // http client jar 14 import org.apache.http.impl.client.DefaultHttpClient; // http client jar 15 import org.apache.http.message.BasicNameValuePair; // http core jar 16 17 public class URIRequester { 18 19 public void runGetRequest() { 20 try { 21 // assemble parameters into name/value pairs 22 List< NameValuePair > qparams = new ArrayList< NameValuePair >(); 23 qparams.add( new BasicNameValuePair( "cmd", "getSurveyList" ) ); 24 qparams.add( new BasicNameValuePair( "dk", "ArkhangaiBayankhongorÖvörkhangai" ) ); 25 26 URI uri = URIUtils.createURI( 27 "http", // String scheme, 28 "mineful", // String host, 29 80, // int port, 30 "/api", // String path, 31 URLEncodedUtils.format( qparams, "UTF-8" ), // String query, 32 null // String fragment 33 ); 34 35 HttpGet httpget = new HttpGet( uri ); 36 System.out.println( "here is httpget.getURI(): " + httpget.getURI() ); 37 38 // Create a response handler 39 HttpClient httpclient = new DefaultHttpClient(); 40 ResponseHandler< String > responseHandler = new BasicResponseHandler(); 41 // execute the request 42 String responseBody = httpclient.execute( httpget, responseHandler ); 43 System.out.println( responseBody ); 44 45 httpclient.getConnectionManager().shutdown(); 46 47 } catch ( Exception e ) { 48 e.printStackTrace( System.out ); 49 } 50 } // end method runGetRequest 51 52 } // end class com.numbersinsight.api.rest.commons40.URIRequester 53
Here is an example POST request in Java. Notice that the request builds an XML
string. It does not send an XML file.
1 package com.numbersinsight.api.rest.commons40; 2 3 import java.net.URI; 4 import java.util.ArrayList; 5 import java.util.List; 6 import org.apache.http.NameValuePair; // http core jar 7 import org.apache.http.client.HttpClient; // http client jar 8 import org.apache.http.client.ResponseHandler; // http client jar 9 import org.apache.http.client.methods.HttpGet; // http client jar 10 import org.apache.http.client.methods.HttpPost; // http client jar 11 import org.apache.http.client.utils.URIUtils; // http client jar 12 import org.apache.http.client.utils.URLEncodedUtils; // http client jar 13 import org.apache.http.impl.client.BasicResponseHandler; // http client jar 14 import org.apache.http.impl.client.DefaultHttpClient; // http client jar 15 import org.apache.http.message.BasicNameValuePair; // http core jar 16 17 public class URIRequester { 18 19 public void runPostRequest() { 20 try { 21 // assemble parameters into name/value pairs 22 List< NameValuePair > params = new ArrayList< NameValuePair >(); 23 params.add( new BasicNameValuePair( "cmd", "addDataRecord" ) ); 24 params.add( new BasicNameValuePair( "dk", "GurvanbulagSaikhandulaanAdaatsag" ) ); 25 26 StringBuffer xmlBuff = new StringBuffer(); 27 // assemble the XML string as StringBuffer 28 xmlBuff.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ); 29 xmlBuff.append( "<apirequest version=\"0.1\">" ); 30 xmlBuff.append( "<record table_id=\"7\">" ); 31 xmlBuff.append( "<field id=\"12\">Tusk</field>" ); 32 xmlBuff.append( "<field id=\"13\">100</field>" ); 33 xmlBuff.append( "</record></apirequest>" ); 34 // add the XML StringBuffer to params 35 params.add( new BasicNameValuePair( "xmlString", xmlBuff.toString() ) ); 36 37 URI uri = URIUtils.createURI( 38 "http", // String scheme, 39 "mineful.com", // String host, 40 80, // int port, 41 "/api", // String path, 42 URLEncodedUtils.format( params, "UTF-8" ), // String query, 43 null // String fragment 44 ); 45 46 HttpPost httpPost = new HttpPost( uri ); 47 System.out.println( "here is httpget.getURI(): " + httpPost.getURI() ); 48 49 // Create a response handler 50 HttpClient httpclient = new DefaultHttpClient(); 51 ResponseHandler< String > responseHandler = new BasicResponseHandler(); 52 String responseBody = httpclient.execute( httpPost, responseHandler ); 53 System.out.println( responseBody ); 54 httpclient.getConnectionManager().shutdown(); 55 56 } catch ( Exception e ) { 57 e.printStackTrace( System.out ); 58 } // end try/catch 59 } // end method runPostRequest 60 61 } // end class com.numbersinsight.api.rest.commons40.URIRequester
If the XML if not properly formed, you will get a message like the following:
XML Error: Element type "xml" must be followed by either attribute specifications, ">" or "/>".
Ruby
Here is an example GET request in Ruby:
1 require 'net/http' 2 3 DEVELOPER_KEY = 'DelgerkhangaiZamyn-ÜüdTsogtAltai' 4 MINEFUL_API_URL = 'http://www.mineful.com/api' 5 COMMAND = 'getSurveyList' 6 7 url = "#{MINEFUL_API_URL}?dk=#{DEVELOPER_KEY}&cmd=#{COMMAND}" 8 begin 9 xml_result_set = Net::HTTP.get_response(URI.parse(url)) 10 rescue Exception => e 11 puts 'Connection error: ' + e.message 12 end 13 print xml_result_set.body 14 print xml_result_set.to_s + "\n"
Here is a POST request in Ruby:
1 require 'net/http' 2 require 'cgi' 3 4 http = Net::HTTP.new( 'localhost', 8080 ) 5 6 path = '/api' 7 path << '?cmd=addDataRecord' # add the command 8 path << '&dk=Bayan-UulYesönbulagKhökhmoritLun' # add the developer key 9 path << '&xmlString=' # start the xml String 10 11 XMLSTRING = '<?xml version="1.0" encoding="UTF-8"?>' 12 XMLSTRING << '<apirequest version="0.1">' 13 XMLSTRING << '<record table_id="7">' 14 XMLSTRING << '<field id="12">Tusk</field>' 15 XMLSTRING << '<field id="13">100</field>' 16 XMLSTRING << '</record>' 17 XMLSTRING << '</apirequest>' 18 url_encoded_string = CGI::escape( XMLSTRING ) # make XML string ready for prime time 19 20 path << url_encoded_string # add XMLSTRING to the URL 21 puts "path: " + path 22 23 data = ' '; 24 headers = {} 25 26 # execute the request 27 resp = http.post( path, data, headers ) 28 29 puts 'Code = ' + resp.code 30 puts 'Message = ' + resp.message 31 resp.each { |key, val| puts "\t" + key + ' = ' + val } 32 puts resp 33 puts data # this will contain the xml response string
If the XML if not properly formed, you will get a message like the following:
XML Error: Element type "xml" must be followed by either attribute specifications, ">" or "/>".
Python
Here is an example GET request in Python:
1 import http.client 2 3 conn = http.client.HTTPConnection( "mineful.com" ) 4 5 # base URL 6 urlString = 'http://www.mineful.com/api' 7 # add the command 8 urlString += '?cmd=getSurveyList' 9 # add the developer key 10 urlString += '&dk=MongonmortUgtaalBayanjargalan' 11 12 conn.request( "GET", urlString ) 13 14 response = conn.getresponse() 15 print( response.status, response.reason ) 16 data = response.read() 17 print( data ) 18 conn.close()
Here is a POST request in Python:
1 import urllib.parse 2 import http.client 3 4 # process the XML String 5 xmlStringRaw = '<?xml version="1.0" encoding="UTF-8"?>' 6 xmlStringRaw += '<apirequest version="0.1">' 7 xmlStringRaw += ' <record table_id="7">' 8 xmlStringRaw += ' <field id="12">Tusk</field>' 9 xmlStringRaw += ' <field id="13">100</field>' 10 xmlStringRaw += ' </record>' 11 xmlStringRaw += '</apirequest>' 12 13 urlString = urllib.parse.quote_plus( xmlStringRaw ) 14 15 path = "/api" 16 # add the command 17 path += "?cmd=addDataRecord" 18 # add the dev key 19 path += "&dk=05e2aaa5c1e011df8d4461a747243d4a" 20 # add the XML String 21 path += "&xmlString=" 22 path += urlString 23 24 conn = http.client.HTTPConnection( "localhost:8080" ) 25 26 conn.request( "POST", path ) 27 response = conn.getresponse() 28 print( response.status, response.reason ) 29 data = response.read() 30 print( data ) 31 conn.close()
If the XML if not properly formed, you will get a message like the following:
XML Error: Element type "xml" must be followed by either attribute specifications, ">" or "/>".
