Calling an endpoint

There are three ways of calling a custom endpoint in Curiosity Workspace: one from within the user interface, one from within a data connector, and finally one for external callers.

From within the user interface

The first way is to use the endpoint API that is provided as part of the Curiosity front-end package, which takes care of configuring the HTTP request, data serialization, authorization and the asynchronous endpoint processing logic.

Existing endpoints can be invoked from front-end code using the following syntax:

// Calls an endpoint without any body:
var response = await Mosaik.API.Endpoints.CallAsync<string>("hello");

// Calls an endpoint with an object as body:

public class RequestData 
{
    public DateTimeOffset From { get; set; }
    public DateTimeOffset To   { get; set; }
}

var requestData = new RequestData() 
{ 
    From = DateTimeOffset.UtcNow.AddDays(-30), 
    To   = DateTimeOffset.UtcNow,
};

var response = await Mosaik.API.Endpoints.CallAsync<string>("hello", requestData);

From data connectors

The data connector package provides a helper class that wraps the logic of calling an endpoint for you. You can use this class as following:

var endpointClient = new EndpointsClient("url-to-server", "endpoint-token");

var stringResponse = await endpointClient.CallAsync("hello");
var objectResponse = await endpointClient.CallAsync<ResponseType>("hello");

//You can also call the endpoint with some content:

var requestData = new RequestData() 
{ 
    From = DateTimeOffset.UtcNow.AddDays(-30), 
    To   = DateTimeOffset.UtcNow,
};

await endpointClient.CallAsync<RequestData>("hello", requestData);
var response = await endpointClient.CallAsync<RequestData, string>("hello", requestData);

From outside the workspace

The final way is to use any HTTP client or tool that can send requests over HTTPS, such as curl or Postman. In this case you'll need to generate a valid endpoint token, and you need to provide it as a Bearer token in the Authentication header. For example, you can call an endpoint using curl:

# POST to: http://localhost:5001/api/cce/token/run/hello

curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" --data "${BODY}" <http://localhost:5001/api/cce/token/run/hello>

When calling an endpoint, you must check the Status Code of the response, as endpoints run asynchronously by default, and will return 202 Accepted if the call was received but the result is not yet available. If you receive a 202 response, you should call again the same endpoint with the exact same request (i.e. same body) until you receive a 200 (in case of success) or 500 (in case of failure) response.

When you call an endpoint from the front-end or the connector, the helper methods will take care of this retry logic for you

Last updated