Skip to content

Commit

Permalink
Merge pull request #334 from Sidekick-Poe/feature/package-update
Browse files Browse the repository at this point in the history
Removed deprecated packages and updated some packages
  • Loading branch information
leMicin authored Dec 19, 2024
2 parents 5064091 + a5c4557 commit a773069
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 101 deletions.
176 changes: 84 additions & 92 deletions src/Sidekick.Apis.PoeWiki/PoeWikiClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Logging;
using Sidekick.Apis.PoeWiki.Api;
using Sidekick.Apis.PoeWiki.Extensions;
Expand All @@ -15,11 +14,13 @@ namespace Sidekick.Apis.PoeWiki
/// PoeWiki.net API.
/// https://www.poewiki.net/wiki/Path_of_Exile_Wiki:Data_query_API
/// </summary>
public class PoeWikiClient(
public class PoeWikiClient
(
ILogger<PoeWikiClient> logger,
IHttpClientFactory httpClientFactory,
IBrowserProvider browserProvider,
ICacheProvider cacheProvider) : IPoeWikiClient
ICacheProvider cacheProvider
) : IPoeWikiClient
{
private readonly JsonSerializerOptions options = new()
{
Expand Down Expand Up @@ -65,18 +66,17 @@ private HttpClient GetHttpClient()
/// <inheritdoc/>
public async Task Initialize()
{
var result = await cacheProvider.GetOrSet(
"PoeWikiBlightOils",
async () =>
{
var result = await GetMetadataIdsFromItemNames(oilNames);
if (result == null)
{
return new();
}

return result;
});
var result = await cacheProvider.GetOrSet("PoeWikiBlightOils",
async () =>
{
var result = await GetMetadataIdsFromItemNames(oilNames);
if (result == null)
{
return new();
}

return result;
});

BlightOilNamesByMetadataIds = result.ToDictionary(x => x.MetadataId ?? string.Empty, x => x.Name ?? string.Empty);
}
Expand All @@ -85,29 +85,28 @@ public async Task Initialize()
{
try
{
var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "query"),
new("format", "json"),
new("titles", $"File:{mapType} area screenshot.png|File:{mapType} area screenshot.jpg"),
new("prop", "imageinfo"),
new("iiprop", "url"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "query"),
new("format", "json"),
new("titles", $"File:{mapType} area screenshot.png|File:{mapType} area screenshot.jpg"),
new("prop", "imageinfo"),
new("iiprop", "url"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStreamAsync();
var json = await JsonNode.ParseAsync(content);

var screenshotUrl = json!["query"]!
["pages"]!.AsObject()?.First(x => x.Key != "-1").Value!
["imageinfo"]?.AsArray()?.First()!
["url"]?.AsValue().GetValue<string>()!;
var screenshotUrl = json!["query"]!["pages"]!.AsObject().First(x => x.Key != "-1").Value!["imageinfo"]?.AsArray().First()!["url"]?.AsValue().GetValue<string>()!;

return new Uri(screenshotUrl);
}
catch (Exception) { }
catch (Exception)
{
// Not having a map screenshot is not exception worthy.
}

return null;
}
Expand All @@ -116,26 +115,23 @@ public async Task Initialize()
{
try
{
var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "1"),
new("tables", "maps,items,areas"),
new("join_on", "items._pageID=maps._pageID,maps.area_id=areas.id"),
new("fields", "items.name,maps.area_id,areas.boss_monster_ids,items.drop_monsters,areas.area_type_tags"),
new("group_by", "items.name"),
new("where", @$"items.name=""{mapType}"" AND items.drop_enabled = true"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "1"),
new("tables", "maps,items,areas"),
new("join_on", "items._pageID=maps._pageID,maps.area_id=areas.id"),
new("fields", "items.name,maps.area_id,areas.boss_monster_ids,items.drop_monsters,areas.area_type_tags"),
new("group_by", "items.name"),
new("where", @$"items.name=""{mapType}"" AND items.drop_enabled = true"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStreamAsync();
var result = await JsonSerializer.DeserializeAsync<CargoQueryResult<MapResult>>(content, options);
return result
?.CargoQuery.Select(x => x.Title)
.FirstOrDefault();
return result?.CargoQuery.Select(x => x.Title).FirstOrDefault();
}
catch (Exception e)
{
Expand All @@ -149,19 +145,18 @@ public async Task Initialize()
{
try
{
var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "monsters"),
new("fields", "monsters.name,monsters.metadata_id"),
new("where", @$"monsters.metadata_id IN ({mapResult.BossMonsterIds.ToQueryString()})"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "monsters"),
new("fields", "monsters.name,monsters.metadata_id"),
new("where", @$"monsters.metadata_id IN ({mapResult.BossMonsterIds.ToQueryString()})"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStreamAsync();
var result = await JsonSerializer.DeserializeAsync<CargoQueryResult<BossResult>>(content, options);
if (result == null)
Expand Down Expand Up @@ -194,20 +189,19 @@ public async Task Initialize()
{
try
{
var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("fields", "items.name,items.description,items.flavour_text,items.drop_level"),
new("tables", "items"),
new("where", @$"items.drop_areas HOLDS '{mapResult.AreaId}' AND items.is_in_game = true AND items.drop_enabled = true"),
new("order by", "items.drop_level DESC"),
new("limit", "500"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("fields", "items.name,items.description,items.flavour_text,items.drop_level"),
new("tables", "items"),
new("where", @$"items.drop_areas HOLDS '{mapResult.AreaId}' AND items.is_in_game = true AND items.drop_enabled = true"),
new("order by", "items.drop_level DESC"),
new("limit", "500"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<CargoQueryResult<ItemResult>>(content, options);
if (result == null)
Expand Down Expand Up @@ -242,20 +236,19 @@ public async Task Initialize()
{
var enchantmentText = modifierLine.Text.Replace("Allocates ", string.Empty);

var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "blight_crafting_recipes,blight_crafting_recipes_items,mods,passive_skills"),
new("join_on", "blight_crafting_recipes_items.recipe_id=blight_crafting_recipes.id,blight_crafting_recipes.modifier_id=mods.id,blight_crafting_recipes.passive_id=passive_skills.id"),
new("fields", "blight_crafting_recipes_items.item_id"),
new("where", @$"passive_skills.name='{enchantmentText}' OR mods.stat_text='{enchantmentText}'"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "blight_crafting_recipes,blight_crafting_recipes_items,mods,passive_skills"),
new("join_on", "blight_crafting_recipes_items.recipe_id=blight_crafting_recipes.id,blight_crafting_recipes.modifier_id=mods.id,blight_crafting_recipes.passive_id=passive_skills.id"),
new("fields", "blight_crafting_recipes_items.item_id"),
new("where", @$"passive_skills.name='{enchantmentText}' OR mods.stat_text='{enchantmentText}'"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStreamAsync();
var result = await JsonSerializer.DeserializeAsync<CargoQueryResult<ItemIdResult>>(content, options);
if (result == null)
Expand Down Expand Up @@ -288,19 +281,18 @@ public async Task Initialize()
{
try
{
var query = new QueryBuilder(
new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "items"),
new("fields", "items.name,items.metadata_id"),
new("where", @$"items.name IN ({itemNames.ToQueryString()})"),
});
var query = new List<KeyValuePair<string, string>>
{
new("action", "cargoquery"),
new("format", "json"),
new("limit", "500"),
new("tables", "items"),
new("fields", "items.name,items.metadata_id"),
new("where", @$"items.name IN ({itemNames.ToQueryString()})"),
};

using var client = GetHttpClient();
var response = await client.GetAsync(query.ToString());
var response = await client.GetAsync(QueryStringHelper.ToQueryString(query));
var content = await response.Content.ReadAsStreamAsync();
var result = await JsonSerializer.DeserializeAsync<CargoQueryResult<ItemNameMetadataIdResult>>(content, options);
if (result == null)
Expand Down
47 changes: 47 additions & 0 deletions src/Sidekick.Apis.PoeWiki/QueryStringHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Web;

namespace Sidekick.Apis.PoeWiki;

public static class QueryStringHelper
{
public static string ToQueryString(List<KeyValuePair<string, string>> parameters)
{
if (parameters == null || parameters.Count == 0)
{
return string.Empty;
}

var queryString = new List<string>();
foreach (var parameter in parameters)
{
// Encode both the key and value to ensure proper URL encoding
var encodedKey = HttpUtility.UrlEncode(parameter.Key);
var encodedValue = HttpUtility.UrlEncode(parameter.Value);

// Combine key and value as a query parameter
queryString.Add($"{encodedKey}={encodedValue}");
}

// Join all parameters with '&' and prepend a '?' for query string
return "?" + string.Join("&", queryString);
}

// Overload for dictionary input
public static string ToQueryString(Dictionary<string, string> parameters)
{
if (parameters == null || parameters.Count == 0)
{
return string.Empty;
}

var queryString = new List<KeyValuePair<string, string>>();
foreach (var parameter in parameters)
{
queryString.Add(new KeyValuePair<string, string>(parameter.Key, parameter.Value));
}

return ToQueryString(queryString);
}
}
1 change: 0 additions & 1 deletion src/Sidekick.Apis.PoeWiki/Sidekick.Apis.PoeWiki.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Sidekick.Common.Settings
@using Sidekick.Common.Blazor.Settings.Components

<KeybindEditor Value="@KeyOpen"
ValueChanged="KeyChanged"
Expand Down
3 changes: 0 additions & 3 deletions src/Sidekick.Common.Blazor/Update/Update.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public partial class Update
[Inject]
private ICacheProvider CacheProvider { get; set; } = null!;

[Inject]
private ICurrentView CurrentView { get; set; } = null!;

private string? Step { get; set; }
private bool Error { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SQLite" Version="3.13.0"/>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117"/>
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions src/Sidekick.Common.Platform/Sidekick.Common.Platform.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifiers>win-x64;win-x86;linux</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="NotificationIconSharp" Version="1.0.1" />
<PackageReference Include="PipeMethodCalls" Version="4.0.2" />
<PackageReference Include="PipeMethodCalls.NetJson" Version="3.0.0" />
<PackageReference Include="SharpHook" Version="5.3.8" />
Expand Down
4 changes: 2 additions & 2 deletions tests/Sidekick.Apis.Poe.Tests/Sidekick.Apis.Poe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit.web" Version="1.36.0" />
<PackageReference Include="bunit.web" Version="1.37.7" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -21,7 +21,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit a773069

Please sign in to comment.