<commit_before>package synapticloop.b2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.request.B2AuthorizeAccountRequest;
import synapticloop.b2.request.B2CreateBucketRequest;
import synapticloop.b2.request.B2DeleteBucketRequest;
import synapticloop.b2.request.B2DeleteFileVersionRequest;
import synapticloop.b2.request.B2DownloadFileByIdRequest;
import synapticloop.b2.request.B2DownloadFileByNameRequest;
import synapticloop.b2.request.B2GetFileInfoRequest;
import synapticloop.b2.request.B2GetUploadUrlRequest;
import synapticloop.b2.request.B2HeadFileByIdRequest;
import synapticloop.b2.request.B2ListBucketsRequest;
import synapticloop.b2.request.B2ListFileNamesRequest;
import synapticloop.b2.request.B2ListFileVersionsRequest;
import synapticloop.b2.request.B2UpdateBucketRequest;
import synapticloop.b2.request.B2UploadFileRequest;
import synapticloop.b2.response.B2AuthorizeAccountResponse;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2DeleteFileVersionResponse;
import synapticloop.b2.response.B2DownloadFileResponse;
import synapticloop.b2.response.B2FileInfoResponse;
import synapticloop.b2.response.B2FileResponse;
import synapticloop.b2.response.B2GetUploadUrlResponse;
import synapticloop.b2.response.B2ListFilesResponse;

/**
 * This is a wrapper class for the underlying calls to the request/response 
 * classes.
 * 
 * @author synapticloop
 */
public class B2ApiClient {

	private String accountId = null;
	private String applicationKey = null;

	private B2AuthorizeAccountResponse b2AuthorizeAccountResponse = null;

	/**
	 * Instantiate a new B2ApiClient object, on first call of any method this 
	 * will attempt to authorize itself against the B2 API with teh account id
	 * and the application key.
	 * 
	 * @param accountId The account id
	 * @param applicationKey the application key
	 */
	public B2ApiClient(String accountId, String applicationKey) {
		this.accountId = accountId;
		this.applicationKey = applicationKey;
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * 
	 *   BUCKET RELATED API ACTIONS
	 * 
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	/**
	 * Create a bucket with the specified name and bucket type (either private 
	 * or public)
	 * 
	 * @param bucketName the name of the bucket
	 * @param bucketType the type of the bucket
	 * 
	 * @return the newly created bucket
	 * 
	 * @throws B2ApiException if the bucket could not be created, of there was an 
	 *     error with the authentication
	 */
	public B2BucketResponse createBucket(String bucketName, BucketType bucketType) throws B2ApiException {
		return(new B2CreateBucketRequest(getB2AuthorizeAccountResponse(), bucketName, bucketType).getResponse());
	}

	/**
	 * Delete a bucket by the bucket identifier
	 * 
	 * @param bucketId the id of the bucket to delete
	 * 
	 * @return the delete bucket response
	 * 
	 * @throws B2ApiException if something went wrong with the call, or the bucket was not empty
	 */
	public B2BucketResponse deleteBucket(String bucketId) throws B2ApiException {
		return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
	}

	/**
	 * Delete a bucket including all of the files that reside within the bucket.
	 * 
	 * <strong>WARNING:</strong> this is a destructive action and will delete 
	 * everything within the bucket and the bucket itself
	 * 
	 * @param bucketId the id of the bucket to delete
	 * 
	 * @return the deleted bucket response
	 * 
	 * @throws B2ApiException if there was an error deleting the bucket, or any 
	 *     of the enclosed files
	 */
	public B2BucketResponse deleteBucketFully(String bucketId) throws B2ApiException {
		B2ListFilesResponse b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, 1000).getResponse();
		String nextFileName = b2ListFilesResponse.getNextFileName();
		String nextFileId = b2ListFilesResponse.getNextFileId();
		while(true) {
			List<B2FileInfoResponse> files = b2ListFilesResponse.getFiles();
			for (B2FileInfoResponse b2FileInfoResponse : files) {
				new B2DeleteFileVersionRequest(getB2AuthorizeAccountResponse(), b2FileInfoResponse.getFileName(), b2FileInfoResponse.getFileId()).getResponse();
			}

			if(null == nextFileName) {
				break;
			} else {
				b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, nextFileName, nextFileId, 1000).getResponse();
				nextFileName = b2ListFilesResponse.getNextFileName();
				nextFileId = b2ListFilesResponse.getNextFileId();
			}
		}

