<commit_before>MTPocket
========

A objective-c networking library that doesn't suck.
Gives you a request object that does all the work for you and a response object that has all the info you'd ever want to know about the transaction.
SUPER easy to use, see examples below.

### Advantages

1. It gives you comprehensive response object that contains all the info you'd ever want to know about the response:
	- a boolean success property, yes or no if successful.
	- the dictionary/array object generated from the response data.
	- the raw data returned by the server (for debugging).
	- the raw data returned by the server converted to a UTF-8 encoded string (for even easier debugging).
	- the original request object.
	- the apple response object (has status codes, etc.).
	- an error object, nil if no error.
	- a status property with common status codes mapped to easy to remember/autocompleted enums.
	- the format the response is in (JSON/XML/TEXT).
2. It allows you to have fine grained control, if you want it, or you can use convenience methods.
3. It allows you to either call it synchronously so you can control what queue its on, which I like, or asynchronously on a global queue, if you don't care.
4. It's dead simple, two simple components, a request and response object.
5. The enums help a lot and clearly imply your options.
6. Easily upload files as multipart/form-data.
7. Track the progress of a large download or upload.


### Installation

In your Podfile, add this line:

    pod "MTPocket"

pod? => https://github.com/CocoaPods/CocoaPods/

NOTE: You may need to add `-all_load` to "Other Linker Flags" in your targets build settings if the pods library only contains categories.

### Example Usage

Let's start simple:

	MTPocketResponse *response = [MTPocketRequest requestForURL:URL format:MTPocketFormatHTML].synchronous;
	
	if (response.success) {
		// yeah!
	}
	else {
		if (response.status == MTPocketResultNoConnection) {
			NSLog(@"No internets.");
		}
	}

Next steps:

	MTPocketResponse *response = [MTPocketRequest requestForURL:[NSURL URLWithString:@"stitches" relativeToURL:_baseURL]
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatJSON
                                                       username:@"username"
                                                       password:@"password"
                                                           body:nil].synchronous;
														
	if (response.success) {
		NSLog(@"%@", [[response.body firstObject] objectForKey:@"thread_color"]); // => red
	}
	else if (response.status == MTPocketStatusUnauthorized) {
		// code to let user update their login info
	}

Post to the server:

	NSDictionary *dict = @{ @"stitch" : @{ @"thread_color" : @"blue", @"length" : @3 } };
	MTPocketResponse *response	= [MTPocketRequest requestForURL:[NSURL URLWithString:@"stitches" relativeToURL:_baseURL]
                                                          method:MTPocketMethodPOST
                                                          format:MTPocketFormatJSON
                                                        username:nil
                                                        password:nil
                                                            body:dict].synchronous;

An easy async example:

	NSURLConnection *connection = [MTPocketRequest requestForURL:BASE_URL
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatHTML
                                                           body:nil
                                                        success:^(MTPocketResponse *response) {
                                                            NSLog(@"%@", response.body);
                                                        } failure:^(MTPocketResponse *response) {
															NSLog(@"%@", response.error);
                                                        }].asynchronous;

Let's monitor the progress of a large request:

	NSURLConnection *connection = [MTPocketRequest requestForURL:BASE_URL
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatHTML
                                                           body:nil
                                                 downloadToFile:nil
                                               downloadProgress:^(long long bytesLoaded, long long bytesTotal) {
                                                   NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
                                               }
                                                        success:^(MTPocketResponse *response) {
															NSLog(@"%@", response.body);
                                                        }
                                                        failure:^(MTPocketResponse *response) {
															NSLog(@"%@", response.error);
                                                        }].asynchronous;

Easy enough, now let's download a file and save it to disk:

	    NSString *location = [DOCS_DIR stringByAppendingPathComponent:@"test.mp3"];
		
	    NSURLConnection *connection = [MTPocketRequest requestForURL:DOWNLOAD_FILE_URL
	                                                         method:MTPocketMethodGET
	                                                         format:MTPocketFormatHTML
	                                                           body:nil
	                                                 downloadToFile:location
	                                               downloadProgress:^(long long bytesLoaded, long long bytesTotal) {
														NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
	                                               }
	                                                        success:^(MTPocketResponse *successResponse) {
	                                                            NSLog(@"%@", response.body);
	                                                        }
	                                                        failure:^(MTPocketResponse *response) {
																NSLog(@"%@", response.error);
	                                                        }].asynchronous;

Ok, now this is cool, because normally it would be a lot of work:

		NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];
	    NSData *fileData = [NSData dataWithContentsOfFile:imagePath];
		
	    NSURLConnection *connection = [MTPocketRequest requestForURL:UPLOAD_FILE_URL
	                                                         method:MTPocketMethodFILE
	                                                         format:MTPocketFormatJSON
	                                                           body:fileData
	                                                 uploadFilename:@"test.jpg"
	                                                uploadFormField:@"files[]"
	                                                 uploadMIMEType:@"image/jpeg"
	                                                 uploadProgress:^(long long bytesLoaded, long long bytesTotal) {
														NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
	                                                 }
	                                                        success:^(MTPocketResponse *successResponse) {
																NSLog(@"%@", response.body);
	                                                        }
	                                                        failure:^(MTPocketResponse *response) {
																NSLog(@"%@", response.error);
	                                                        }].asynchronous;

That's just the standard stuff. say you want to own totally custom request:<commit_msg>Almost a complete revamp of MTPocket. Be careful when upgrading.<commit_after>MTPocket
========

A objective-c networking library that doesn't suck.
Gives you a request object that does all the work for you and a response object that has all the info you'd ever want to know about the transaction.
SUPER easy to use, see examples below.

