Skip to content

Commit

Permalink
$network action logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gliechtenstein committed Aug 19, 2017
1 parent b2cc487 commit bbd0d1a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions app/Jasonette/JasonNetworkAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ - (void)request{
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}

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

if(method){
if([[method lowercaseString] isEqualToString:@"post"]){
Expand All @@ -162,6 +171,13 @@ - (void)request{
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// Nothing
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[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 +203,13 @@ - (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) {
[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 +236,13 @@ - (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) {
[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 +277,13 @@ - (void)request{
[manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
// Nothing
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[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 +442,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

0 comments on commit bbd0d1a

Please sign in to comment.