package com.panaceamobile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.json.JSONObject;
public class Api {
private String apiUrl = "http://api.panaceamobile.com/json";
private String username;
private String password;
private String encodingScheme = "UTF-8";
private boolean debug = false;
/**
* Instantiates a new instance of the API
*
* @param username Your Panacea Mobile username
* @param password Your Panacea Mobile password
*/
public Api(String username, String password) {
this.username = username;
this.password = password;
}
/**
* Set's the debugging state
*
* (if on it will print lines to the console, default: off)
*
* @param state
*/
public void setDebugging(boolean state) {
this.debug = state;
}
private static java.util.HashMap<String, String> createParams(String action) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("action", action);
return map;
}
private static void addParameter(java.util.Map<String, String> map, String key, String value) {
if(value != null) {
map.put(key, value);
}
}
private static void addParameter(java.util.Map<String, String> map, String key, Integer value) {
if(value != null) {
map.put(key, String.valueOf(value));
}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private JSONObject call(java.util.Map<String, String> map) {
String workingUrl = apiUrl;
String key = null;
String val = null;
try {
workingUrl += "?username="+URLEncoder.encode(username, encodingScheme);
workingUrl += "&password="+URLEncoder.encode(password, encodingScheme);
Set<String> keys = map.keySet();
Iterator<String> i = keys.iterator();
while(i.hasNext()) {
key = (String) i.next();
val = map.get(key);
if(val != null) {
workingUrl += "&"+URLEncoder.encode(key, encodingScheme) + "=" +URLEncoder.encode(val, encodingScheme);
}
}
debug("URL to be called "+workingUrl);
URL url = new URL(workingUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
String result = convertStreamToString(urlConnection.getInputStream());
if(result != null) {
if(result.length() > 0) {
/* Something */
JSONObject json = new JSONObject(result);
return json;
}
}
} catch(ConnectException e) {
debug("Connection error "+ e.getMessage());
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
private boolean validateResult(JSONObject json) {
if(json != null) {
try {
int status = json.getInt("status");
if(status >= 0) {
/* OK! */
return true;
}
debug("result was "+json.toString());
} catch(Exception e) {
e.printStackTrace();
}
}
return false;
}
private void debug(String data) {
if(this.debug) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "[" + sdf.format(cal.getTime()) + "] "+data;
System.out.println(str);
}
}
/**
* Send's a message via Panacea Mobile
*
* @param to The destination of the message
* @param text The text of the message
* @param from The source of the message (where the user will see it came from)
* @param report_mask Delivery report mask (See API documentation)
* @param report_url Delivery report URL (See API documentation)
* @param charset Character set encoding (default: UTF-8)
* @param data_coding Data coding (See API documentation)
* @return
*/
public String message_send(String to, String text, String from, Integer report_mask, String report_url, String charset, Integer data_coding) {
HashMap<String, String> map = new HashMap<String, String>();
addParameter(map, "action", "message_send");
addParameter(map, "to", to);
addParameter(map, "text", text);
addParameter(map, "from", from);
addParameter(map, "report_mask", report_mask);
addParameter(map, "report_url", report_url);
addParameter(map, "charset", charset);
addParameter(map, "data_coding", data_coding);
JSONObject result = call(map);
if(validateResult(result)) {
/* Return the message ID */
try {
return result.getString("details");
} catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* Check your balance
*
* @return The balance
*/
public Double user_get_balance() {
HashMap<String, String> map = createParams("user_get_balance");
JSONObject result = call(map);
if(validateResult(result)) {
try {
return Double.valueOf(result.getDouble("details"));
} catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* Check the status of a message
* @param message_id The message ID you wish to check
* @return A JSONObject containing keys status, cost and parts see ApiExample.java
*/
public JSONObject message_status(String message_id) {
HashMap<String, String> map = createParams("message_status");
addParameter(map, "message_id", message_id);
JSONObject result = call(map);
if (validateResult(result)) {
try {
return result.getJSONObject("details");
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}