<commit_before>package org.openexchangerates.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.openexchangerates.client.RestClient.ErrorHandler;
import org.openexchangerates.client.RestClient.HttpGETCache;
import org.openexchangerates.client.RestClient.Response;
import org.openexchangerates.client.RestClient.ResponseDeserializer;
import org.openexchangerates.client.RestClient.URLBuilder;

/**
 * openexchangerates.org (OER) client for Java.  Deserializes OER JSON messages into native Java types.
 * 
 * Depends on org.touge RestClient (a wrapper for HTTPUrlConnection) and org.json JSON library.
 * 
 * @author kgilmer
 *
 */
public final class OERClient {
	
	/**
	 * Date format used in composing openexchangerate.org URLs 
	 */
	private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * Default URL for OER service.
	 */
	public static final String DEFAULT_OER_URL = "https://raw.github.com/currencybot/open-exchange-rates/master";
	/**
	 * Default HTTP connection timeout.
	 */
	public static final int DEFAULT_CONNECT_TIMEOUT = 6000;
	/**
	 * Default HTTP read timeout.
	 */
	public static final int DEFAULT_READ_TIMEOUT = 10000;
	
	private final RestClient restClient;
	private final URLBuilder baseURL;
	private final URLBuilder currenciesURL;
	private final URLBuilder latestURL;
	
	
	/**
	 * {@link OERClient} constructor with default HTTP client and no caching or debugging.  Throw any errors back to the caller.
	 */
	public OERClient() {
		this(null, DEFAULT_OER_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, null, RestClient.THROW_ALL_ERRORS);
	}
	
	/**
	 * {@link OERClient} constructor with defaults for everything except for cache.
	 * @param cache
	 */
	public OERClient(HttpGETCache cache) {
		this(cache, DEFAULT_OER_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, null, RestClient.THROW_ALL_ERRORS);
	}
	
	/**
	 * {@link OERClient} constructor with all configuration capabilities exposed to the client.
	 * 
	 * @param cache cache impl or null for no caching
	 * @param oerUrl base URL to use to connect to OER service
	 * @param connectTimeout connection timeout
	 * @param readTimeout stream read timeout
	 * @param debugWriter PrintWriter to send request/response messages to.  If null no debug output will be generated.
	 * @param errorHandler Determine how connection and deserialization errors are handled.
	 */
	public OERClient(HttpGETCache cache, String oerUrl, int connectTimeout, int readTimeout, PrintWriter debugWriter, ErrorHandler errorHandler) {
		this.restClient = new RestClient();
		this.baseURL = restClient.buildURL(oerUrl);
		this.currenciesURL = baseURL.copy("currencies.json");
		this.latestURL = baseURL.copy("latest.json");
		this.restClient.setCache(cache);
		
		if (debugWriter != null)
			this.restClient.setDebugWriter(debugWriter);
		
		// By default set timeouts on connect and response.
		restClient.addConnectionInitializer(
				new RestClient.TimeoutConnectionInitializer(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT));
		
		// By default throw all errors (Connection, Parsing) back to caller.
		restClient.setErrorHandler(errorHandler);
	}

