Skip to content

Commit

Permalink
Bugfixes in script (#3530)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoganGunay committed Jun 6, 2024
1 parent 25a6f98 commit f28cb92
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 74 deletions.
3 changes: 2 additions & 1 deletion certified-connectors/SignHost/apiProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"iconBrandColor": "#abb8c3",
"scriptOperations": [
"Getdetails"
"Getdetails",
"Create"
],
"capabilities": [],
"policyTemplateInstances": [
Expand Down
109 changes: 36 additions & 73 deletions certified-connectors/SignHost/script.csx
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,59 @@
{
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
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());

// Get the "Files" object
var filesObject = (JObject?)jObject["Files"];
// Get the "Files" object
var filesObject = (JObject?)jObject["Files"];

// Prepare a list to hold the transformed files
var filesList = new List<JObject>();
// Prepare a list to hold the transformed files
var filesList = new List<JObject>();

if (filesObject is not null)
if (filesObject is not null)
{
// Iterate over each property (file) in the "Files" object
foreach (var file in filesObject.Properties())
{
// Iterate over each property (file) in the "Files" object
foreach (var file in filesObject.Properties())
// Create a JObject for each file and add it to the list
var fileEntry = new JObject
{
// Create a JObject for each file and add it to the list
var fileEntry = new JObject
{
["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)
["Links"] = file.Value["Links"],
["DisplayName"] = file.Name
};
jObject.Add("Files", JArray.FromObject(filesList));

filesList.Add(fileEntry);
}

response.Content = CreateJsonContent(jObject.ToString());
return response;
// 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));

}
}
}
catch (ConnectorException ex)
{
var response = new HttpResponseMessage(ex.StatusCode);

if (ex.Message.Contains("ValidationFailure:"))
{
response.Content = CreateJsonContent(ex.JsonMessage());
}
else
{
response.Content = CreateJsonContent(ex.Message);
}
response.Content = CreateJsonContent(jObject.ToString());

}

return response;
}
return response;

}
}

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)
catch (Exception ex)
{
level += 1;
error.AppendLine($"Inner exception {level}: {inner.Message}");
inner = inner.InnerException;
var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = CreateJsonContent("error: "+ ex.Message)
};
return response;
}

error.AppendLine($"Stack trace: {this.StackTrace}");
return error.ToString();
}
}

0 comments on commit f28cb92

Please sign in to comment.