Skip to content

Commit ba34fef

Browse files
committed
feat(discovery): deprecate AddEnvironment that accepts a dictionary, add method that accepts CreateEnvironmentRequest
1 parent 75bd365 commit ba34fef

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

Scripts/Services/Discovery/v1/Discovery.cs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,26 @@ private void OnGetEnvironmentsResponse(RESTConnector.Request req, RESTConnector.
244244
/// <param name="size">The size of the environment to be created. See <a href="https://www.ibm.com/watson/services/discovery/#pricing-block">pricing.</a></param>
245245
/// <param name="customData">Optional custom data.</param>
246246
/// <returns>True if the call succeeds, false if the call is unsuccessful.</returns>
247-
public bool AddEnvironment(SuccessCallback<Environment> successCallback, FailCallback failCallback, string name = default(string), string description = default(string), int size = 0, Dictionary<string, object> customData = null)
247+
public bool AddEnvironment(SuccessCallback<Environment> successCallback, FailCallback failCallback, string name = default(string), string description = default(string), SizeEnum? size = null, Dictionary<string, object> customData = null)
248248
{
249-
if (successCallback == null)
250-
throw new ArgumentNullException("successCallback");
251-
if (failCallback == null)
252-
throw new ArgumentNullException("failCallback");
249+
CreateEnvironmentRequest createEnvironmentRequest = new CreateEnvironmentRequest();
250+
if(!string.IsNullOrEmpty(name))
251+
{
252+
createEnvironmentRequest.Name = name;
253+
}
253254

254-
Dictionary<string, object> addEnvironmentData = new Dictionary<string, object>();
255-
addEnvironmentData["name"] = name;
256-
addEnvironmentData["description"] = description;
257-
addEnvironmentData["size"] = size;
255+
if(!string.IsNullOrEmpty(description))
256+
{
257+
createEnvironmentRequest.Description = description;
258+
}
259+
260+
if(size != null)
261+
{
262+
createEnvironmentRequest.Size = size;
263+
}
264+
258265

259-
return AddEnvironment(successCallback, failCallback, addEnvironmentData, customData);
266+
return AddEnvironment(successCallback, failCallback, createEnvironmentRequest, customData);
260267
}
261268

262269
/// <summary>
@@ -268,6 +275,7 @@ private void OnGetEnvironmentsResponse(RESTConnector.Request req, RESTConnector.
268275
/// <param name="addEnvironmentData">The AddEnvironmentData.</param>
269276
/// <param name="customData">Optional custom data.</param>
270277
/// <returns>True if the call succeeds, false if the call is unsuccessful.</returns>
278+
[Obsolete("Use AddEnvironment with CreateEnvironmentRequest instead.")]
271279
public bool AddEnvironment(SuccessCallback<Environment> successCallback, FailCallback failCallback, Dictionary<string, object> addEnvironmentData, Dictionary<string, object> customData = null)
272280
{
273281
if (successCallback == null)
@@ -300,6 +308,50 @@ public bool AddEnvironment(SuccessCallback<Environment> successCallback, FailCal
300308
return connector.Send(req);
301309
}
302310

311+
/// <summary>
312+
/// Creates a new environment. You can only create one environment per service instance.An attempt to create another environment
313+
/// will result in an error. The size of the new environment can be controlled by specifying the size parameter.
314+
/// </summary>
315+
/// <param name="successCallback">The success callback.</param>
316+
/// <param name="failCallback">The fail callback.</param>
317+
/// <param name="createEnvironmentRequest">An object that defines an environment name and optional description. The fields in this object are not approved for personal information and cannot be deleted based on customer ID.</param>
318+
/// <param name="customData">Optional custom data.</param>
319+
/// <returns>True if the call succeeds, false if the call is unsuccessful.</returns>
320+
public bool AddEnvironment(SuccessCallback<Environment> successCallback, FailCallback failCallback, CreateEnvironmentRequest createEnvironmentRequest, Dictionary<string, object> customData = null)
321+
{
322+
if (successCallback == null)
323+
throw new ArgumentNullException("successCallback");
324+
if (failCallback == null)
325+
throw new ArgumentNullException("failCallback");
326+
327+
AddEnvironmentRequest req = new AddEnvironmentRequest();
328+
req.SuccessCallback = successCallback;
329+
req.FailCallback = failCallback;
330+
req.CustomData = customData == null ? new Dictionary<string, object>() : customData;
331+
if (req.CustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
332+
{
333+
foreach (KeyValuePair<string, string> kvp in req.CustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
334+
{
335+
req.Headers.Add(kvp.Key, kvp.Value);
336+
}
337+
}
338+
req.Parameters["version"] = VersionDate;
339+
req.OnResponse = OnAddEnvironmentResponse;
340+
req.Headers["Content-Type"] = "application/json";
341+
req.Headers["Accept"] = "application/json";
342+
343+
fsData data = null;
344+
_serializer.TrySerialize(createEnvironmentRequest, out data);
345+
string json = data.ToString().Replace('\"', '"');
346+
req.Send = Encoding.UTF8.GetBytes(json);
347+
348+
RESTConnector connector = RESTConnector.GetConnector(Credentials, Environments);
349+
if (connector == null)
350+
return false;
351+
352+
return connector.Send(req);
353+
}
354+
303355
private class AddEnvironmentRequest : RESTConnector.Request
304356
{
305357
/// <summary>

0 commit comments

Comments
 (0)