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

Add apiKey as a mutable property of BugsnagEvent #458

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Bugsnag.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"requires_arc": true,
"public_header_files": [
"Source/BSG_KSCrashReportWriter.h",
"Source/Bugsnag{,MetaData,Configuration,Breadcrumb,Event,Plugin}.h"
"Source/Bugsnag{,Metadata,Configuration,Breadcrumb,Event,Plugin}.h"
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Bugsnag Notifiers on other platforms.
* Add a breadcrumb when Bugsnag first starts with the message "Bugsnag loaded"
[#445](https://github.com/bugsnag/bugsnag-cocoa/pull/445)

* Add a per-Event `apiKey` property. This defaults to the global
`BugsnagConfiguration` value but can be overridden in event passed to the
`Bugsnag.notify()` callback.
[#458](https://github.com/bugsnag/bugsnag-cocoa/pull/458)

## Bug fixes

* Fix possible report corruption when using `notify()` from multiple threads
Expand Down
1 change: 1 addition & 0 deletions Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ typedef NSDictionary *_Nullable (^BugsnagBeforeNotifyHook)(
NSArray *_Nonnull rawEventReports, NSDictionary *_Nonnull report);

@interface BugsnagConfiguration : NSObject

/**
* The API key of a Bugsnag project
*/
Expand Down
30 changes: 17 additions & 13 deletions Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ @interface BugsnagConfiguration ()

@implementation BugsnagConfiguration

// MARK: - Class Methods

/**
* Determine the apiKey-validity of a passed-in string:
* Exactly 32 hexadecimal digits.
*/
+ (BOOL)isValidApiKey:(NSString *)apiKey {
NSCharacterSet *chars = [[NSCharacterSet
characterSetWithCharactersInString:@"0123456789ABCDEF"] invertedSet];
BOOL isHex = (NSNotFound == [[apiKey uppercaseString] rangeOfCharacterFromSet:chars].location);
return isHex && [apiKey length] == BSGApiKeyLength;
}

// MARK: - Instance Methods

/**
* Should not be called, but if it _is_ then fail meaningfully rather than silently
*/
Expand All @@ -70,7 +85,7 @@ - (instancetype)init {
-(instancetype)initWithApiKey:(NSString *)apiKey
error:(NSError * _Nullable __autoreleasing * _Nullable)error
{
if (! [self isValidApiKey:apiKey]) {
if (! [BugsnagConfiguration isValidApiKey:apiKey]) {
*error = [NSError errorWithDomain:BSGConfigurationErrorDomain
code:BSGConfigurationErrorInvalidApiKey
userInfo:nil];
Expand Down Expand Up @@ -279,7 +294,7 @@ - (NSString *)apiKey {
}

- (void)setApiKey:(NSString *)apiKey {
if ([self isValidApiKey:apiKey]) {
if ([BugsnagConfiguration isValidApiKey:apiKey]) {
[self willChangeValueForKey:NSStringFromSelector(@selector(apiKey))];
_apiKey = apiKey;
[self didChangeValueForKey:NSStringFromSelector(@selector(apiKey))];
Expand Down Expand Up @@ -319,17 +334,6 @@ - (BOOL)isValidUrl:(NSURL *)url {
return url != nil && url.scheme != nil && url.host != nil;
}

/**
* Determine the apiKey-validity of a passed-in string:
* Exactly 32 hexadecimal digits.
*/
- (BOOL)isValidApiKey:(NSString *)apiKey {
NSCharacterSet *chars = [[NSCharacterSet
characterSetWithCharactersInString:@"0123456789ABCDEF"] invertedSet];
BOOL isHex = (NSNotFound == [[apiKey uppercaseString] rangeOfCharacterFromSet:chars].location);
return isHex && [apiKey length] == BSGApiKeyLength;
}

- (NSUInteger)maxBreadcrumbs {
return self.breadcrumbs.capacity;
}
Expand Down
7 changes: 7 additions & 0 deletions Source/BugsnagEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ __deprecated_msg("Use toJson: instead.");
*/
@property(readonly, nonnull) BugsnagHandledState *handledState;

/**
* A per-event override for the apiKey.
* - Reads default to the BugsnagConfiguration apiKey value unless explicitly set.
* - Writes are not persisted to BugsnagConfiguration.
*/
@property(readwrite, copy, nonnull) NSString *apiKey;

/**
* Property overrides
*/
Expand Down
26 changes: 26 additions & 0 deletions Source/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ + (instancetype)errorDataFromThreads:(NSArray *)threads;
- (instancetype)initWithClass:(NSString *_Nonnull)errorClass message:(NSString *_Nonnull)errorMessage NS_DESIGNATED_INITIALIZER;
@end

@interface BugsnagConfiguration (BugsnagEvent)
+ (BOOL)isValidApiKey:(NSString *_Nullable)apiKey;
@end

@interface BugsnagEvent ()

/**
Expand Down Expand Up @@ -368,6 +372,28 @@ - (void)addMetadata:(NSDictionary *_Nonnull)tabData
self.metadata = allMetadata;
}

@synthesize apiKey = _apiKey;

- (NSString *)apiKey {
if (! _apiKey) {
_apiKey = Bugsnag.configuration.apiKey;
}
return _apiKey;
}


- (void)setApiKey:(NSString *)apiKey {
if ([BugsnagConfiguration isValidApiKey:apiKey]) {
_apiKey = apiKey;
}

// A malformed apiKey should not cause an error: the fallback global value
// in BugsnagConfiguration will do to get the event reported.
else {
bsg_log_warn(@"Attempted to set an invalid Event API key.");
}
}

- (void)addAttribute:(NSString *)attributeName
withValue:(id)value
toTabWithName:(NSString *)tabName {
Expand Down
Loading