		// now delete the bucket
		return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
	}

	/**
	 * Update a bucket to be a specified type
	 * 
	 * @param bucketId the id of the bucket to set
	 * @param bucketType the type of the bucket
	 * 
	 * @return the bucket response
	 * 
	 * @throws B2ApiException if there was an error updating the bucket
	 */
	public B2BucketResponse updateBucket(String bucketId, BucketType bucketType) throws B2ApiException {
		return(new B2UpdateBucketRequest(getB2AuthorizeAccountResponse(), bucketId, bucketType).getResponse());
	}

	/**
	 * List all of the buckets in the account
	 * 
	 * @return the list of buckets for the account
	 * 
	 * @throws B2ApiException if something went wrong
	 */
	public List<B2BucketResponse> listBuckets() throws B2ApiException {
		return(new B2ListBucketsRequest(getB2AuthorizeAccountResponse()).getResponse());
	}

	/**
	 * List all of the buckets in the account that 
	 * 
	 * @return the list of buckets for the account
	 * 
	 * @throws B2ApiException if something went wrong
	 */
	public List<B2BucketResponse> listBuckets(String pattern) throws B2ApiException {
		return(new B2ListBucketsRequest(getB2AuthorizeAccountResponse()).getResponse());
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * 
	 *   FILE INFORMATION API ACTIONS
	 * 
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	/**
	 * Retrieve the file information for a particular fileId, this includes all 
	 * of the 'X-Bz-Info-*' headers that were passed in with the file creation 
	 * in a map (without the 'X-Bz-Info-' prefix).  For example, if you uploaded
	 * a file with a header X-Bz-Info-Tag, the returned file info map would 
	 * contain a key of 'Tag'
	 * 
	 * @param fileId the file ID to retrieve the information on<commit_msg>removed unnecessary method<commit_after>package synapticloop.b2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.request.B2AuthorizeAccountRequest;
import synapticloop.b2.request.B2CreateBucketRequest;
import synapticloop.b2.request.B2DeleteBucketRequest;
import synapticloop.b2.request.B2DeleteFileVersionRequest;
import synapticloop.b2.request.B2DownloadFileByIdRequest;
import synapticloop.b2.request.B2DownloadFileByNameRequest;
import synapticloop.b2.request.B2GetFileInfoRequest;
import synapticloop.b2.request.B2GetUploadUrlRequest;
import synapticloop.b2.request.B2HeadFileByIdRequest;
import synapticloop.b2.request.B2ListBucketsRequest;
import synapticloop.b2.request.B2ListFileNamesRequest;
import synapticloop.b2.request.B2ListFileVersionsRequest;
import synapticloop.b2.request.B2UpdateBucketRequest;
import synapticloop.b2.request.B2UploadFileRequest;
import synapticloop.b2.response.B2AuthorizeAccountResponse;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2DeleteFileVersionResponse;
import synapticloop.b2.response.B2DownloadFileResponse;
import synapticloop.b2.response.B2FileInfoResponse;
import synapticloop.b2.response.B2FileResponse;
import synapticloop.b2.response.B2GetUploadUrlResponse;
import synapticloop.b2.response.B2ListFilesResponse;

/**
 * This is a wrapper class for the underlying calls to the request/response 
 * classes.
 * 
 * @author synapticloop
 */
public class B2ApiClient {

	private String accountId = null;
	private String applicationKey = null;

	private B2AuthorizeAccountResponse b2AuthorizeAccountResponse = null;

	/**
	 * Instantiate a new B2ApiClient object, on first call of any method this 
	 * will attempt to authorize itself against the B2 API with teh account id
	 * and the application key.
	 * 
	 * @param accountId The account id
	 * @param applicationKey the application key
	 */
	public B2ApiClient(String accountId, String applicationKey) {
		this.accountId = accountId;
		this.applicationKey = applicationKey;
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * 
	 *   BUCKET RELATED API ACTIONS
	 * 
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	/**
	 * Create a bucket with the specified name and bucket type (either private 
	 * or public)
	 * 
	 * @param bucketName the name of the bucket
	 * @param bucketType the type of the bucket
	 * 
	 * @return the newly created bucket
	 * 
	 * @throws B2ApiException if the bucket could not be created, of there was an 
	 *     error with the authentication
	 */
	public B2BucketResponse createBucket(String bucketName, BucketType bucketType) throws B2ApiException {
		return(new B2CreateBucketRequest(getB2AuthorizeAccountResponse(), bucketName, bucketType).getResponse());
	}

	/**
	 * Delete a bucket by the bucket identifier
	 * 
	 * @param bucketId the id of the bucket to delete
	 * 
	 * @return the delete bucket response
	 * 
	 * @throws B2ApiException if something went wrong with the call, or the bucket was not empty
	 */
	public B2BucketResponse deleteBucket(String bucketId) throws B2ApiException {
		return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
	}

	/**
	 * Delete a bucket including all of the files that reside within the bucket.
	 * 
	 * <strong>WARNING:</strong> this is a destructive action and will delete 
	 * everything within the bucket and the bucket itself
	 * 
	 * @param bucketId the id of the bucket to delete
	 * 
	 * @return the deleted bucket response
	 * 
	 * @throws B2ApiException if there was an error deleting the bucket, or any 
	 *     of the enclosed files
	 */
	public B2BucketResponse deleteBucketFully(String bucketId) throws B2ApiException {
		B2ListFilesResponse b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, 1000).getResponse();
		String nextFileName = b2ListFilesResponse.getNextFileName();
		String nextFileId = b2ListFilesResponse.getNextFileId();
		while(true) {
			List<B2FileInfoResponse> files = b2ListFilesResponse.getFiles();
			for (B2FileInfoResponse b2FileInfoResponse : files) {
				new B2DeleteFileVersionRequest(getB2AuthorizeAccountResponse(), b2FileInfoResponse.getFileName(), b2FileInfoResponse.getFileId()).getResponse();
			}

			if(null == nextFileName) {
				break;
			} else {
				b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, nextFileName, nextFileId, 1000).getResponse();
				nextFileName = b2ListFilesResponse.getNextFileName();
				nextFileId = b2ListFilesResponse.getNextFileId();
			}
		}

		// now delete the bucket
		return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
	}

	/**
	 * Update a bucket to be a specified type
	 * 
	 * @param bucketId the id of the bucket to set
	 * @param bucketType the type of the bucket
	 * 
	 * @return the bucket response
	 * 
	 * @throws B2ApiException if there was an error updating the bucket
	 */
	public B2BucketResponse updateBucket(String bucketId, BucketType bucketType) throws B2ApiException {
		return(new B2UpdateBucketRequest(getB2AuthorizeAccountResponse(), bucketId, bucketType).getResponse());
	}

	/**
	 * List all of the buckets in the account
	 * 
	 * @return the list of buckets for the account
	 * 
	 * @throws B2ApiException if something went wrong
	 */
	public List<B2BucketResponse> listBuckets() throws B2ApiException {
		return(new B2ListBucketsRequest(getB2AuthorizeAccountResponse()).getResponse());
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * 
	 *   FILE INFORMATION API ACTIONS
	 * 
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	/**
	 * Retrieve the file information for a particular fileId, this includes all 
	 * of the 'X-Bz-Info-*' headers that were passed in with the file creation 
	 * in a map (without the 'X-Bz-Info-' prefix).  For example, if you uploaded
	 * a file with a header X-Bz-Info-Tag, the returned file info map would 
	 * contain a key of 'Tag'
	 * 
	 * @param fileId the file ID to retrieve the information on
{'fraction_non_alphanumeric': 0.09740805872008564, 'fraction_numerical': 0.016438565639574892, 'mean_word_length': 4.280581348405329, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1879632', 'n_tokens_mistral': 4230, 'n_tokens_neox': 3769, 'n_words': 1048}