Java API Example

This class provides an example of how one would call the Vyew API functions in Java. Only TWO (“create”, “import”) API wrapper functions are provided as an example.

Internally, these two functions use the “callAPI” function to properly assemble a URL that the Vyew API can understand. When a response from the API call is received, the result is parsed in to an ArrayList via the “processResult” function.

package com.vyew;
 
import java.math.BigInteger;
import java.net.*;
import java.io.*;
import java.security.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.net.URLEncoder;
 
/**
 * This class provides an example of how one would call the Vyew API functions in
 * Java.  Only TWO ("create", "import") API wrapper functions are provided as an example.
 * 
 * Internally, these two functions use the "callAPI" function to properly assemble a
 * URL that the Vyew API can understand.  When a response from the API call is received,
 * the result is parsed in to an ArrayList via the "processResult" function.
 * 
 * For more information regarding the Vyew API, see: 
 * http://dev.vyew.com/public-api/files/VyewAPI-php.html
 * 
 * @author Michael Bautista
 */
public class VyewAPI {
 
	/** your assigned API key */
	private String key = "enter_api_key";
	
	/** your assigned API secret */
	private String secret = "enter_api_secret";
	
	/** the url of the Vyew API */
	private String api_url = "https://vyew.com/api/api.php"; 
	
	
	public static void main (String [] args){
		/** 
		 * USAGE EXAMPLE: 
		 * 
		 * 1. create a new book 
		 * 2. if success, import a file in to the new book
		 */
		VyewAPI vyewAPI = new VyewAPI();
		ArrayList<Object> result;
		
		//attempt to create a new book
		result = vyewAPI.create("My New API Book");
		
		//check the result
		if(result.size() > 0 && result.get(0).toString().equals("1")){
			System.out.println("SUCCESS creating book: " + result);
			
			//success creating book, attempt to import a file to it
			String bookID = result.get(2).toString();			
			result = vyewAPI.importFile(bookID, "http://xnet.vyew.com/docs/_media/public/vyewlogo-600px.png");
			
			if(result.size() > 0 && result.get(0).toString().equals("1")){
				System.out.println("SUCCESS importing in to book: " + result);	
			}else{
				System.out.println("ERROR importing in to book: " + result);
			}
		}else{
			System.out.println("ERROR creating book: " + result);
		}
		
	}
	
	//////////////////////////////////////////////////////////////////////
	//
	// API Function Wrappers
	//
	// see http://dev.vyew.com/public-api/files/VyewAPI-php.html for more details
	//
	/**
	 * Create a new book with a given name
	 * 
	 * @param name - name of the book to create
	 * @return ArrayList<Object> = [<result code>, 
	 * 								<message>, 
	 * 								<ID of the new book>, 
	 * 								<url of the new book>]
	 */
	public ArrayList<Object> create(String name){
		HashMap<String,String> args = new HashMap<String,String>();
		args.put("name", name);
		
		ArrayList<Object> result = callAPI("create", args);
		return result;
	}
	
	/**
	 * Create a new book with a default name ("New Vyewbook")
	 * 
	 * @return ArrayList<Object> = [<result code>, 
	 * 								<message>, 
	 * 								<ID of the new book>, 
	 * 								<url of the new book>]
	 */
	public ArrayList<Object> create(){
		ArrayList<Object> result = callAPI("create", new HashMap<String,String>());
		return result;
	}
	
	/**
	 * Import a file in to an existing book
	 * 
	 * @param vyewbook - Book ID or meeting ID of book to insert to
	 * @param source - URL of content to import
	 * @return ArrayList<Object> = [<result code>, 
	 * 								<message>, 
	 * 								<url of the book just imported to>]
	 */
	public ArrayList<Object> importFile(String vyewbook, String source){
		HashMap<String,String> args = new HashMap<String,String>();
		args.put("vyewbook", vyewbook);
		args.put("source", source);
		
		ArrayList<Object> result = callAPI("import", args);
		return result;
	}
	
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	
	/**
	 * Attempt to call an API function on the Vyew server
	 * 
	 * @param cmd - the API command to call 
	 * 				(see http://dev.vyew.com/public-api/files/VyewAPI-php.html)
	 * @param args - arguments to pass in for the API call
	 * 				(see http://dev.vyew.com/public-api/files/VyewAPI-php.html)
	 * @return result ArrayList
	 * 				(see http://dev.vyew.com/public-api/files/VyewAPI-php.html)
	 */
	private ArrayList<Object> callAPI(String cmd, HashMap<String,String> args){
		
		ArrayList<Object> result = new ArrayList<Object>();
		
		try
		{
			//construct the query string
			String query = "cmd=" + cmd;
			
			//iterate through the args and append to the query
			Set<String> keys = args.keySet();
			Iterator<String> itr = keys.iterator();
			while(itr.hasNext()){
				String key = itr.next();	
				query += "&" + key + "=" + URLEncoder.encode(args.get(key), "UTF-8");
			}
			
			//all API calls require a time
			long time = System.currentTimeMillis()/1000;
			query += "&key=" + key + "&time=" + time;
			
			//construct the md5 parameter
			String hash = md5(query + secret);
			
			//construct the final url
			String url = api_url + "?" + query + "&md5=" + hash;
			System.out.println(url);
			
			//attempt the api call
			URL api_url = new URL(url);
			URLConnection conn = api_url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader (conn.getInputStream()) );
			
			//read the response from the call;
			//if a response was found, attempt to parse it in to an ArrayList
			String inputLine;
			String resultString = "";
			while ((inputLine = in.readLine()) != null){
				if(inputLine.length() > 0 ){
					resultString = inputLine;
					break;
				}
			}
			
			//parse the response
			result = processResult(resultString); 						
	        in.close();
	        
	        return result;
		}
		
		catch( MalformedURLException e)
		{
			System.out.println("MalformedURLException: " + e.toString());
			return result;
		}
		catch (IOException e)
		{
			System.out.println("IOException: " + e.toString());
			return result;
		}
		
	}
	
	/**
	 * Given a response string from a Vyew API call, attempt
	 * to parse it in to an ArrayList
	 * 
	 * @param result
	 * @return ArrayList - parsed result
	 */
	private ArrayList<Object> processResult(String result){
		ArrayList<Object> res = new ArrayList<Object>();
		
		if(result == null) return res;
						
		String[] parts = result.split("\\|");
		for(int i=0; i<parts.length; i++) res.add(parts[i]);
		
		return res;
	}
	
	/**
	 * Return MD5 encrypted string
	 * 
	 * @param in - String to be encrypted
	 * @return encrypted String
	 */
	private String md5(String in){
		String hashtext = "";
		
		try{
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.reset();
			m.update(in.getBytes());
			byte[] digest = m.digest();
			BigInteger bigInt = new BigInteger(1,digest);
			hashtext = bigInt.toString(16);
			while(hashtext.length() < 32 ){
				hashtext = "0"+hashtext;
			}
		}catch( NoSuchAlgorithmException e){
			System.out.println("NoSuchAlgorithmException: " + e.toString());
		}
		
		return hashtext;
	}
	
}
 
api/serverside/java.txt · Last modified: 2010/04/29 11:57 by simadmin     Back to top