public async Task<string> Request()
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var urlA = "https://api.browse.ai/v2/robots/018bb693-18a5-4590-9859-675b5d1eabde/tasks";
var urlB = "https://api.browse.ai/v2/robots/4a962674-6fbe-4eea-92da-e930600eba05/tasks";
var apiKey = "the key";
var targetUrl = "https://www.ema.europa.eu/en/search?search_api_fulltext=risk";
const int delayMilliseconds = 5000;
//var responseGetData = new HttpResponseMessage();
string status = "failed";
int attempt = 1;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
var requestContentA = new StringContent(
"{\"inputParameters\":{\"originUrl\":\"" + targetUrl + "\"}}",
Encoding.UTF8,
"application/json");
var requestRun = CreateRequest(HttpMethod.Post, urlA, apiKey, requestContentA);
try
{
HttpResponseMessage responseRun = await client.SendAsync(requestRun);
responseRun.EnsureSuccessStatusCode();
var jsonObjectRunA = JObject.Parse(await responseRun.Content.ReadAsStringAsync());
string id = jsonObjectRunA["result"]["id"].ToString();
do
{
await Task.Delay(delayMilliseconds);
var requestGetDataA = CreateRequest(HttpMethod.Get, urlA + "/" + id, apiKey);
var responseGetDataA = await client.SendAsync(requestGetDataA);
responseGetDataA.EnsureSuccessStatusCode();
var jsonObjectGetDataA = JObject.Parse(await responseGetDataA.Content.ReadAsStringAsync());
status = jsonObjectGetDataA["result"]["status"].ToString();
if (status != "successful" && jsonObjectGetDataA["result"]["retriedByTaskId"].ToString() != "null" && jsonObjectGetDataA["result"]["retriedByTaskId"].ToString() != "")
id = jsonObjectGetDataA["result"]["retriedByTaskId"].ToString();
attempt++;
} while (status != "successful" && attempt < 10);
if (status == "successful")
{
//todo:
// get all tasks created by workflow from robot A and check if all them is finished, then
// retrieve the json of each task, then
// parse each json and get the 'document link' values from it, then
// add all the values to one list which to be returned (btw, i can do it by myself)
var requestGetTasksB = CreateRequest(HttpMethod.Get, urlB, apiKey);
var responseGetTasksB = await client.SendAsync(requestGetTasksB);
responseGetTasksB.EnsureSuccessStatusCode();
var jsonObjectGetTasksB = JObject.Parse(await responseRun.Content.ReadAsStringAsync());
//... what's the next?
StringBuilder sb = new StringBuilder();
sb.Add ("links list separated by comma");
return sb.ToString();
}
else return "error!";
}
catch (HttpRequestException ex)
{
return $"Request error: {ex.Message}";
}
}
private HttpRequestMessage CreateRequest(HttpMethod method, string url, string apiKey, HttpContent content = null)
{
var request = new HttpRequestMessage(method, url)
{
Headers = { Authorization = new AuthenticationHeaderValue("Bearer", apiKey) },
Content = content
};
return request;
}