	/**
	 * Get the latest currency rates.
	 * 
	 * @return Map of latest currency rates.  Key is currency, value is rate as double.
	 * @throws IOException on I/O error
	 * @throws JSONException on JSON parsing error
	 */
	public Map<String, Double> getLatestRates() throws JSONException, IOException {
		Map<String, Double> rateMap = new HashMap<String, Double>();
		
		JSONObject json = callServer(latestURL.toString());
		JSONObject rates = json.getJSONObject("rates");			

		for (Iterator<String> i = rates.keys(); i.hasNext();) {
			String key = i.next();
			rateMap.put(key, rates.getDouble(key));
		}					
		<commit_msg>Updated the OER client to support API_KEY as recently added by openexchangerates.org.  A new constructor was added to allow clients to specify the API_KEY, or alternatively a private constant can be updated with the developers key.  As of yet untested.<commit_after>package org.openexchangerates.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.openexchangerates.client.RestClient.ErrorHandler;
import org.openexchangerates.client.RestClient.HttpGETCache;
import org.openexchangerates.client.RestClient.Response;
import org.openexchangerates.client.RestClient.ResponseDeserializer;
import org.openexchangerates.client.RestClient.URLBuilder;

/**
 * openexchangerates.org (OER) client for Java.  Deserializes OER JSON messages into native Java types.
 * 
 * Depends on org.touge RestClient (a wrapper for HTTPUrlConnection) and org.json JSON library.
 * 
 * @author kgilmer
 *
 */
public final class OERClient {
	
	/**
	 * Date format used in composing openexchangerate.org URLs 
	 */
	private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * Default URL for OER service.
	 */
	//	public static final String DEFAULT_OER_URL = "https://raw.github.com/currencybot/open-exchange-rates/master";
	
	public static final String DEFAULT_OER_URL = "http://openexchangerates.org";
	
	/**
	 * This API_KEY value is used only if the non-api specifying constructor is used.
	 */
	public static final String DEFAULT_API_KEY = "CHANGE_ME_TO_YOUR_API_KEY";
	
	/**
	 * Default HTTP connection timeout.
	 */
	public static final int DEFAULT_CONNECT_TIMEOUT = 6000;
	/**
	 * Default HTTP read timeout.
	 */
	public static final int DEFAULT_READ_TIMEOUT = 10000;
	
	private final RestClient restClient;
	private final URLBuilder baseURL;
	private final URLBuilder currenciesURL;
	private final URLBuilder latestURL;
	
	/**
	 * {@link OERClient} constructor with default HTTP client and no caching or debugging.  Throw any errors back to the caller.
	 */
	public OERClient() {
		this(null, DEFAULT_OER_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, null, RestClient.THROW_ALL_ERRORS);
	}
	
	/**
	 * {@link OERClient} constructor with defaults for everything except for cache.
	 * @param cache
	 */
	public OERClient(HttpGETCache cache) {
		this(cache, DEFAULT_OER_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, null, RestClient.THROW_ALL_ERRORS);
	}
		
	/**
	 * {@link OERClient} constructor with all configuration capabilities exposed to the client.
	 * 
	 * @param cache cache impl or null for no caching
	 * @param oerUrl base URL to use to connect to OER service
	 * @param connectTimeout connection timeout
	 * @param readTimeout stream read timeout
	 * @param debugWriter PrintWriter to send request/response messages to.  If null no debug output will be generated.
	 * @param errorHandler Determine how connection and deserialization errors are handled.
	 * @param apiKey API_KEY provisioned at openexchangerates.org
	 */
	public OERClient(HttpGETCache cache, String oerUrl, int connectTimeout, int readTimeout, PrintWriter debugWriter, ErrorHandler errorHandler, String apiKey) {
		this.restClient = new RestClient();
		this.baseURL = restClient.buildURL(oerUrl).addParameter("API_KEY", apiKey);
		this.currenciesURL = baseURL.copy("currencies.json");
		this.latestURL = baseURL.copy("latest.json");
		this.restClient.setCache(cache);
		
		if (debugWriter != null)
			this.restClient.setDebugWriter(debugWriter);
		
		// By default set timeouts on connect and response.
		restClient.addConnectionInitializer(
				new RestClient.TimeoutConnectionInitializer(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT));
		
		// By default throw all errors (Connection, Parsing) back to caller.
		restClient.setErrorHandler(errorHandler);
	}
	
	/**
	 * @param cache
	 * @param oerUrl
	 * @param connectTimeout
	 * @param readTimeout
	 * @param debugWriter
	 * @param errorHandler
	 */
	public OERClient(HttpGETCache cache, String oerUrl, int connectTimeout, int readTimeout, PrintWriter debugWriter, ErrorHandler errorHandler) {
		this(cache, oerUrl, connectTimeout, readTimeout, debugWriter, errorHandler, DEFAULT_API_KEY);
	}

	/**
	 * Get the latest currency rates.
	 * 
	 * @return Map of latest currency rates.  Key is currency, value is rate as double.
	 * @throws IOException on I/O error
	 * @throws JSONException on JSON parsing error
	 */
	public Map<String, Double> getLatestRates() throws JSONException, IOException {
		Map<String, Double> rateMap = new HashMap<String, Double>();
		
		JSONObject json = callServer(latestURL.toString());
		JSONObject rates = json.getJSONObject("rates");			

		for (Iterator<String> i = rates.keys(); i.hasNext();) {
			String key = i.next();
			rateMap.put(key, rates.getDouble(key));
		}					
		
{'fraction_non_alphanumeric': 0.08976464148877943, 'fraction_numerical': 0.0019704433497536944, 'mean_word_length': 4.58094074526573, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4629740', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2590, 'n_words': 807}