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

Exposes x-watson-learning-opt-out request query parameter #35

Merged
merged 5 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep only one "createRequestHeaders" method because it is only SDK-internal use, and change value through config. If the developers want to change the SDK code, they could do it, we just keep the code simple.

Thanks.

Copy link
Collaborator Author

@nacho4d nacho4d Jul 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. What do you think about having a private header "AuthConfiguration+Internal.h" ? It should look something like this:

// AuthConfiguration+Internal.h
#import AuthConfiguration.h
@interface AuthConfiguration(Internal)
- (NSDictionary*) createRequestHeaders;
- (NSDictionary*) createRequestHeadersWithXWatsonLearningOptOut;
... other internal methods to be used only by the sdk...
@end

This way we can separate internal and internal stuff. In this case createRequestHeadersWithXWatsonLearningOptOut is used in STT and TTS request. IMO is better to have it as a method instead of doing it separately twice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good idea to separate them, changing configuration is good too, either way is OK.


@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:@"true" 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
1 change: 1 addition & 0 deletions watsonsdktest-objective-c/STTViewController.m
Expand Up @@ -46,6 +46,7 @@ - (void)viewDidLoad
[confSTT setBasicAuthPassword:credentials[@"STTPassword"]];
[confSTT setAudioCodec:WATSONSDK_AUDIO_CODEC_TYPE_OPUS];
[confSTT setModelName:WATSONSDK_DEFAULT_STT_MODEL];
[confSTT setXWatsonLearningOptOut:false]; // Change to true to opt-out learning

// [conf setTokenGenerator:^(void (^tokenHandler)(NSString *token)){
// NSURL *url = [[NSURL alloc] initWithString:@"https://<token-factory-url>"];
Expand Down
1 change: 1 addition & 0 deletions watsonsdktest-objective-c/TTSViewController.m
Expand Up @@ -41,6 +41,7 @@ - (void)viewDidLoad {
[confTTS setBasicAuthUsername:credentials[@"TTSUsername"]];
[confTTS setBasicAuthPassword:credentials[@"TTSPassword"]];
[confTTS setAudioCodec:WATSONSDK_TTS_AUDIO_CODEC_TYPE_OPUS];
[confTTS setXWatsonLearningOptOut:false]; // Change to true to opt-out learning
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use YES/NO instead of true/false in Objective-C. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe, me too. I will fix this


// [confTTS setTokenGenerator:^(void (^tokenHandler)(NSString *token)){
// NSURL *url = [[NSURL alloc] initWithString:@"https://my-token-factory/token"];
Expand Down
1 change: 1 addition & 0 deletions watsonsdktest-swift/SwiftSTTViewController.swift
Expand Up @@ -42,6 +42,7 @@ class SwiftSTTViewController: UIViewController, UITextFieldDelegate, UIPickerVie
confSTT.basicAuthPassword = credentials!["STTPassword"] as! String
confSTT.audioCodec = WATSONSDK_AUDIO_CODEC_TYPE_OPUS
confSTT.modelName = WATSONSDK_DEFAULT_STT_MODEL
confSTT.xWatsonLearningOptOut = false // Change to true to opt-out

self.sttInstance = SpeechToText(config: confSTT)
self.sttInstance?.listModels({ (jsonDict, error) -> Void in
Expand Down
1 change: 1 addition & 0 deletions watsonsdktest-swift/SwiftTTSViewController.swift
Expand Up @@ -43,6 +43,7 @@ class SwiftTTSViewController: UIViewController, UITextFieldDelegate, UIPickerVie
confTTS.basicAuthPassword = credentials?["TTSPassword"] as! String
confTTS.audioCodec = WATSONSDK_TTS_AUDIO_CODEC_TYPE_OPUS
confTTS.voiceName = WATSONSDK_DEFAULT_TTS_VOICE
confTTS.xWatsonLearningOptOut = false // Change to true to opt-out

self.ttsInstance = TextToSpeech(config: confTTS)
self.ttsInstance?.listVoices({ (jsonDict, error) -> Void in
Expand Down