Check asset warnings
One of the key features of Open Assets is that it gives you insights in to potential issues with assets of interest or those under your control.
There maybe scenarios where you will want to sync these warnings or an aggregation of the warnings to you own internal systems, such as a contract system. To sync the warnings you will need to periodically request updated Warningsopen in new window.
The Get Warningsopen in new window endpoint returns warnings descending by the last updated timestamp. This allows you to page through the results until you receive a warning with a timestamp smaller than the timestamp you last carried the period check for updates.
Note
It is recommended that warnings are synced with external systems on a daily or weekly basis.
Syncing on a time period less than 24 hours will result in very few updates and only unnecessarily increase load on the Open Assets system.
Example
In this example we are calling the Get Warningsopen in new window endpoint and asking for the most recent 50. If the Results
includes a UpdatedTimestampUtc
that is less than or equal to the mostRecentTimestampUtc
we'll stop there because we have already processed these warnings in a previous run. Equally if the results contains less that the amount we asked to take
we can stop because we must be at the end of the list.
It is important to save the mostRecentTimestampUtc
to make sure that we only process new warnings we haven't seen yet. Full example available in C# (.NET 6) on GitHubopen in new window.
Note
The example below is using the .NET 6 minimal API.
const string apiKey = "<Enter API Key Here>";
const string api = "http://api-sandbox.open-assets.co.uk";
var client = new HttpClient();
client.BaseAddress = new Uri(api);
client.DefaultRequestHeaders.Add("x-api-key", apiKey);
DateTimeOffset? maxUpdatedTimestampUtc = null;
var mostRecentTimestampUtc = await GetMostRecentTimestampUtc();
var skip = 0;
var take = 50;
while (true)
{
var response = JsonConvert.DeserializeObject<WarningSearchResponse>(
await client.GetStringAsync(quot;/warnings?take={take}&skip={skip}")
);
// todo - process warning updates
if (!response!.Results.Any())
{
break;
}
maxUpdatedTimestampUtc ??= response.Results.Max(x => x.UpdatedTimestampUtc);
if (mostRecentTimestampUtc > response.Results.Min(x => x.UpdatedTimestampUtc) || take > response.Results.Count)
{
break;
}
skip += take;
}
if (maxUpdatedTimestampUtc.HasValue)
{
await UpdateLastRunTimestampUtc(maxUpdatedTimestampUtc.Value);
}
Task UpdateLastRunTimestampUtc(DateTimeOffset mostRecentTimestampUtc)
{
// todo - persist the most recent warning timestamp for use on the next run.
return Task.CompletedTask;
}
Task<DateTimeOffset> GetMostRecentTimestampUtc()
{
// todo - replace with logic to get the last run timestamp from storage, such as a SQL Db.
return Task.FromResult(DateTimeOffset.MinValue);
}