1
+ /**
2
+ * Copyright 2015 IBM Corp. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ using FullSerializer ;
19
+ using IBM . Watson . DeveloperCloud . Connection ;
20
+ using IBM . Watson . DeveloperCloud . Logging ;
21
+ using IBM . Watson . DeveloperCloud . Utilities ;
22
+ using System ;
23
+ using System . IO ;
24
+ using System . Text ;
25
+ using UnityEngine ;
26
+
27
+ namespace IBM . Watson . DeveloperCloud . Services . PersonalityInsights . v3
28
+ {
29
+ /// <summary>
30
+ /// This class wraps the Personality Insights service.
31
+ /// <a href="http://www.ibm.com/watson/developercloud/personality-insights.html">Personality Insights Service</a>
32
+ /// </summary>
33
+ public class PersonalityInsights : IWatsonService
34
+ {
35
+ #region Private Data
36
+ private const string SERVICE_ID = "PersonalityInsightsV3" ;
37
+ private static fsSerializer sm_Serializer = new fsSerializer ( ) ;
38
+ #endregion
39
+
40
+ #region Profile
41
+ private const string SERVICE_GET_PROFILE = "/v3/profile" ;
42
+
43
+ public delegate void OnGetProfile ( Profile profile , string data ) ;
44
+
45
+ public bool GetProfile ( OnGetProfile callback , string source ,
46
+ string contentType = ContentType . TEXT_PLAIN ,
47
+ string contentLanguage = Language . ENGLISH ,
48
+ string accept = ContentType . APPLICATION_JSON ,
49
+ string acceptLanguage = Language . ENGLISH ,
50
+ bool raw_scores = false ,
51
+ bool csv_headers = false ,
52
+ bool consumption_preferences = false ,
53
+ string version = PersonalityInsightsVersion . Version ,
54
+ string data = default ( string ) )
55
+ {
56
+ if ( callback == null )
57
+ throw new ArgumentNullException ( "callback" ) ;
58
+ if ( string . IsNullOrEmpty ( source ) )
59
+ throw new ArgumentNullException ( "A JSON or Text source is required for GetProfile!" ) ;
60
+
61
+ RESTConnector connector = RESTConnector . GetConnector ( SERVICE_ID , SERVICE_GET_PROFILE ) ;
62
+ if ( connector == null )
63
+ return false ;
64
+
65
+ GetProfileRequest req = new GetProfileRequest ( ) ;
66
+ req . Source = source ;
67
+ req . Callback = callback ;
68
+ req . Data = data ;
69
+ req . OnResponse = GetProfileResponse ;
70
+
71
+ req . Parameters [ "raw_scores" ] = raw_scores . ToString ( ) ;
72
+ req . Parameters [ "csv_headers" ] = csv_headers . ToString ( ) ;
73
+ req . Parameters [ "consumption_preferences" ] = consumption_preferences . ToString ( ) ;
74
+ req . Parameters [ "version" ] = version ;
75
+
76
+ req . Headers [ "Content-Type" ] = contentType ;
77
+ req . Headers [ "Content-Language" ] = contentLanguage ;
78
+ req . Headers [ "Accept" ] = accept ;
79
+ req . Headers [ "Accept-Language" ] = acceptLanguage ;
80
+
81
+ if ( source . StartsWith ( Application . dataPath ) )
82
+ {
83
+ string jsonData = default ( string ) ;
84
+ jsonData = File . ReadAllText ( source ) ;
85
+ req . Send = System . Text . Encoding . UTF8 . GetBytes ( jsonData ) ;
86
+ }
87
+ else
88
+ {
89
+ req . Send = System . Text . Encoding . UTF8 . GetBytes ( source ) ;
90
+ }
91
+
92
+ return connector . Send ( req ) ;
93
+ }
94
+
95
+ /// <summary>
96
+ /// Get profile request.
97
+ /// </summary>
98
+ public class GetProfileRequest : RESTConnector . Request
99
+ {
100
+ /// <summary>
101
+ /// The source string.
102
+ /// </summary>
103
+ public string Source { get ; set ; }
104
+ /// <summary>
105
+ /// Custom data.
106
+ /// </summary>
107
+ public string Data { get ; set ; }
108
+ /// <summary>
109
+ /// The callback.
110
+ /// </summary>
111
+ public OnGetProfile Callback { get ; set ; }
112
+ }
113
+
114
+ private void GetProfileResponse ( RESTConnector . Request req , RESTConnector . Response resp )
115
+ {
116
+ Profile response = new Profile ( ) ;
117
+ if ( resp . Success )
118
+ {
119
+ try
120
+ {
121
+ fsData data = null ;
122
+ fsResult r = fsJsonParser . Parse ( Encoding . UTF8 . GetString ( resp . Data ) , out data ) ;
123
+ if ( ! r . Succeeded )
124
+ throw new WatsonException ( r . FormattedMessages ) ;
125
+
126
+ object obj = response ;
127
+ r = sm_Serializer . TryDeserialize ( data , obj . GetType ( ) , ref obj ) ;
128
+ if ( ! r . Succeeded )
129
+ throw new WatsonException ( r . FormattedMessages ) ;
130
+ }
131
+ catch ( Exception e )
132
+ {
133
+ Log . Error ( "PersonalityInsights" , "GetProfileResponse Exception: {0}" , e . ToString ( ) ) ;
134
+ resp . Success = false ;
135
+ }
136
+ }
137
+
138
+ if ( ( ( GetProfileRequest ) req ) . Callback != null )
139
+ ( ( GetProfileRequest ) req ) . Callback ( resp . Success ? response : null , ( ( GetProfileRequest ) req ) . Data ) ;
140
+ }
141
+ #endregion
142
+
143
+ #region IWatsonService implementation
144
+ public string GetServiceID ( )
145
+ {
146
+ return SERVICE_ID ;
147
+ }
148
+
149
+ public void GetServiceStatus ( ServiceStatus callback )
150
+ {
151
+ if ( Utilities . Config . Instance . FindCredentials ( SERVICE_ID ) != null )
152
+ new CheckServiceStatus ( this , callback ) ;
153
+ else
154
+ callback ( SERVICE_ID , false ) ;
155
+ }
156
+
157
+ private class CheckServiceStatus
158
+ {
159
+ private PersonalityInsights m_Service = null ;
160
+ private ServiceStatus m_Callback = null ;
161
+
162
+ public CheckServiceStatus ( PersonalityInsights service , ServiceStatus callback )
163
+ {
164
+ m_Service = service ;
165
+ m_Callback = callback ;
166
+ string dataPath = Application . dataPath + "/Watson/Examples/ServiceExamples/TestData/personalityInsights.json" ;
167
+ if ( ! m_Service . GetProfile ( OnGetProfile , dataPath , ContentType . TEXT_PLAIN , Language . ENGLISH ) )
168
+ m_Callback ( SERVICE_ID , false ) ;
169
+ }
170
+
171
+ private void OnGetProfile ( Profile resp , string data )
172
+ {
173
+ if ( m_Callback != null )
174
+ m_Callback ( SERVICE_ID , resp != null ) ;
175
+ }
176
+ }
177
+ #endregion
178
+ }
179
+ }
0 commit comments