Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TradeBeaver.ApiLoader.Models;
- using System.Net.Http;
- using Newtonsoft.Json.Linq;
- using System.Net;
- using System.Runtime.CompilerServices;
- namespace TradeBeaver.ApiLoader
- {
- public class Markets
- {
- string ApiBuild(int regionID, int type_id, int page)
- {
- return "https:/"+ $"/esi.evetech.net/latest/markets/{regionID}/orders/?datasource=tranquility&order_type=all&page={page}&type_id={type_id}";
- }
- private List<DontLock> BuildModel(List<StoreAccessSheet> storeAccessSheets)
- {
- List<DontLock> result = new List<DontLock>();
- int count = 0;
- foreach (var item in storeAccessSheets)
- {
- result.Add(new DontLock() { Count = count,Sheet = item });
- count++;
- }
- return result;
- }
- public List<Order> GetOrder(List<StoreAccessSheet> storeAccessSheets)
- {
- Pages<Order>[] result = new Pages<Order>[storeAccessSheets.Count];
- Parallel.ForEach(BuildModel(storeAccessSheets), sheetDontLock =>
- {
- int count = CountPage(ApiBuild(sheetDontLock.Sheet.regionID, sheetDontLock.Sheet.type_id, 1));
- result[sheetDontLock.Count] = RequestSheet(sheetDontLock.Sheet, count);
- });
- WaitTask(result.SelectMany(x=>x.TaskApi).ToList());
- UpdateErrorPages(result);
- return result.SelectMany(x => x.RequestPages).SelectMany(x=>x.Page).ToList();
- }
- private Pages<Order> RequestSheet(StoreAccessSheet sheet, int count)
- {
- Pages<Order> pages = new Pages<Order>(count, sheet);
- List<ConfiguredTaskAwaitable> taskApi = new List<ConfiguredTaskAwaitable>();
- for (int i = 0; i < count; i++)
- {
- taskApi.Add(RequestAPIAsync(pages, i).ConfigureAwait(false));
- }
- pages.TaskApi = taskApi;
- return pages;
- }
- private void UpdateErrorPages(Pages<Order>[] Arraypages)
- {
- if (!Arraypages.SelectMany(x=>x.RequestPages).Any(x => x.Page == null)) { return; }
- int errorCount = 0;
- Parallel.ForEach(Arraypages, pages =>
- {
- while (pages.RequestPages.Any(x => x.Page == null))
- {
- List<ConfiguredTaskAwaitable> taskApi = new List<ConfiguredTaskAwaitable>();
- foreach (var item in pages.RequestPages.Where(x => x.Page == null))
- {
- taskApi.Add(RequestAPIAsync(pages, item.Number).ConfigureAwait(false));
- }
- WaitTask(taskApi);
- errorCount++;
- if (errorCount > 10)
- {
- throw new Exception("Ошибка соединения с интернетом");
- }
- }
- });
- }
- private void WaitTask(List<ConfiguredTaskAwaitable> taskApi)
- {
- while (!taskApi.All(x=>x.GetAwaiter().IsCompleted) )
- {
- Task.Delay(5).Wait();
- }
- }
- private async Task RequestAPIAsync(Pages<Order> pages, int count)
- {
- try
- {
- pages.RequestPages[count ] = new PageElement<Order>( count,
- await ApiConect.GetApiAsync<List<Order>>(ApiBuild(pages.sheet.regionID, pages.sheet.type_id, count+1)));
- }
- catch
- {
- pages.RequestPages[count] = new PageElement<Order>(count,null);
- }
- }
- private int CountPage(string api)
- {
- int value = 0;
- int countExeption = 0;
- while (true)
- {
- try
- {
- WebRequest request = WebRequest.Create(api);
- WebResponse response = request.GetResponse();
- WebHeaderCollection headers = response.Headers;
- value = Convert.ToInt32(headers.Get("X-Pages"));
- response.Close();
- return value;
- }
- catch { countExeption++; Task.Delay(countExeption * 10).Wait(); }
- if (countExeption == 5) { break; }
- }
- throw new Exception();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment