1 package com.leonarduk.clearcheckbook; 2 3 import java.io.IOException; 4 5 import org.apache.log4j.Logger; 6 7 import com.leonarduk.clearcheckbook.calls.AbstractCall; 8 import com.leonarduk.clearcheckbook.calls.AccountCall; 9 import com.leonarduk.clearcheckbook.calls.CategoryCall; 10 import com.leonarduk.clearcheckbook.calls.LimitCall; 11 import com.leonarduk.clearcheckbook.calls.PremiumCall; 12 import com.leonarduk.clearcheckbook.calls.ReminderCall; 13 import com.leonarduk.clearcheckbook.calls.ReportCall; 14 import com.leonarduk.clearcheckbook.calls.TransactionCall; 15 import com.leonarduk.clearcheckbook.calls.UserCall; 16 import com.leonarduk.clearcheckbook.dto.ParsedNameValuePair; 17 import com.leonarduk.utils.HtmlUnitUtils; 18 19 public class ClearCheckBookConnection { 20 21 final private String password; 22 final private String userName; 23 public final String baseurl = "https://www.clearcheckbook.com/api/"; 24 25 private AccountCall accountCall; 26 27 private CategoryCall categoryCall; 28 private LimitCall limitCall; 29 private PremiumCall premiumCall; 30 private ReminderCall reminderCall; 31 private ReportCall reportCall; 32 private TransactionCall transactionCall; 33 private UserCall userCall; 34 35 private static final Logger _logger = Logger.getLogger(AbstractCall.class); 36 37 public ClearCheckBookConnection(String userName, String password) { 38 this.userName = userName; 39 this.password = password; 40 41 this.accountCall = new AccountCall(this); 42 this.categoryCall = new CategoryCall(this); 43 this.limitCall = new LimitCall(this); 44 this.premiumCall = new PremiumCall(this); 45 this.reminderCall = new ReminderCall(this); 46 this.reportCall = new ReportCall(this); 47 this.transactionCall = new TransactionCall(this); 48 this.userCall = new UserCall(this); 49 50 } 51 52 public AccountCall account() { 53 return accountCall; 54 } 55 56 public CategoryCall category() { 57 return categoryCall; 58 } 59 60 public LimitCall limit() { 61 return limitCall; 62 } 63 64 public PremiumCall premium() { 65 return premiumCall; 66 } 67 68 public ReminderCall reminder() { 69 return reminderCall; 70 } 71 72 public ReportCall report() { 73 return reportCall; 74 } 75 76 public TransactionCall transaction() { 77 return transactionCall; 78 } 79 80 public UserCall user() { 81 return userCall; 82 } 83 84 public String getUserName() { 85 return userName; 86 } 87 88 public String getBaseurl() { 89 return baseurl; 90 } 91 92 /*** 93 * 94 * @param url 95 * @param parameters 96 * @return 97 * @throws IOException 98 */ 99 public String getPage(String url, ParsedNameValuePair... parameters) 100 throws IOException { 101 _logger.debug("URL:" + getFullUrl(url) + parameters); 102 String page = HtmlUnitUtils.getPageText(getFullUrl(url), "GET", 103 userName, password, parameters); 104 return page; 105 } 106 107 private String addGetParameters(String url, 108 ParsedNameValuePair... parameters) { 109 url += "?"; 110 if (null != parameters[0]) { 111 for (int i = 0; i < parameters.length; i++) { 112 url += parameters[i].getName() + "=" + parameters[i].getValue() 113 + "&"; 114 } 115 } 116 return url; 117 } 118 119 /*** 120 * 121 * @param url 122 * @param parameters 123 * @return 124 * @throws IOException 125 */ 126 public String postPage(String url, ParsedNameValuePair... parameters) 127 throws IOException { 128 String fullPath = getFullUrl(url); 129 _logger.debug("URL:" + fullPath + " " + parameters); 130 String page = HtmlUnitUtils.getPageText(fullPath, "POST", userName, 131 password, parameters); 132 return page; 133 } 134 135 /*** 136 * 137 * @param url 138 * @return 139 */ 140 private String getFullUrl(String url) { 141 String fullPath = "https://" + userName + ":" + password 142 + "@www.clearcheckbook.com/api/" + url; 143 return fullPath; 144 } 145 146 /*** 147 * 148 * @param url 149 * @param parameters 150 * @return 151 * @throws IOException 152 */ 153 public String putPage(String url, ParsedNameValuePair... parameters) 154 throws IOException { 155 url = addGetParameters(url, parameters); 156 String fullUrl = getFullUrl(url); 157 158 _logger.debug("URL:" + fullUrl); 159 String page = HtmlUnitUtils.getPageText(fullUrl, "PUT", userName, 160 password, parameters); 161 return page; 162 } 163 164 /*** 165 * 166 * @param url 167 * @param parameters 168 * @return 169 * @throws IOException 170 */ 171 public String deletePage(String url, ParsedNameValuePair... parameters) 172 throws IOException { 173 _logger.debug("URL:" + this.baseurl + url); 174 String page = HtmlUnitUtils.getPageText(this.baseurl + url, "DELETE", 175 userName, password, parameters); 176 return page; 177 } 178 179 }