### Advantages

1. It gives you comprehensive response object that contains all the info you'd ever want to know about the response:
	- a boolean success property, yes or no if successful.
	- the dictionary/array object generated from the response data.
	- the raw data returned by the server (for debugging).
	- the raw data returned by the server converted to a UTF-8 encoded string (for even easier debugging).
	- the original request object.
	- the apple response object (has status codes, etc.).
	- an error object, nil if no error.
	- a status property with common status codes mapped to easy to remember/autocompleted enums.
	- the format the response is in (JSON/XML/TEXT).
2. It allows you to have fine grained control, if you want it, or you can use convenience methods.
3. It allows you to either call it synchronously so you can control what queue its on, which I like, or asynchronously on a global queue, if you don't care.
4. It's dead simple, two simple components, a request and response object.
5. The enums help a lot and clearly imply your options.
6. Easily upload files as multipart/form-data.
7. Track the progress of a large download or upload.


### Installation

In your Podfile, add this line:

    pod "MTPocket"

pod? => https://github.com/CocoaPods/CocoaPods/

NOTE: You may need to add `-all_load` to "Other Linker Flags" in your targets build settings if the pods library only contains categories.

### Example Usage

Let's start simple:

	MTPocketResponse *response = [MTPocketRequest requestForURL:URL format:MTPocketFormatHTML].synchronous;
	
	if (response.success) {
		// yeah!
	}
	else {
		if (response.status == MTPocketResultNoConnection) {
			NSLog(@"No internets.");
		}
	}

Next steps:

	MTPocketResponse *response = [MTPocketRequest requestForURL:[NSURL URLWithString:@"stitches" relativeToURL:_baseURL]
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatJSON
                                                       username:@"username"
                                                       password:@"password"
                                                           body:nil].synchronous;
														
	if (response.success) {
		NSLog(@"%@", [[response.body firstObject] objectForKey:@"thread_color"]); // => red
	}
	else if (response.status == MTPocketStatusUnauthorized) {
		// code to let user update their login info
	}

Post to the server:

	NSDictionary *dict = @{ @"stitch" : @{ @"thread_color" : @"blue", @"length" : @3 } };
	MTPocketResponse *response	= [MTPocketRequest requestForURL:[NSURL URLWithString:@"stitches" relativeToURL:_baseURL]
                                                          method:MTPocketMethodPOST
                                                          format:MTPocketFormatJSON
                                                        username:nil
                                                        password:<PASSWORD>
                                                            body:dict].synchronous;

An easy async example:

	NSURLConnection *connection = [MTPocketRequest requestForURL:BASE_URL
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatHTML
                                                           body:nil
                                                        success:^(MTPocketResponse *response) {
                                                            NSLog(@"%@", response.body);
                                                        } failure:^(MTPocketResponse *response) {
															NSLog(@"%@", response.error);
                                                        }].asynchronous;

Let's monitor the progress of a large request:

	NSURLConnection *connection = [MTPocketRequest requestForURL:BASE_URL
                                                         method:MTPocketMethodGET
                                                         format:MTPocketFormatHTML
                                                           body:nil
                                                 downloadToFile:nil
                                               downloadProgress:^(long long bytesLoaded, long long bytesTotal) {
                                                   NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
                                               }
                                                        success:^(MTPocketResponse *response) {
															NSLog(@"%@", response.body);
                                                        }
                                                        failure:^(MTPocketResponse *response) {
															NSLog(@"%@", response.error);
                                                        }].asynchronous;

Easy enough, now let's download a file and save it to disk:

	    NSString *location = [DOCS_DIR stringByAppendingPathComponent:@"test.mp3"];
		
	    NSURLConnection *connection = [MTPocketRequest requestForURL:DOWNLOAD_FILE_URL
	                                                         method:MTPocketMethodGET
	                                                         format:MTPocketFormatHTML
	                                                           body:nil
	                                                 downloadToFile:location
	                                               downloadProgress:^(long long bytesLoaded, long long bytesTotal) {
														NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
	                                               }
	                                                        success:^(MTPocketResponse *successResponse) {
	                                                            NSLog(@"%@", response.body);
	                                                        }
	                                                        failure:^(MTPocketResponse *response) {
																NSLog(@"%@", response.error);
	                                                        }].asynchronous;

Ok, now this is cool, because normally it would be a lot of work:

		NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];
	    NSData *fileData = [NSData dataWithContentsOfFile:imagePath];
		
	    NSURLConnection *connection = [MTPocketRequest requestForURL:UPLOAD_FILE_URL
	                                                         method:MTPocketMethodFILE
	                                                         format:MTPocketFormatJSON
	                                                           body:fileData
	                                                 uploadFilename:@"test.jpg"
	                                                uploadFormField:@"files[]"
	                                                 uploadMIMEType:@"image/jpeg"
	                                                 uploadProgress:^(long long bytesLoaded, long long bytesTotal) {
														NSLog(@"%@/%@", @(bytesLoaded), @(bytesTotal));
	                                                 }
	                                                        success:^(MTPocketResponse *successResponse) {
																NSLog(@"%@", response.body);
	                                                        }
	                                                        failure:^(MTPocketResponse *response) {
																NSLog(@"%@", response.error);
	                                                        }].asynchronous;

That's just the standard stuff. say you want to cook your own totally custom request:
{'fraction_non_alphanumeric': 0.07107996496058065, 'fraction_numerical': 0.0012514078338130396, 'mean_word_length': 1.1494082840236686, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 218, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3561006', 'n_tokens_mistral': 3464, 'n_tokens_neox': 2946, 'n_words': 1016}