Skip to content

Commit ad82c66

Browse files
committed
Revise handwritten visrec methods
1 parent 7c302c0 commit ad82c66

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

src/IBM.WatsonDeveloperCloud.VisualRecognition.v3/VisualRecognitionServiceExtension.cs

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,28 @@ public Classifier CreateClassifier(CreateClassifier createClassifier, Dictionary
8282
formData.Add(negativeExamplesContent, "negative_examples", "negative_examples.zip");
8383
}
8484

85-
result = this.Client.PostAsync($"{this.Endpoint}/v3/classifiers")
86-
.WithArgument("api_key", ApiKey)
87-
.WithArgument("version", VersionDate)
88-
.WithBodyContent(formData)
89-
.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"))
90-
.As<Classifier>()
91-
.Result;
85+
IClient client;
86+
if (_tokenManager == null)
87+
{
88+
client = this.Client;
89+
}
90+
else
91+
{
92+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
93+
}
94+
var restRequest = client.PostAsync($"{this.Endpoint}/v3/classifiers");
95+
96+
restRequest.WithArgument("version", VersionDate);
97+
if(!string.IsNullOrEmpty(ApiKey))
98+
restRequest.WithArgument("api_key", ApiKey);
99+
restRequest.WithBodyContent(formData);
100+
if (customData != null)
101+
restRequest.WithCustomData(customData);
102+
restRequest.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"));
103+
result = restRequest.As<Classifier>().Result;
104+
if (result == null)
105+
result = new Classifier();
106+
result.CustomData = restRequest.CustomData;
92107
}
93108
catch (AggregateException ae)
94109
{
@@ -141,13 +156,28 @@ public Classifier UpdateClassifier(UpdateClassifier updateClassifier, Dictionary
141156
formData.Add(negativeExamplesContent, "negative_examples", "negative_examples.zip");
142157
}
143158

144-
result = this.Client.PostAsync($"{this.Endpoint}/v3/classifiers/{updateClassifier.ClassifierId}")
145-
.WithArgument("api_key", ApiKey)
146-
.WithArgument("version", VersionDate)
147-
.WithBodyContent(formData)
148-
.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"))
149-
.As<Classifier>()
150-
.Result;
159+
IClient client;
160+
if (_tokenManager == null)
161+
{
162+
client = this.Client;
163+
}
164+
else
165+
{
166+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
167+
}
168+
var restRequest = client.PostAsync($"{this.Endpoint}/v3/classifiers/{updateClassifier.ClassifierId}");
169+
170+
restRequest.WithArgument("version", VersionDate);
171+
if (!string.IsNullOrEmpty(ApiKey))
172+
restRequest.WithArgument("api_key", ApiKey);
173+
restRequest.WithBodyContent(formData);
174+
if (customData != null)
175+
restRequest.WithCustomData(customData);
176+
restRequest.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"));
177+
result = restRequest.As<Classifier>().Result;
178+
if (result == null)
179+
result = new Classifier();
180+
result.CustomData = restRequest.CustomData;
151181
}
152182
catch (AggregateException ae)
153183
{
@@ -171,12 +201,25 @@ public Task<Stream> GetCoreMlModel(string classifierId, Dictionary<string, objec
171201

172202
try
173203
{
174-
var request = this.Client.GetAsync($"{this.Endpoint}/v3/classifiers/{classifierId}/core_ml_model");
175-
request.WithArgument("api_key", ApiKey);
176-
request.WithArgument("version", VersionDate);
177-
request.WithArgument("classifier_id", classifierId);
178-
request.WithFormatter(MediaTypeHeaderValue.Parse("application/octet-stream"));
179-
result = request.AsStream();
204+
IClient client;
205+
if (_tokenManager == null)
206+
{
207+
client = this.Client;
208+
}
209+
else
210+
{
211+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
212+
}
213+
var restRequest = client.PostAsync($"{this.Endpoint}/v3/classifiers/{classifierId}/core_ml_model");
214+
215+
restRequest.WithArgument("version", VersionDate);
216+
if (!string.IsNullOrEmpty(ApiKey))
217+
restRequest.WithArgument("api_key", ApiKey);
218+
restRequest.WithArgument("classifier_id", classifierId);
219+
if (customData != null)
220+
restRequest.WithCustomData(customData);
221+
restRequest.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"));
222+
result = restRequest.AsStream();
180223
}
181224
catch (AggregateException ae)
182225
{

test/IBM.WatsonDeveloperCloud.VisualRecognition.v3.IntegrationTests/VisualRecognitionServiceRCIntegrationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public void Setup()
8282
VcapCredentials vcapCredentials = JsonConvert.DeserializeObject<VcapCredentials>(credentials);
8383
var vcapServices = JObject.Parse(credentials);
8484

85-
Credential credential = vcapCredentials.GetCredentialByname("visual-recognition-sdk")[0].Credentials;
85+
Credential credential = vcapCredentials.GetCredentialByname("visual-recognition-iam-sdk")[0].Credentials;
8686
_endpoint = credential.Url;
87-
_apikey = credential.ApiKey;
87+
_apikey = credential.IamApikey;
8888
}
8989
#endregion
9090

@@ -199,7 +199,7 @@ public void ListClassifiers_RC_Success()
199199
}
200200

201201
#region Custom
202-
//[TestMethod]
202+
[TestMethod]
203203
public void TestClassifiers_RC_Success()
204204
{
205205
Classifier createClassifierResult = null;

0 commit comments

Comments
 (0)