Skip to content

Commit 1ce0b66

Browse files
committed
Merge pull request #198 from ParsePlatform/nlutsenko.command.notifications
Added response body string to network notifications, don't supply any user info if running outside of debug mode.
2 parents 275a5a9 + 49e6328 commit 1ce0b66

File tree

10 files changed

+68
-21
lines changed

10 files changed

+68
-21
lines changed

Parse/Internal/Commands/CommandRunner/PFCommandRunningConstants.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@ extern NSString *const PFCommandRunnerDidReceiveURLResponseNotification;
5050

5151
/*!
5252
@abstract The key of request(NSURLRequest) in the userInfo dictionary of a notification.
53+
@note This key is populated in userInfo, only of `PFLogLevel` in `PFLogger` is set to `PFLogLevelDebug`.
5354
*/
5455
extern NSString *const PFCommandRunnerNotificationURLRequestUserInfoKey;
5556

5657
/*!
5758
@abstract The key of response(NSHTTPURLResponse) in the userInfo dictionary of a notification.
59+
@note This key is populated in userInfo, only of `PFLogLevel` in `PFLogger` is set to `PFLogLevelDebug`.
5860
*/
5961
extern NSString *const PFCommandRunnerNotificationURLResponseUserInfoKey;
62+
63+
/*!
64+
@abstract The key of repsonse body (usually `NSString` with JSON) in the userInfo dictionary of a notification.
65+
@note This key is populated in userInfo, only of `PFLogLevel` in `PFLogger` is set to `PFLogLevelDebug`.
66+
*/
67+
extern NSString *const PFCommandRunnerNotificationURLResponseBodyUserInfoKey;

