Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$network.request logging #258

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/Jasonette/JasonNetworkAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ - (void)request{
}

NSString *method = self.options[@"method"];
if(!method) method = @"get";
if(dataType && ([dataType isEqualToString:@"html"] || [dataType isEqualToString:@"xml"] || [dataType isEqualToString:@"rss"])){
[self loadCookies];
}
Expand All @@ -154,6 +155,18 @@ - (void)request{
}


if(self.options[@"debug"]) {
[self log:@{
@"type": @"request",
@"options": @{
@"method": method,
@"url": url,
@"header": manager.requestSerializer.HTTPRequestHeaders,
@"body": (parameters ? parameters : @{})
}
}];
}

if(method){
if([[method lowercaseString] isEqualToString:@"post"]){
//dispatch_async(dispatch_queue_create("clientQueue", NULL) , ^{
Expand All @@ -162,6 +175,14 @@ - (void)request{
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// Nothing
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if(self.options[@"debug"]) {
[self log:@{
@"type": @"response",
@"options": @{
@"method": method, @"url": url, @"dataType": (dataType ? dataType : @"json"), @"responseObject": responseObject
}
}];
}
// Ignore if the url is different
if(![JasonHelper isURL:task.originalRequest.URL equivalentTo:url]) return;
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -187,6 +208,14 @@ - (void)request{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[manager.operationQueue cancelAllOperations];
[manager PUT:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
if(self.options[@"debug"]) {
[self log:@{
@"type": @"response",
@"options": @{
@"method": method, @"url": url, @"dataType": (dataType ? dataType : @"json"), @"responseObject": responseObject
}
}];
}
// Ignore if the url is different
if(![JasonHelper isURL:task.originalRequest.URL equivalentTo:url]) return;
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -213,6 +242,14 @@ - (void)request{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[manager.operationQueue cancelAllOperations];
[manager DELETE:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
if(self.options[@"debug"]) {
[self log:@{
@"type": @"response",
@"options": @{
@"method": method, @"url": url, @"dataType": (dataType ? dataType : @"json"), @"responseObject": responseObject
}
}];
}
// Ignore if the url is different
if(![JasonHelper isURL:task.originalRequest.URL equivalentTo:url]) return;
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -247,6 +284,14 @@ - (void)request{
[manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
// Nothing
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if(self.options[@"debug"]) {
[self log:@{
@"type": @"response",
@"options": @{
@"method": method, @"url": url, @"dataType": (dataType ? dataType : @"json"), @"responseObject": responseObject
}
}];
}
// Ignore if the url is different
if(![JasonHelper isURL:task.originalRequest.URL equivalentTo:url]) return;
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -405,4 +450,24 @@ - (void)s3UploadDidSucceed: (NSString *)upload_filename withOriginalUrl: origina
[[Jason client] success: @{@"filename": upload_filename, @"file_name": upload_filename} withOriginalUrl:original_url];
}


// debug mode
- (void)log: (NSDictionary *) log {
if([log[@"type"] isEqualToString:@"request"]){
NSString *method = log[@"options"][@"method"];
NSLog(@"🔵 Network Request: %@ 🔵", (method == NULL ? @"GET" : [method uppercaseString]));
NSLog(@"URL: %@", log[@"options"][@"url"]);
if(log[@"options"][@"header"]) {
NSLog(@"\nHeader: %@", log[@"options"][@"header"]);
}
if(log[@"options"][@"body"]) {
NSLog(@"\nBody: %@", log[@"options"][@"body"]);
}
} else if([log[@"type"] isEqualToString:@"response"]) {
NSString *method = log[@"options"][@"method"];
NSLog(@"🔷 Network Response: %@ 🔷", (method == NULL ? @"GET" : [method uppercaseString]));
NSLog(@"DataType: %@", log[@"options"][@"dataType"]);
NSLog(@"ResponseObject: \n %@", log[@"options"][@"responseObject"]);
}
}
@end