This commit is contained in:
DoganGunay 2024-06-06 16:59:23 +02:00 коммит произвёл GitHub
Родитель 25a6f986c8
Коммит f28cb921f5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 39 добавлений и 75 удалений

Просмотреть файл

@ -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();
} }
} }