stictt

Api paste

Jun 13th, 2021
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TradeBeaver.ApiLoader.Models;
  7. using System.Net.Http;
  8. using Newtonsoft.Json.Linq;
  9.  
  10. using System.Net;
  11. using System.Runtime.CompilerServices;
  12. namespace TradeBeaver.ApiLoader
  13. {
  14.     public class Markets
  15.     {
  16.         string ApiBuild(int regionID, int type_id, int page)
  17.         {
  18.             return "https:/"+ $"/esi.evetech.net/latest/markets/{regionID}/orders/?datasource=tranquility&order_type=all&page={page}&type_id={type_id}";
  19.         }
  20.  
  21.         private List<DontLock> BuildModel(List<StoreAccessSheet> storeAccessSheets)
  22.         {
  23.             List<DontLock> result = new List<DontLock>();
  24.             int count = 0;
  25.             foreach (var item in storeAccessSheets)
  26.             {
  27.                 result.Add(new DontLock() { Count = count,Sheet = item });
  28.                 count++;
  29.             }
  30.             return result;
  31.         }
  32.  
  33.  
  34.         public List<Order> GetOrder(List<StoreAccessSheet> storeAccessSheets)
  35.         {
  36.             Pages<Order>[] result = new Pages<Order>[storeAccessSheets.Count];
  37.            
  38.             Parallel.ForEach(BuildModel(storeAccessSheets), sheetDontLock =>
  39.             {
  40.                 int count = CountPage(ApiBuild(sheetDontLock.Sheet.regionID, sheetDontLock.Sheet.type_id, 1));
  41.                 result[sheetDontLock.Count] = RequestSheet(sheetDontLock.Sheet, count);
  42.             });
  43.             WaitTask(result.SelectMany(x=>x.TaskApi).ToList());
  44.             UpdateErrorPages(result);
  45.             return result.SelectMany(x => x.RequestPages).SelectMany(x=>x.Page).ToList();
  46.         }
  47.  
  48.         private Pages<Order> RequestSheet(StoreAccessSheet sheet, int count)
  49.         {
  50.             Pages<Order> pages = new Pages<Order>(count, sheet);
  51.             List<ConfiguredTaskAwaitable> taskApi = new List<ConfiguredTaskAwaitable>();
  52.             for (int i = 0; i < count; i++)
  53.             {
  54.                 taskApi.Add(RequestAPIAsync(pages, i).ConfigureAwait(false));
  55.             }
  56.             pages.TaskApi = taskApi;
  57.             return pages;
  58.         }
  59.  
  60.         private void UpdateErrorPages(Pages<Order>[] Arraypages)
  61.         {
  62.             if (!Arraypages.SelectMany(x=>x.RequestPages).Any(x => x.Page == null)) { return; }
  63.             int errorCount = 0;
  64.             Parallel.ForEach(Arraypages, pages =>
  65.             {
  66.                 while (pages.RequestPages.Any(x => x.Page == null))
  67.                 {
  68.                     List<ConfiguredTaskAwaitable> taskApi = new List<ConfiguredTaskAwaitable>();
  69.                     foreach (var item in pages.RequestPages.Where(x => x.Page == null))
  70.                     {
  71.                         taskApi.Add(RequestAPIAsync(pages, item.Number).ConfigureAwait(false));
  72.                     }
  73.                     WaitTask(taskApi);
  74.                     errorCount++;
  75.                     if (errorCount > 10)
  76.                     {
  77.                         throw new Exception("Ошибка соединения с интернетом");
  78.                     }
  79.                 }
  80.             });
  81.            
  82.         }
  83.  
  84.         private void WaitTask(List<ConfiguredTaskAwaitable> taskApi)
  85.         {
  86.             while (!taskApi.All(x=>x.GetAwaiter().IsCompleted) )
  87.             {
  88.                 Task.Delay(5).Wait();
  89.             }
  90.         }
  91.  
  92.  
  93.         private async Task RequestAPIAsync(Pages<Order> pages, int count)
  94.         {
  95.             try
  96.             {
  97.                 pages.RequestPages[count ] = new PageElement<Order>( count,
  98.                                             await ApiConect.GetApiAsync<List<Order>>(ApiBuild(pages.sheet.regionID, pages.sheet.type_id, count+1)));
  99.             }
  100.             catch
  101.             {
  102.                 pages.RequestPages[count] = new PageElement<Order>(count,null);
  103.             }
  104.         }
  105.  
  106.         private int CountPage(string api)
  107.         {
  108.             int value = 0;
  109.             int countExeption = 0;
  110.             while (true)
  111.             {
  112.                 try
  113.                 {
  114.                     WebRequest request = WebRequest.Create(api);
  115.                     WebResponse response = request.GetResponse();
  116.  
  117.                     WebHeaderCollection headers = response.Headers;
  118.                     value = Convert.ToInt32(headers.Get("X-Pages"));
  119.                     response.Close();
  120.                     return value;
  121.                 }
  122.                 catch { countExeption++; Task.Delay(countExeption * 10).Wait(); }
  123.                 if (countExeption == 5) { break; }
  124.             }
  125.             throw new Exception();
  126.         }
  127.     }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment