<commit_before>/**
 * File: Client.java
 * Original Author: <NAME> <<EMAIL>>
 * Copyright 2012 Answers.com
 */
package com.likethecolor.alchemy.api;

import com.likethecolor.alchemy.api.call.AbstractCall;
import com.likethecolor.alchemy.api.entity.Response;
import com.likethecolor.alchemy.api.params.Params;
import com.likethecolor.alchemy.api.validator.ApiKeyFileNameValidator;
import com.likethecolor.alchemy.api.validator.ApiKeyValidator;
import com.likethecolor.alchemy.api.validator.OutputStatusValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Client {
  private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
  private static final String API_KEY = "apikey";
  private static final String API_KEY_FILE = "/alchemy-key.txt";
  private static final String HEADER_CONTENT_LENGTH = "Content-Length";
  private static final String DEFAULT_URL = "http://access.alchemyapi.com/calls/";

  private String apiKey;
  private String requestUri = DEFAULT_URL;

  public Client() throws IOException {
    this(API_KEY_FILE);
  }

  public Client(final String apiFileNameResource) throws IOException {
    loadAPIKey(apiFileNameResource);
  }

  private void loadAPIKey(final String apiFileNameResource) throws IOException {
    ApiKeyFileNameValidator.validate(apiFileNameResource);

    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(apiFileNameResource)));
      setAPIKey(bufferedReader.readLine());
    }
    finally {
      if(bufferedReader != null) {
        bufferedReader.close();
      }
    }
  }

  public void setAPIKey(final String apiKey) {
    ApiKeyValidator.validate(apiKey);

    LOGGER.debug("api key: " + apiKey);
    this.apiKey = apiKey.trim();
  }

  public Response call(final AbstractCall call) throws IOException {
    final Params params = call.getParams();
    call.getCallType().setValuesOnParams(params);
    final String callName = call.getCallType().getPrefix() + call.getName();
    final String callPrefix = call.getCallType().getType();

    String jsonString;
    if(Constants.CALL_TYPE_URL.equals(call.getCallType().getType())) {
      jsonString = get(callName, callPrefix, params);
    }
    else {
      jsonString = post(callName, callPrefix, params);
    }
    LOGGER.debug("json string: " + jsonString);
    return call.parse(jsonString);
  }

  private String get(final String callName, String callPrefix, Params params) throws IOException {
    String jsonString = null;
    StringBuilder uri = new StringBuilder()
        .append(requestUri).append(callPrefix).append('/').append(callName)
        .append('?').append(API_KEY).append("=").append(this.apiKey)
        .append(params.toString());
<commit_msg>issue 5: loading of data from files should be handled in another class<commit_after>/**
 * File: Client.java
 * Original Author: <NAME> <<EMAIL>>
 * Copyright 2012 Answers.com
 */
package com.likethecolor.alchemy.api;

import com.likethecolor.alchemy.api.call.AbstractCall;
import com.likethecolor.alchemy.api.entity.Response;
import com.likethecolor.alchemy.api.params.Params;
import com.likethecolor.alchemy.api.validator.ApiKeyValidator;
import com.likethecolor.alchemy.api.validator.OutputStatusValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Client {
  private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
  private static final String API_KEY = "apikey";
  private static final String HEADER_CONTENT_LENGTH = "Content-Length";
  private static final String DEFAULT_URL = "http://access.alchemyapi.com/calls/";

  private String apiKey;
  private String requestUri = DEFAULT_URL;

  public Client() throws IOException {
  }

  public Client(final String apiKey) throws IOException {
    setAPIKey(apiKey);
  }

  private void setAPIKey(final String apiKey) {
    ApiKeyValidator.validate(apiKey);

    LOGGER.debug("api key: " + apiKey);
    this.apiKey = apiKey.trim();
  }

  public Response call(final AbstractCall call) throws IOException {
    final Params params = call.getParams();
    call.getCallType().setValuesOnParams(params);
    final String callName = call.getCallType().getPrefix() + call.getName();
    final String callPrefix = call.getCallType().getType();

    String jsonString;
    if(Constants.CALL_TYPE_URL.equals(call.getCallType().getType())) {
      jsonString = get(callName, callPrefix, params);
    }
    else {
      jsonString = post(callName, callPrefix, params);
    }
    LOGGER.debug("json string: " + jsonString);
    return call.parse(jsonString);
  }

  private String get(final String callName, String callPrefix, Params params) throws IOException {
    String jsonString = null;
    StringBuilder uri = new StringBuilder()
        .append(requestUri).append(callPrefix).append('/').append(callName)
        .append('?').append(API_KEY).append("=").append(this.apiKey)
        .append(params.toString());

{'fraction_non_alphanumeric': 0.11072852482783617, 'fraction_numerical': 0.002355926060166727, 'mean_word_length': 4.507984031936128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '4149529', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1612, 'n_words': 356}