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 all commits
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
6 changes: 6 additions & 0 deletions watsonsdk.xcodeproj/project.pbxproj
Expand Up @@ -23,6 +23,8 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
4FAA404E1D372962001DD221 /* AuthConfigurationInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FAA404D1D37292C001DD221 /* AuthConfigurationInternal.h */; };
4FAA404F1D372963001DD221 /* AuthConfigurationInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FAA404D1D37292C001DD221 /* AuthConfigurationInternal.h */; };
4FC4331C1D0EE82F00ECEFD3 /* AuthConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EFE11E31B71D07D00EF10BF /* AuthConfiguration.m */; };
4FC4331D1D0EE83F00ECEFD3 /* SRDelegateController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BCAD8211CE6BF1200BE3B5F /* SRDelegateController.m */; };
4FC4331E1D0EE84800ECEFD3 /* SpeechUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B47E7571CF21645003E0860 /* SpeechUtility.m */; };
Expand Down Expand Up @@ -196,6 +198,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
4FAA404D1D37292C001DD221 /* AuthConfigurationInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AuthConfigurationInternal.h; sourceTree = "<group>"; };
4FC433141D0EE7AA00ECEFD3 /* WatsonSDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = WatsonSDK.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4FC433161D0EE7AA00ECEFD3 /* WatsonSDK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WatsonSDK.h; sourceTree = "<group>"; };
4FC433181D0EE7AA00ECEFD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -541,6 +544,7 @@
C1416C33194B49520009C49A /* libs */,
9BCAD7C51CE5BC8B00BE3B5F /* websocket */,
7EFE11E21B71D07D00EF10BF /* AuthConfiguration.h */,
4FAA404D1D37292C001DD221 /* AuthConfigurationInternal.h */,
7EFE11E31B71D07D00EF10BF /* AuthConfiguration.m */,
9B47E7561CF21645003E0860 /* SpeechUtility.h */,
9B47E7571CF21645003E0860 /* SpeechUtility.m */,
Expand Down Expand Up @@ -659,6 +663,7 @@
4FC4334F1D0EFC2A00ECEFD3 /* WatsonSDK.h in Headers */,
4FC4334E1D0EFBEA00ECEFD3 /* SRHash.h in Headers */,
4FC4334D1D0EFBE000ECEFD3 /* SRURLUtilities.h in Headers */,
4FAA404E1D372962001DD221 /* AuthConfigurationInternal.h in Headers */,
4FC4334C1D0EFB6300ECEFD3 /* config_types.h in Headers */,
4FC4334B1D0EFB5B00ECEFD3 /* SRRunLoopThread.h in Headers */,
4FC4334A1D0EFB5200ECEFD3 /* SRDelegateController.h in Headers */,
Expand Down Expand Up @@ -691,6 +696,7 @@
9B36693F1CF21A5400806BEE /* WebSocketAudioStreamer.h in Headers */,
9BCAD83C1CE6BF1200BE3B5F /* SRError.h in Headers */,
9BA4758F1B9D354E00D66F1E /* ogg.h in Headers */,
4FAA404F1D372963001DD221 /* AuthConfigurationInternal.h in Headers */,
9BA475891B9D34AA00D66F1E /* OggHelper.h in Headers */,
9BA475901B9D354E00D66F1E /* os_types.h in Headers */,
9BCAD8381CE6BF1200BE3B5F /* SRIOConsumerPool.h in Headers */,
Expand Down
2 changes: 1 addition & 1 deletion watsonsdk/AuthConfiguration.h
Expand Up @@ -20,12 +20,12 @@

@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;

@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
22 changes: 22 additions & 0 deletions watsonsdk/AuthConfigurationInternal.h
@@ -0,0 +1,22 @@
/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

#import "AuthConfiguration.h"

@interface AuthConfiguration (Internal)
- (NSDictionary*) createRequestHeaders;
- (NSDictionary*) createRequestHeadersWithXWatsonLearningOptOut;
@end
4 changes: 3 additions & 1 deletion watsonsdk/stt/SpeechToText.m
Expand Up @@ -15,6 +15,7 @@
**/

#import <SpeechToText.h>
#import "AuthConfigurationInternal.h"

// type defs for block callbacks
#define NUM_BUFFERS 3
Expand Down Expand Up @@ -445,7 +446,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
3 changes: 2 additions & 1 deletion watsonsdk/tts/TextToSpeech.m
Expand Up @@ -15,6 +15,7 @@
**/

#import "TextToSpeech.h"
#import "AuthConfigurationInternal.h"

typedef void (^PlayAudioCallbackBlockType)(NSError*);

Expand Down Expand Up @@ -340,7 +341,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:NO]; // Change to YES 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:NO]; // Change to YES to opt-out learning

// [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