APIHost¶
APIHost hosts REST endpoints inside an i-Con process and hands each request to a message queue for background processing.
Request flow¶
Host settings¶
| Property | Description | Example |
|---|---|---|
Port |
The port the host listens on | 5555 |
TransferProtocol |
HTTP or HTTPS |
HTTPS |
WhitelistedIPs |
Addresses allowed to call the host, separated by \| |
10.0.0.15\|10.0.0.16 |
APIServices |
The collection of services (routes) the host exposes. See below. |
An empty allowlist allows everyone
If WhitelistedIPs is blank, any address can call the host. Always set it for production endpoints unless access is controlled elsewhere.
Service settings¶
Each entry in APIServices defines one route:
| Property | Description |
|---|---|
Name |
The controller or route name |
HTTPMethod |
The verbs the route accepts: GET, POST, PUT or DELETE |
RequestQueue |
The queue requests are written to, for example .\private$\InboundRequestQ |
ResponseQueue |
The queue results are read from |
TimeOut |
How many seconds to wait for a response before failing the request |
UseRouteParameter, RouteParameterValue |
Match parameterised URLs, such as /api/records/{id} |
ControllerRequestCode |
Optional C# run before the request is queued |
ControllerResponseCode |
Optional C# run on the result before it is returned |
Example: read a JSON request¶
This ControllerRequestCode reads the body, extracts three values and passes them to the queue as one delimited string in value.
C#
try
{
string rawBody = Request.Content.ReadAsStringAsync().Result;
JObject json = JObject.Parse(rawBody);
string token = json["token"]?.ToString() ?? string.Empty;
string action = json["action"]?.ToString() ?? string.Empty;
string customerId = json["data"]?["customerId"]?.ToString() ?? string.Empty;
// Message placed on the request queue
value = string.Format("{0}|{1}|{2}", token, action, customerId);
}
catch (Exception ex)
{
value = "ERR|" + ex.Message;
}
Example: build a JSON response¶
This ControllerResponseCode splits the worker's result, available in answer, into a JSON object.
C#
string[] parts = answer.ToString().Split('|');
var response = new
{
status = parts[0] == "OK" ? "Success" : "Failed",
message = parts.Length > 1 ? parts[1] : string.Empty,
timestamp = DateTime.UtcNow.ToString("o"),
data = new
{
transactionId = parts.Length > 2 ? parts[2] : null,
recordCount = parts.Length > 3 ? int.Parse(parts[3]) : 0
}
};