Parse/Internal/Commands/CommandRunner/PFCommandRunningConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
NSString *const PFCommandRunnerDidReceiveURLResponseNotification = @"PFCommandRunnerDidReceiveURLResponseNotification";
3131
NSString *const PFCommandRunnerNotificationURLRequestUserInfoKey = @"PFCommandRunnerNotificationURLRequestUserInfoKey";
3232
NSString *const PFCommandRunnerNotificationURLResponseUserInfoKey = @"PFCommandRunnerNotificationURLResponseUserInfoKey";
33+
NSString *const PFCommandRunnerNotificationURLResponseBodyUserInfoKey = @"PFCommandRunnerNotificationURLResponseBodyUserInfoKey";

Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,29 @@ + (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSStrin
252252

253253
- (void)urlSession:(PFURLSession *)session willPerformURLRequest:(NSURLRequest *)request {
254254
[[BFExecutor defaultPriorityBackgroundExecutor] execute:^{
255-
NSDictionary *userInfo = @{ PFCommandRunnerNotificationURLRequestUserInfoKey : request };
255+
NSDictionary *userInfo = ([PFLogger sharedLogger].logLevel == PFLogLevelDebug ?
256+
@{ PFCommandRunnerNotificationURLRequestUserInfoKey : request } : nil);
256257
[self.notificationCenter postNotificationName:PFCommandRunnerWillSendURLRequestNotification
257258
object:self
258259
userInfo:userInfo];
259260
}];
260261
}
261262

262-
- (void)urlSession:(PFURLSession *)session didPerformURLRequest:(NSURLRequest *)request withURLResponse:(nullable NSURLResponse *)response {
263+
- (void)urlSession:(PFURLSession *)session
264+
didPerformURLRequest:(NSURLRequest *)request
265+
withURLResponse:(nullable NSURLResponse *)response
266+
responseString:(nullable NSString *)responseString {
263267
[[BFExecutor defaultPriorityBackgroundExecutor] execute:^{
264-
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
265-
userInfo[PFCommandRunnerNotificationURLRequestUserInfoKey] = request;
266-
if (response) {
267-
userInfo[PFCommandRunnerNotificationURLResponseUserInfoKey] = response;
268+
NSMutableDictionary *userInfo = nil;
269+
if ([PFLogger sharedLogger].logLevel == PFLogLevelDebug) {
270+
userInfo = [NSMutableDictionary dictionaryWithObject:request
271+
forKey:PFCommandRunnerNotificationURLRequestUserInfoKey];
272+
if (response) {
273+
userInfo[PFCommandRunnerNotificationURLResponseUserInfoKey] = response;
274+
}
275+
if (responseString) {
276+
userInfo[PFCommandRunnerNotificationURLResponseBodyUserInfoKey] = responseString;
277+
}
268278
}
269279
[self.notificationCenter postNotificationName:PFCommandRunnerDidReceiveURLResponseNotification
270280
object:self

Parse/Internal/Commands/CommandRunner/URLSession/Session/PFURLSession.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ NS_ASSUME_NONNULL_BEGIN
2222
@protocol PFURLSessionDelegate <NSObject>
2323

2424
- (void)urlSession:(PFURLSession *)session willPerformURLRequest:(NSURLRequest *)request;
25-
- (void)urlSession:(PFURLSession *)session didPerformURLRequest:(NSURLRequest *)request withURLResponse:(nullable NSURLResponse *)response;
25+
26+
- (void)urlSession:(PFURLSession *)session didPerformURLRequest:(NSURLRequest *)request withURLResponse:(nullable NSURLResponse *)response responseString:(nullable NSString *)string;
2627

2728
@end
2829

Parse/Internal/Commands/CommandRunner/URLSession/Session/PFURLSession.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ - (BFTask *)_performDataTask:(NSURLSessionDataTask *)dataTask withDelegate:(PFUR
177177

178178
BFTask *resultTask = [delegate.resultTask continueWithBlock:^id(BFTask *task) {
179179
@strongify(self);
180-
[self.delegate urlSession:self didPerformURLRequest:dataTask.originalRequest withURLResponse:delegate.response];
180+
[self.delegate urlSession:self
181+
didPerformURLRequest:dataTask.originalRequest
182+
withURLResponse:delegate.response
183+
responseString:delegate.responseString];
181184

182185
[self _removeDelegateForTaskWithIdentifier:taskIdentifier];
183186
return task;

Parse/Internal/Commands/CommandRunner/URLSession/Session/TaskDelegate/PFURLSessionDataTaskDelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
2020
@property (nonatomic, strong, readonly) BFTask *resultTask;
2121

2222
@property (nonatomic, strong, readonly) NSHTTPURLResponse *response;
23+
@property (nullable, nonatomic, copy, readonly) NSString *responseString;
2324

2425
- (instancetype)init NS_UNAVAILABLE;
2526
- (instancetype)initForDataTask:(NSURLSessionDataTask *)dataTask

Parse/Internal/Commands/CommandRunner/URLSession/Session/TaskDelegate/PFURLSessionDataTaskDelegate_Private.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import "PFURLSessionDataTaskDelegate.h"
1111

12+
NS_ASSUME_NONNULL_BEGIN
13+
1214
@interface PFURLSessionDataTaskDelegate ()
1315

1416
@property (nonatomic, strong, readonly) dispatch_queue_t dataQueue;
@@ -19,10 +21,14 @@
1921
@property (nonatomic, strong, readonly) NSOutputStream *dataOutputStream;
2022
@property (nonatomic, assign, readonly) uint64_t downloadedBytes;
2123

22-
@property (nonatomic, strong) id result;
23-
@property (nonatomic, strong) NSError *error;
24+
@property (nullable, nonatomic, strong) id result;
25+
@property (nullable, nonatomic, strong) NSError *error;
26+
27+
@property (nullable, nonatomic, copy, readwrite) NSString *responseString;
2428

2529
- (void)_taskDidFinish NS_REQUIRES_SUPER;
2630
- (void)_taskDidCancel NS_REQUIRES_SUPER;
2731

2832
@end
33+
34+
NS_ASSUME_NONNULL_END

Parse/Internal/Commands/CommandRunner/URLSession/Session/TaskDelegate/PFURLSessionJSONDataTaskDelegate.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ @implementation PFURLSessionJSONDataTaskDelegate
3232
- (void)_taskDidFinish {
3333
NSData *data = [self.dataOutputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
3434

35-
NSString *resultString = nil;
3635
id result = nil;
3736

3837
NSError *jsonError = nil;
3938
if (data) {
40-
resultString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
39+
self.responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
4140
result = [NSJSONSerialization JSONObjectWithData:data
4241
options:0
4342
error:&jsonError];
@@ -64,7 +63,7 @@ - (void)_taskDidFinish {
6463
if (self.response.statusCode >= 200) {
6564
if (self.response.statusCode < 400) {
6665
PFCommandResult *commandResult = [PFCommandResult commandResultWithResult:result
67-
resultString:resultString
66+
resultString:self.responseString
6867
httpResponse:self.response];
6968
self.result = commandResult;
7069
} else if ([result isKindOfClass:[NSDictionary class]]) {
@@ -78,7 +77,7 @@ - (void)_taskDidFinish {
7877
}
7978

8079
if (!self.result && !self.error) {
81-
self.error = [PFErrorUtilities errorWithCode:kPFErrorInternalServer message:resultString];
80+
self.error = [PFErrorUtilities errorWithCode:kPFErrorInternalServer message:self.responseString];
8281
}
8382
[super _taskDidFinish];
8483
}

Parse/Internal/PFLogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ A shared instance of `PFLogger` that should be used for all logging.
2626
2727
@returns An shared singleton instance of `PFLogger`.
2828
*/
29-
+ (instancetype)sharedLogger;
29+
+ (instancetype)sharedLogger; //TODO: (nlutsenko) Convert to use an instance everywhere instead of a shared singleton.
3030

3131
///--------------------------------------
3232
/// @name Logging Messages

Tests/Unit/URLSessionTests.m

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ - (void)testPerformDataRequestSuccess {
131131
sessionDelegate = (id)session;
132132

133133
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
134-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:[OCMArg isNotNil]]);
134+
OCMExpect([delegate urlSession:session
135+
didPerformURLRequest:mockedURLRequest
136+
withURLResponse:[OCMArg isNotNil]
137+
responseString:[OCMArg isNotNil]]);
135138

136139
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
137140
[[session performDataURLRequestAsync:mockedURLRequest forCommand:mockedCommand cancellationToken:nil] continueWithBlock:^id(BFTask *task) {
@@ -222,7 +225,10 @@ - (void)testPerformDataRequestCancellation {
222225
sessionDelegate = (id)session;
223226

224227
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
225-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:[OCMArg isNotNil]]);
228+
OCMExpect([delegate urlSession:session
229+
didPerformURLRequest:mockedURLRequest
230+
withURLResponse:[OCMArg isNotNil]
231+
responseString:nil]);
226232

227233
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
228234
[[session performDataURLRequestAsync:mockedURLRequest
@@ -266,7 +272,10 @@ - (void)testPerformDataRequestError {
266272
sessionDelegate = (id)session;
267273

268274
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
269-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:nil]);
275+
OCMExpect([delegate urlSession:session
276+
didPerformURLRequest:mockedURLRequest
277+
withURLResponse:nil
278+
responseString:[OCMArg isNotNil]]);
270279

271280
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
272281
[[session performDataURLRequestAsync:mockedURLRequest forCommand:mockedCommand cancellationToken:nil]
@@ -367,7 +376,10 @@ - (void)testFileUploadSuccess {
367376
sessionDelegate = (id)session;
368377

369378
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
370-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:[OCMArg isNotNil]]);
379+
OCMExpect([delegate urlSession:session
380+
didPerformURLRequest:mockedURLRequest
381+
withURLResponse:[OCMArg isNotNil]
382+
responseString:[OCMArg isNotNil]]);
371383

372384
__block int lastProgress = 0;
373385

@@ -457,7 +469,10 @@ - (void)testFileUploadRequestCancellation {
457469
sessionDelegate = (id)session;
458470

459471
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
460-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:[OCMArg isNotNil]]);
472+
OCMExpect([delegate urlSession:session
473+
didPerformURLRequest:mockedURLRequest
474+
withURLResponse:[OCMArg isNotNil]
475+
responseString:nil]);
461476

462477
__block int lastProgress = 0;
463478

@@ -527,7 +542,10 @@ - (void)testCaching {
527542
sessionDelegate = (id)session;
528543

529544
OCMExpect([delegate urlSession:session willPerformURLRequest:mockedURLRequest]);
530-
OCMExpect([delegate urlSession:session didPerformURLRequest:mockedURLRequest withURLResponse:[OCMArg isNotNil]]);
545+
OCMExpect([delegate urlSession:session
546+
didPerformURLRequest:mockedURLRequest
547+
withURLResponse:[OCMArg isNotNil]
548+
responseString:[OCMArg isNotNil]]);
531549

532550
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
533551
[[session performDataURLRequestAsync:mockedURLRequest forCommand:mockedCommand cancellationToken:nil]

0 commit comments

Comments
 (0)