To connect to a REST API you can use the Empty Data Source connector. This provides a container that can be used as a dummy target or used for a custom data source when combined with .NET C# code in project automation.
The Schema Column collection for this Data Source.
Since the Empty Data Source does not define any Schema you need to specify the schema columns.
A list of user defined properties that can be accessed via project automation or dynamic columns.
This is a feature where this provider can provide read-only
access to a basic JSON-REST type service.
The Properties collection is used to control the service endpoint and other basic parameters.
Property | Description | Example |
---|---|---|
URL | The url to the JSON service | https://jsonplaceholder.typicode.com/users |
Username | Username to use with BASIC AUTH | username |
Password | Password for Username | password123 |
AuthorizationToken | Set a Bearer HTTP Header for OAuth authentication |
AaaaBBccc444566 |
DataTokenName | The Json Token that returns an Array of items to read | Required if the items array is a child of the json document data |
The Empty Data Source can be loaded with data via project automation code, this is if you need to quickly create a Data source from a service that is not supported in Data Sync.
class ProjectAutomationOverride : Simego.DataSync.Automation.ProjectAutomationShim //Do Not Change This Line
{
// Http Request Helper to call Json Service
private HttpWebRequestHelper helper = new HttpWebRequestHelper() { UseDefaultCredentials = true };
public override void Start()
{
// Set callback function implementation.
DataSourceA.GetDataTableCallback = GetDataTableSource;
}
public DataTableStore GetDataTableSource(DataTableStoreCallbackInfo info)
{
// Convert Datasource Properties to a Dictionary.
var prop = DataSourceA.Properties.ToDictionary(k => k.Name);
// Get a response from the Json Service
var jsonResponse = helper.GetRequestAsJson(prop["url"].Value);
//Enumerate the result json array (use jsonResponse["xxx'] if required)
foreach(var item_json in jsonResponse)
{
// Add the columns to the Data Row
info.Store.Rows.Add(info,
(o, columnName) =>
{
// Get the requested column from the Json Document
var value = item_json[columnName];
//Return the value.
return value != null ? value.ToObject(typeof(object)) : null;
});
}
return info.Store;
}
}
Download Json Demo Project Automation Project
Another way of achieving the same result as by using Project Automation is to use Dynamic Columns. Override the loading of the data table with your own code.
partial class DataSourceRowOverride : Simego.DataSync.DynamicColumns.DataSourceRowInternal //Do Not Change This Line
{
//Every time DS3 calculates a row is runs this BeginRow method.
//You can use this to decide whether to include the row in the results
//Return true to process the row
//Return false to skip the row
public override bool BeginRow()
{
return true; // return false to skip row from results.
}
public override DataTableStore GetDataTable(DataTableStore dt, IDataSourceReader reader)
{
// Create a Callback Info Object to be used later during the data load.
var info = new DataTableStoreCallbackInfo()
{
Mapping = new DataSchemaMapping(reader.SchemaMap, reader.Side),
IncludedColumns = reader.SchemaMap.GetIncludedColumns()
};
// Convert Datasource Properties to a Dictionary to get the Endpoint URL
var prop = GetDataSourceA().Properties.ToDictionary(k => k.Name, v => v.Value, StringComparer.OrdinalIgnoreCase);
// Get a WebRequest Helper to call the Json API
var helper = new HttpWebRequestHelper() { UseDefaultCredentials = true, TraceEnabled = true };
// Get a response from the Json Service
var jsonResponse = helper.GetRequestAsJson(prop["url"]);
//Enumerate the result json array (use jsonResponse["xxx'] if required)
foreach(var item_json in jsonResponse)
{
// Add the columns to the Data Row
dt.Rows.Add(info,
(o, columnName) =>
{
// Get the requested column from the Json Document
var value = item_json[columnName];
//Return the value.
return value != null ? value.ToObject(typeof(object)) : null;
});
}
return dt;
}