Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

Commit

Permalink
Add X-Watson-Learning-Opt-Out in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho4d committed Jul 9, 2016
1 parent 96b4ca0 commit afa3b57
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions watsonsdk/AuthConfiguration.h
Expand Up @@ -20,12 +20,14 @@

@property NSString* basicAuthUsername;
@property NSString* basicAuthPassword;
@property (nonatomic) BOOL xWatsonLearningOptOut;

@property (readonly) NSString *token;
@property (copy, nonatomic) void (^tokenGenerator) (void (^tokenHandler)(NSString *token));

- (void) invalidateToken;
- (void) requestToken: (void(^)(AuthConfiguration *config)) completionHandler;
- (NSDictionary*) createRequestHeaders;
- (NSDictionary*) createRequestHeadersWithXWatsonLearningOptOut;

@end
13 changes: 12 additions & 1 deletion watsonsdk/AuthConfiguration.m
Expand Up @@ -49,7 +49,7 @@ - (void)requestToken:(void (^)(AuthConfiguration *))completionHandler
}
}

- (NSDictionary*) createRequestHeaders {
- (NSMutableDictionary*) _createRequestHeaders {
NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
if (self.tokenGenerator) {
if (self.token) {
Expand All @@ -65,5 +65,16 @@ - (NSDictionary*) createRequestHeaders {
return headers;
}

- (NSDictionary*) createRequestHeaders {
return [self _createRequestHeaders];
}

- (NSDictionary*) createRequestHeadersWithXWatsonLearningOptOut {
NSMutableDictionary *headers = [self _createRequestHeaders];
if (self.xWatsonLearningOptOut) {
[headers setObject:@(YES) forKey:@"X-Watson-Learning-Opt-Out"];
}
return headers;
}

@end
3 changes: 2 additions & 1 deletion watsonsdk/stt/SpeechToText.m
Expand Up @@ -445,7 +445,8 @@ - (void) initializeStreaming {
// connect if we are not connected
if(![self.audioStreamer isWebSocketConnected]) {
[self.config requestToken:^(AuthConfiguration *config) {
[self.audioStreamer connect:(STTConfiguration*)config headers:[config createRequestHeaders]];
[self.audioStreamer connect:(STTConfiguration*)config
headers:[config createRequestHeadersWithXWatsonLearningOptOut]];
}];
}

Expand Down
2 changes: 1 addition & 1 deletion watsonsdk/tts/TextToSpeech.m
Expand Up @@ -340,7 +340,7 @@ - (void) performDataGet:(void (^)(NSData*, NSError*))handler forURL:(NSURL*)url
[defaultConfigObject setURLCache:nil];

[self.config requestToken:^(AuthConfiguration *config) {
NSDictionary* headers = [config createRequestHeaders];
NSDictionary* headers = [config createRequestHeadersWithXWatsonLearningOptOut];
[defaultConfigObject setHTTPAdditionalHeaders:headers];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
Expand Down

0 comments on commit afa3b57

Please sign in to comment.