Skip to content

Commit 073bdd5

Browse files
committed
Move items related to web calls to their own namespace
1 parent 841046b commit 073bdd5

File tree

12 files changed

+143
-143
lines changed

12 files changed

+143
-143
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace EnhancePoE.Model.WebDataTypes;
5+
namespace EnhancePoE.Api.Data;
66

77
// names are from api
88
public class Item

EnhancePoE/Model/WebDataTypes/StashTabPropsList.cs renamed to EnhancePoE/Api/Data/StashTabPropsList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace EnhancePoE.Model.WebDataTypes;
3+
namespace EnhancePoE.Api.Data;
44

55
// property names from api
66
public class StashTabProps
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Text.Json.Serialization;
66
using System.Threading.Tasks;
77

8-
namespace EnhancePoE.Model;
8+
namespace EnhancePoE.Api;
99

1010
internal sealed class League
1111
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace EnhancePoE.Model;
1+
namespace EnhancePoE.Api;
22

33
internal static class RateLimit
44
{

EnhancePoE/Api/StashTabGetter.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using System.Windows;
6+
using System.Net;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text.Json;
10+
using ZemotoCommon;
11+
using EnhancePoE.Model;
12+
using EnhancePoE.Api.Data;
13+
14+
namespace EnhancePoE.Api;
15+
16+
internal sealed class StashTabGetter
17+
{
18+
private bool _isFetching;
19+
20+
public async Task<List<StashTab>> FetchStashTabsAsync()
21+
{
22+
string accName = Properties.Settings.Default.accName.Trim();
23+
string league = Properties.Settings.Default.League.Trim();
24+
25+
var stashTabPropsList = await GetStashPropsAsync(accName, league);
26+
if (stashTabPropsList is not null)
27+
{
28+
var stashTabs = new List<StashTab>();
29+
for (int i = 0; i < stashTabPropsList.tabs.Count; i++)
30+
{
31+
var stashTabProps = stashTabPropsList.tabs[i];
32+
var uri = new Uri($"https://www.pathofexile.com/character-window/get-stash-items?accountName={accName}&tabIndex={i}&league={league}");
33+
stashTabs.Add(new StashTab(stashTabProps.n, stashTabProps.i, uri));
34+
}
35+
36+
return stashTabs;
37+
}
38+
39+
_isFetching = false;
40+
return null;
41+
}
42+
43+
private async Task<StashTabPropsList> GetStashPropsAsync(string accName, string league)
44+
{
45+
if (_isFetching || RateLimit.CheckForBan())
46+
{
47+
return null;
48+
}
49+
50+
// -1 for 1 request + 3 times if ratelimit high exceeded
51+
if (RateLimit.RateLimitState[0] >= RateLimit.MaximumRequests - 4)
52+
{
53+
RateLimit.RateLimitExceeded = true;
54+
return null;
55+
}
56+
57+
_isFetching = true;
58+
using var __ = new ScopeGuard(() => _isFetching = false);
59+
60+
var propsUri = new Uri($"https://www.pathofexile.com/character-window/get-stash-items?accountName={accName}&tabs=1&league={league}&tabIndex=");
61+
using var res = await DoAuthenticatedGetRequestAsync(propsUri);
62+
if (!res.IsSuccessStatusCode)
63+
{
64+
_ = MessageBox.Show(res.StatusCode == HttpStatusCode.Forbidden ? "Connection forbidden. Please check your Account Name and Session ID." : res.ReasonPhrase,
65+
"Error fetching data", MessageBoxButton.OK, MessageBoxImage.Error);
66+
return null;
67+
}
68+
69+
using var content = res.Content;
70+
string resContent = await content.ReadAsStringAsync();
71+
return JsonSerializer.Deserialize<StashTabPropsList>(resContent);
72+
}
73+
74+
public async Task<bool> GetItemsAsync(StashTab stashTab)
75+
{
76+
if (_isFetching || RateLimit.CheckForBan())
77+
{
78+
return false;
79+
}
80+
81+
if (RateLimit.RateLimitState[0] >= RateLimit.MaximumRequests - 4)
82+
{
83+
RateLimit.RateLimitExceeded = true;
84+
return false;
85+
}
86+
87+
_isFetching = true;
88+
using var __ = new ScopeGuard(() => _isFetching = false);
89+
90+
using var res = await DoAuthenticatedGetRequestAsync(stashTab.StashTabUri);
91+
if (!res.IsSuccessStatusCode)
92+
{
93+
_ = MessageBox.Show(res.ReasonPhrase, "Error fetching data", MessageBoxButton.OK, MessageBoxImage.Error);
94+
return false;
95+
}
96+
97+
using var content = res.Content;
98+
string resContent = await content.ReadAsStringAsync();
99+
var deserializedContent = JsonSerializer.Deserialize<ItemList>(resContent);
100+
101+
stashTab.Quad = deserializedContent.quadLayout;
102+
stashTab.FilterItemsForChaosRecipe(deserializedContent.items);
103+
104+
return true;
105+
}
106+
107+
private async Task<HttpResponseMessage> DoAuthenticatedGetRequestAsync(Uri uri)
108+
{
109+
var cookieContainer = new CookieContainer();
110+
cookieContainer.Add(uri, new Cookie("POESESSID", Properties.Settings.Default.SessionId));
111+
112+
using var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
113+
using var client = new HttpClient(handler);
114+
115+
// add user agent
116+
client.DefaultRequestHeaders.Add("User-Agent", $"EnhancePoEApp/v{Assembly.GetExecutingAssembly().GetName().Version}");
117+
118+
var res = await client.GetAsync(uri);
119+
120+
// get new rate limit values
121+
string rateLimit = res.Headers.GetValues("X-Rate-Limit-Account").FirstOrDefault();
122+
string rateLimitState = res.Headers.GetValues("X-Rate-Limit-Account-State").FirstOrDefault();
123+
string responseTime = res.Headers.GetValues("Date").FirstOrDefault();
124+
RateLimit.DeserializeRateLimits(rateLimit, rateLimitState);
125+
RateLimit.DeserializeResponseSeconds(responseTime);
126+
127+
return res; // Needs to be disposed
128+
}
129+
}

EnhancePoE/Model/Cell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using EnhancePoE.Model.WebDataTypes;
1+
using EnhancePoE.Api.Data;
22
using ZemotoCommon.UI;
33

44
namespace EnhancePoE.Model;

EnhancePoE/Model/ItemSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
1+
using EnhancePoE.Api.Data;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
4-
using EnhancePoE.Model.WebDataTypes;
55

66
namespace EnhancePoE.Model;
77

EnhancePoE/Model/ItemSetManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using EnhancePoE.Api.Data;
34
using EnhancePoE.Model;
4-
using EnhancePoE.Model.WebDataTypes;
55
using ZemotoCommon.UI;
66

77
namespace EnhancePoE;

EnhancePoE/Model/StashTab.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
1+
using EnhancePoE.Api.Data;
2+
using System;
23
using System.Collections.Generic;
34
using System.Collections.ObjectModel;
4-
using EnhancePoE.Model.WebDataTypes;
55
using ZemotoCommon.UI;
66

77
namespace EnhancePoE.Model;

EnhancePoE/Model/StashTabGetter.cs

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)