Skip to content

Commit

Permalink
Update OneSignalClient to include headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezp committed Oct 10, 2024
1 parent a30f51b commit 291ae98
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions iOS_SDK/OneSignalSDK/OneSignalCore/Source/API/OneSignalClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,19 @@ - (void)handleJSONNSURLResponse:(NSURLResponse*)response data:(NSData*)data erro

NSHTTPURLResponse* HTTPResponse = (NSHTTPURLResponse*)response;
NSInteger statusCode = [HTTPResponse statusCode];
NSDictionary *headers = [HTTPResponse allHeaderFields]; // Get the response headers
NSError* jsonError = nil;
NSMutableDictionary* innerJson;

if (data != nil && [data length] > 0) {
innerJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
innerJson[@"httpStatusCode"] = [NSNumber numberWithLong:statusCode];
innerJson[@"headers"] = headers; // Include headers in the innerJson response

[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"network response (%@) with URL %@: %@", NSStringFromClass([request class]), request.urlRequest.URL.absoluteString, innerJson]];
if (jsonError) {
if (failureBlock != nil)
failureBlock([NSError errorWithDomain:@"OneSignal Error" code:statusCode userInfo:@{@"returned" : jsonError}]);
failureBlock([NSError errorWithDomain:@"OneSignal Error" code:statusCode userInfo:@{@"returned" : jsonError, @"headers": headers}]); // Add headers to error block
return;
}
}
Expand All @@ -217,21 +220,22 @@ - (void)handleJSONNSURLResponse:(NSURLResponse*)response data:(NSData*)data erro
if (error == nil && (statusCode == 200 || statusCode == 201 || statusCode == 202)) {
if (successBlock != nil) {
if (innerJson != nil)
successBlock(innerJson);
successBlock(innerJson); // Send success response with headers
else
successBlock(nil);
}
} else if (failureBlock != nil) {
// Make sure to send all the infomation available to the client
// Send failure response with all available information, including headers
if (innerJson != nil && error != nil)
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"returned" : innerJson, @"error": error}]);
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"returned" : innerJson, @"error": error, @"headers": headers}]);
else if (innerJson != nil)
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"returned" : innerJson}]);
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"returned" : innerJson, @"headers": headers}]);
else if (error != nil)
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"error" : error}]);
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"error" : error, @"headers": headers}]);
else
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:nil]);
failureBlock([NSError errorWithDomain:@"OneSignalError" code:statusCode userInfo:@{@"headers": headers}]);
}
}


@end

0 comments on commit 291ae98

Please sign in to comment.