<commit_before>package dk.apaq.simplepay.client;

import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import dk.apaq.simplepay.client.model.Token;
import dk.apaq.simplepay.client.model.Transaction;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.joda.money.Money;

/**
 * Manages a session for communicating with SimplePay.
 * <br><br>
 * An application can have as many sessions it likes with each having their own key. In most systems a single session will suffice, but larger
 * multiuser systems might need access to multiple SimplePay accounts through the same Java application.<br><br>
 *
 * Example:<br>
 * <code>
 * SimplePaySession spay = new SimplePaySession("<key>");
 * Token token = spay.createToken("<card>", 2015, 11, "<cvd>");
 *
 */
public class SimplePaySession {

    private static final String DEFAULT_SERVICEURL = "http://simplepay.dk";
    private static final GsonBuilder GSON_BUILDER = new GsonBuilder();

    static {
        GSON_BUILDER.registerTypeAdapter(Date.class, new DateTimeDeserializer());
    }
    
    private static class DateTimeDeserializer implements JsonDeserializer<Date> {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    }

    private final String key;
    private final String serviceUrl;
    private final DefaultHttpClient client;
    private final Gson gson;
    
    public SimplePaySession(String key) {
        this(key, DEFAULT_SERVICEURL);
    }

    public SimplePaySession(String key, String serviceUrl) {
        this.key = key;
        this.serviceUrl = serviceUrl;
        this.client = new DefaultHttpClient();
        this.client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(key, ""));
        gson = GSON_BUILDER.create();
    }

    public Token createToken(String cardNumber, int expireYear, int expireMonth, String cvd) throws IOException {
        HttpPost method = new HttpPost(serviceUrl + "/tokens");
        method.addHeader("Accept", "application/json");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("cardNumber", cardNumber));
        nvps.add(new BasicNameValuePair("expireYear", Integer.toString(expireYear)));
        nvps.add(new BasicNameValuePair("expireMonth", Integer.toString(expireMonth)));
        nvps.add(new BasicNameValuePair("cvd", cvd));
        method.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = client.execute(method);

        JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));
        return gson.fromJson(reader, Token.class);
    }

    public Transaction createTransaction(String tokenId, String refId, Money amount) {
        return null;
    }

    public Transaction getTransaction(String id) {
        return null;
    }

    public Transaction chargeTransaction(String id) {
        return chargeTransaction(id, null);
    }

    public Transaction chargeTransaction(String id, Money amount) {
        return null;
    }

    public Transaction refundTransaction(String id) {
        return refundTransaction(id, null);
    }

    public Transaction refundTransaction(String id, Money amount) {
        return null;
    }

    public Transaction cancelTransaction(String id) {
        return null;
    }
}<commit_msg>Added support for creating transaction.<commit_after>package dk.apaq.simplepay.client;

import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import dk.apaq.simplepay.client.model.Token;
import dk.apaq.simplepay.client.model.Transaction;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.joda.money.Money;

/**
 * Manages a session for communicating with SimplePay.
 * <br><br>
 * An application can have as many sessions it likes with each having their own key. In most systems a single session will suffice, but larger
 * multiuser systems might need access to multiple SimplePay accounts through the same Java application.<br><br>
 *
 * Example:<br>
 * <code>
 * SimplePaySession spay = new SimplePaySession("<key>");
 * Token token = spay.createToken("<card>", 2015, 11, "<cvd>");
 *
 */
public class SimplePaySession {

    private static final String DEFAULT_SERVICEURL = "http://simplepay.dk";
    private static final GsonBuilder GSON_BUILDER = new GsonBuilder();

    static {
        GSON_BUILDER.registerTypeAdapter(Date.class, new DateTimeDeserializer());
    }
    
    private static class DateTimeDeserializer implements JsonDeserializer<Date> {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    }

    private final String key;
    private final String serviceUrl;
    private final DefaultHttpClient client;
    private final Gson gson;
    
    public SimplePaySession(String key) {
        this(key, DEFAULT_SERVICEURL);
    }

    public SimplePaySession(String key, String serviceUrl) {
        this.key = key;
        this.serviceUrl = serviceUrl;
        this.client = new DefaultHttpClient();
        this.client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(key, ""));
        gson = GSON_BUILDER.create();
    }

    public Token createToken(String cardNumber, int expireYear, int expireMonth, String cvd) throws IOException {
        HttpPost method = new HttpPost(serviceUrl + "/tokens");
        method.addHeader("Accept", "application/json");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("cardNumber", cardNumber));
        nvps.add(new BasicNameValuePair("expireYear", Integer.toString(expireYear)));
        nvps.add(new BasicNameValuePair("expireMonth", Integer.toString(expireMonth)));
        nvps.add(new BasicNameValuePair("cvd", cvd));
        method.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = client.execute(method);

        JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));
        return gson.fromJson(reader, Token.class);
    }

    public Transaction createTransaction(String tokenId, String refId, Money amount) {
        HttpPost method = new HttpPost(serviceUrl + "/transactions");
        method.addHeader("Accept", "application/json");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("token", tokenId));
        nvps.add(new BasicNameValuePair("refId", refId));
        nvps.add(new BasicNameValuePair("amount", amount.getAmountMinorLong()));
        nvps.add(new BasicNameValuePair("currency", amount.getCurrencyUnit().getCurrencyCode()));
        method.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = client.execute(method);

        JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));
        return gson.fromJson(reader, Transaction.class);
    }

    public Transaction getTransaction(String id) {
        return null;
    }

    public Transaction chargeTransaction(String id) {
        return chargeTransaction(id, null);
    }

    public Transaction chargeTransaction(String id, Money amount) {
        return null;
    }

    public Transaction refundTransaction(String id) {
        return refundTransaction(id, null);
    }

    public Transaction refundTransaction(String id, Money amount) {
        return null;
    }

    public Transaction cancelTransaction(String id) {
        return null;
    }
}
{'fraction_non_alphanumeric': 0.09973074851911685, 'fraction_numerical': 0.0012924071082390954, 'mean_word_length': 3.9551760939167555, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '1795894', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2534, 'n_words': 614}