Bugfixes in script (#3530)
This commit is contained in:
Родитель
25a6f986c8
Коммит
f28cb921f5
|
@ -17,7 +17,8 @@
|
||||||
},
|
},
|
||||||
"iconBrandColor": "#abb8c3",
|
"iconBrandColor": "#abb8c3",
|
||||||
"scriptOperations": [
|
"scriptOperations": [
|
||||||
"Getdetails"
|
"Getdetails",
|
||||||
|
"Create"
|
||||||
],
|
],
|
||||||
"capabilities": [],
|
"capabilities": [],
|
||||||
"policyTemplateInstances": [
|
"policyTemplateInstances": [
|
||||||
|
|
|
@ -4,96 +4,59 @@
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (this.Context.OperationId.StartsWith("Getdetails", StringComparison.OrdinalIgnoreCase))
|
// Use the context to forward/send an HTTP request
|
||||||
|
HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
|
||||||
|
|
||||||
|
// Do the transformation if the response was successful, otherwise return error responses as-is
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
|
// Deserialize the JSON to a JObject
|
||||||
|
var jObject = JObject.Parse(await response.Content.ReadAsStringAsync());
|
||||||
|
|
||||||
// Use the context to forward/send an HTTP request
|
// Get the "Files" object
|
||||||
HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
|
var filesObject = (JObject?)jObject["Files"];
|
||||||
// Do the transformation if the response was successful, otherwise return error responses as-is
|
|
||||||
if (response.IsSuccessStatusCode)
|
// Prepare a list to hold the transformed files
|
||||||
|
var filesList = new List<JObject>();
|
||||||
|
|
||||||
|
if (filesObject is not null)
|
||||||
{
|
{
|
||||||
// Deserialize the JSON to a JObject
|
// Iterate over each property (file) in the "Files" object
|
||||||
var jObject = JObject.Parse(await response.Content.ReadAsStringAsync());
|
foreach (var file in filesObject.Properties())
|
||||||
|
|
||||||
// Get the "Files" object
|
|
||||||
var filesObject = (JObject?)jObject["Files"];
|
|
||||||
|
|
||||||
// Prepare a list to hold the transformed files
|
|
||||||
var filesList = new List<JObject>();
|
|
||||||
|
|
||||||
if (filesObject is not null)
|
|
||||||
{
|
{
|
||||||
// Iterate over each property (file) in the "Files" object
|
// Create a JObject for each file and add it to the list
|
||||||
foreach (var file in filesObject.Properties())
|
var fileEntry = new JObject
|
||||||
{
|
{
|
||||||
// Create a JObject for each file and add it to the list
|
["Links"] = file.Value["Links"],
|
||||||
var fileEntry = new JObject
|
["DisplayName"] = file.Name
|
||||||
{
|
|
||||||
["Links"] = file.Value["Links"],
|
|
||||||
["DisplayName"] = file.Name
|
|
||||||
};
|
|
||||||
filesList.Add(fileEntry);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the new JObject to hold the list of files
|
|
||||||
jObject.Remove("Files");
|
|
||||||
var newFilesObject = new JObject
|
|
||||||
{
|
|
||||||
["Files"] = JArray.FromObject(filesList)
|
|
||||||
};
|
};
|
||||||
jObject.Add("Files", JArray.FromObject(filesList));
|
filesList.Add(fileEntry);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Content = CreateJsonContent(jObject.ToString());
|
// Create the new JObject to hold the list of files
|
||||||
return response;
|
jObject.Remove("Files");
|
||||||
|
var newFilesObject = new JObject
|
||||||
|
{
|
||||||
|
["Files"] = JArray.FromObject(filesList)
|
||||||
|
};
|
||||||
|
jObject.Add("Files", JArray.FromObject(filesList));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (ConnectorException ex)
|
|
||||||
{
|
|
||||||
var response = new HttpResponseMessage(ex.StatusCode);
|
|
||||||
|
|
||||||
if (ex.Message.Contains("ValidationFailure:"))
|
response.Content = CreateJsonContent(jObject.ToString());
|
||||||
{
|
|
||||||
response.Content = CreateJsonContent(ex.JsonMessage());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
response.Content = CreateJsonContent(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
return response;
|
catch (Exception ex)
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ConnectorException : Exception
|
|
||||||
{
|
|
||||||
public HttpStatusCode StatusCode { get; }
|
|
||||||
|
|
||||||
public ConnectorException(HttpStatusCode statusCode, string message, Exception innerException = null) : base(message, innerException)
|
|
||||||
{
|
|
||||||
this.StatusCode = statusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
var error = new StringBuilder($"ConnectorException: Status code={this.StatusCode}, Message='{this.Message}'");
|
|
||||||
var inner = this.InnerException;
|
|
||||||
var level = 0;
|
|
||||||
|
|
||||||
while (inner != null && level < 10)
|
|
||||||
{
|
{
|
||||||
level += 1;
|
var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
|
||||||
error.AppendLine($"Inner exception {level}: {inner.Message}");
|
{
|
||||||
inner = inner.InnerException;
|
Content = CreateJsonContent("error: "+ ex.Message)
|
||||||
|
};
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
error.AppendLine($"Stack trace: {this.StackTrace}");
|
|
||||||
return error.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Загрузка…
Ссылка в новой задаче