<commit_before>package com.mailosaur;

import com.mailosaur.model.*;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

public final class MailboxApi {
	
	 static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
	 static final JsonFactory JSON_FACTORY = new GsonFactory();
	 	 
	 static final HttpRequestFactory requestFactory =
		        HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
			          public void initialize(HttpRequest request) {
			        	request.addParser(new JsonHttpParser(JSON_FACTORY));
			          }
			        });

	final String BASE_URI = "https://api.mailosaur.com/v2";
	final String MAILBOX;
	final String API_KEY;
	
	public MailboxApi(String mailbox, String apiKey) {
		MAILBOX = mailbox;
		API_KEY = apiKey;
	}
	
	void writeByteArrayToFile(final byte[] fileBytes, final String filePath) throws com.mailosaur.exception.MailosaurException {
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream(filePath);			
		} catch (FileNotFoundException e) {
			throw new com.mailosaur.exception.MailosaurException("Unable to write to file: File not found " + filePath, e);
		} finally {
		    try {
		    	stream.write(fileBytes);
				stream.close();
			} catch (IOException e) {
				throw new com.mailosaur.exception.MailosaurException("Unable to write to file", e);
			}
		}
	}
	
	String buildQueryString(final Map<String, String> map) throws UnsupportedEncodingException {
		Map<String, String> queryParams = new HashMap<String, String>();
		queryParams.put("key", API_KEY);
		queryParams.putAll(map);
		
		StringBuilder sb = new StringBuilder();
		  for(Entry<String, String> entry : queryParams.entrySet()){
		      if(sb.length() > 0){
		          sb.append('&');
		      }
		      
		      sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
		      	.append('=')
		      	.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
		  }
		  return sb.toString();
	}
	
	GenericUrl buildUrl(final String path, Map<String, String> queryParams) throws UnsupportedEncodingException {
		return new GenericUrl(BASE_URI + path + "?" + buildQueryString(queryParams));
	}
	
	HttpRequest buildRequest(String method, String path) throws com.mailosaur.exception.MailosaurException {
		return buildRequest(method, path, null);
	}
	
	HttpRequest buildRequest(String method, String path, Map<String, String> queryParams) throws com.mailosaur.exception.MailosaurException {<commit_msg>Fix bug when deleting a single email.<commit_after>package com.mailosaur;

import com.mailosaur.model.*;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

public final class MailboxApi {
	
	 static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
	 static final JsonFactory JSON_FACTORY = new GsonFactory();
	 	 
	 static final HttpRequestFactory requestFactory =
		        HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
			          public void initialize(HttpRequest request) {
			        	request.addParser(new JsonHttpParser(JSON_FACTORY));
			          }
			        });

	final String BASE_URI = "https://api.mailosaur.com/v2";
	final String MAILBOX;
	final String API_KEY;
	
	public MailboxApi(String mailbox, String apiKey) {
		MAILBOX = mailbox;
		API_KEY = apiKey;
	}
	
	void writeByteArrayToFile(final byte[] fileBytes, final String filePath) throws com.mailosaur.exception.MailosaurException {
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream(filePath);			
		} catch (FileNotFoundException e) {
			throw new com.mailosaur.exception.MailosaurException("Unable to write to file: File not found " + filePath, e);
		} finally {
		    try {
		    	stream.write(fileBytes);
				stream.close();
			} catch (IOException e) {
				throw new com.mailosaur.exception.MailosaurException("Unable to write to file", e);
			}
		}
	}
	
	String buildQueryString(final Map<String, String> map) throws UnsupportedEncodingException {
		Map<String, String> queryParams = new HashMap<String, String>();
		queryParams.put("key", API_KEY);
        if(map!=null)
		    queryParams.putAll(map);
		
		StringBuilder sb = new StringBuilder();
		  for(Entry<String, String> entry : queryParams.entrySet()){
		      if(sb.length() > 0){
		          sb.append('&');
		      }
		      
		      sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
		      	.append('=')
		      	.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
		  }
		  return sb.toString();
	}
	
	GenericUrl buildUrl(final String path, Map<String, String> queryParams) throws UnsupportedEncodingException {
		return new GenericUrl(BASE_URI + path + "?" + buildQueryString(queryParams));
	}
	
	HttpRequest buildRequest(String method, String path) throws com.mailosaur.exception.MailosaurException {
		return buildRequest(method, path, null);
	}
	
	HttpRequest buildRequest(String method, String path, Map<String, String> queryParams) throws com.mailosaur.exception.MailosaurException {
{'fraction_non_alphanumeric': 0.10796158612143743, 'fraction_numerical': 0.0012391573729863693, 'mean_word_length': 4.219886822958771, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '7239034', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1882, 'n_words': 419}