Advertisement
serhiy1994

browseai api workflow running code

Jun 3rd, 2025 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1.        public async Task<string> Request()
  2.         {            
  3.             ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  4.            
  5.             var urlA = "https://api.browse.ai/v2/robots/018bb693-18a5-4590-9859-675b5d1eabde/tasks";
  6.             var urlB = "https://api.browse.ai/v2/robots/4a962674-6fbe-4eea-92da-e930600eba05/tasks";
  7.             var apiKey = "the key";
  8.             var targetUrl = "https://www.ema.europa.eu/en/search?search_api_fulltext=risk";
  9.             const int delayMilliseconds = 5000;
  10.             //var responseGetData = new HttpResponseMessage();
  11.             string status = "failed";
  12.             int attempt = 1;
  13.  
  14.             ServicePointManager.Expect100Continue = true;
  15.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  16.             ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
  17.  
  18.             var requestContentA = new StringContent(
  19.                         "{\"inputParameters\":{\"originUrl\":\"" + targetUrl + "\"}}",
  20.                         Encoding.UTF8,
  21.                         "application/json");
  22.  
  23.             var requestRun = CreateRequest(HttpMethod.Post, urlA, apiKey, requestContentA);
  24.  
  25.             try
  26.             {
  27.                 HttpResponseMessage responseRun = await client.SendAsync(requestRun);
  28.                 responseRun.EnsureSuccessStatusCode();
  29.  
  30.                 var jsonObjectRunA = JObject.Parse(await responseRun.Content.ReadAsStringAsync());
  31.                 string id = jsonObjectRunA["result"]["id"].ToString();
  32.  
  33.                 do
  34.                 {
  35.                     await Task.Delay(delayMilliseconds);
  36.                     var requestGetDataA = CreateRequest(HttpMethod.Get, urlA + "/" + id, apiKey);
  37.                     var responseGetDataA = await client.SendAsync(requestGetDataA);
  38.                     responseGetDataA.EnsureSuccessStatusCode();
  39.  
  40.                     var jsonObjectGetDataA = JObject.Parse(await responseGetDataA.Content.ReadAsStringAsync());
  41.                     status = jsonObjectGetDataA["result"]["status"].ToString();
  42.                     if (status != "successful" && jsonObjectGetDataA["result"]["retriedByTaskId"].ToString() != "null" && jsonObjectGetDataA["result"]["retriedByTaskId"].ToString() != "")
  43.                         id = jsonObjectGetDataA["result"]["retriedByTaskId"].ToString();
  44.                     attempt++;
  45.                 } while (status != "successful" && attempt < 10);
  46.  
  47.                 if (status == "successful")
  48.                 {
  49.                     //todo:
  50.                     // get all tasks created by workflow from robot A and check if all them is finished, then
  51.                     // retrieve the json of each task, then
  52.                     // parse each json and get the 'document link' values from it, then
  53.                     // add all the values to one list which to be returned (btw, i can do it by myself)
  54.  
  55.                     var requestGetTasksB = CreateRequest(HttpMethod.Get, urlB, apiKey);
  56.                     var responseGetTasksB = await client.SendAsync(requestGetTasksB);
  57.                     responseGetTasksB.EnsureSuccessStatusCode();
  58.  
  59.                     var jsonObjectGetTasksB = JObject.Parse(await responseRun.Content.ReadAsStringAsync());
  60.  
  61.                     //... what's the next?
  62.                    
  63.                     StringBuilder sb = new StringBuilder();
  64.                     sb.Add ("links list separated by comma");
  65.                     return sb.ToString();
  66.                 }
  67.                 else return "error!";                
  68.             }
  69.             catch (HttpRequestException ex)
  70.             {
  71.                return $"Request error: {ex.Message}";
  72.             }
  73.         }
  74.  
  75.         private HttpRequestMessage CreateRequest(HttpMethod method, string url, string apiKey, HttpContent content = null)
  76.         {
  77.             var request = new HttpRequestMessage(method, url)
  78.             {
  79.                 Headers = { Authorization = new AuthenticationHeaderValue("Bearer", apiKey) },
  80.                 Content = content
  81.             };
  82.             return request;
  83.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement