6 Commits

Author SHA1 Message Date
Dima yawaflua Andreev
8b0043e1ab Update nuget.yml 2024-01-19 20:07:44 +03:00
Dima yawaflua Andreev
46f184d775 Merge pull request #1 from yawaflua/yawaflua-patch-1
Change .NET version from 8 to 7
2024-01-19 20:02:04 +03:00
Dima yawaflua Andreev
85c4d5e099 Update SPWorldsWrapper.csproj 2024-01-19 20:00:58 +03:00
Dima yawaflua Andreev
1f81fd5cbd Update README.md 2024-01-19 19:59:45 +03:00
Дмитрий Шиманский
1201e16caa Add auth exception class for better readibility 2023-12-27 23:15:39 +03:00
Дмитрий Шиманский
a1862446a6 Hotfix and some code review 2023-12-16 21:27:10 +03:00
4 changed files with 36 additions and 11 deletions

View File

@@ -12,7 +12,7 @@ jobs:
- name: Setup .NET Core - name: Setup .NET Core
uses: actions/setup-dotnet@v1 uses: actions/setup-dotnet@v1
with: with:
dotnet-version: '8.x.x' dotnet-version: '7.x.x'
- name: Set output - name: Set output
id: vars id: vars
run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
@@ -22,7 +22,7 @@ jobs:
PROJECT_FILE_PATH: SPWorldsWrapper/SPWorldsWrapper.csproj PROJECT_FILE_PATH: SPWorldsWrapper/SPWorldsWrapper.csproj
VERSION_STATIC: ${{ steps.vars.outputs.tags }} VERSION_STATIC: ${{ steps.vars.outputs.tags }}
TAG_COMMIT: true TAG_COMMIT: true
TAG_FORMAT: '*' TAG_FORMAT: ${{ steps.meta.outputs.tags }}
NUGET_KEY: ${{secrets.NUGET_API_KEY}} NUGET_KEY: ${{secrets.NUGET_API_KEY}}
NUGET_SOURCE: https://api.nuget.org NUGET_SOURCE: https://api.nuget.org
INCLUDE_SYMBOLS: false INCLUDE_SYMBOLS: false

View File

@@ -1,5 +1,5 @@
# SPWorldsWrapper # SPWorldsWrapper
![](https://img.shields.io/badge/dotnet-.NET_8-green) ![](https://img.shields.io/badge/dotnet-.NET_7-green)
Actions pass: Actions pass:

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PackageId>SPWorldsWrapper</PackageId> <PackageId>SPWorldsWrapper</PackageId>

View File

@@ -2,13 +2,22 @@
using SPWorldsWrapper.Types; using SPWorldsWrapper.Types;
using System.Net; using System.Net;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Reflection.Metadata.Ecma335;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
namespace SPWorldsWrapper namespace SPWorldsWrapper
{ {
public class AuthentificationError : Exception
{
public AuthentificationError(string? message, Exception? innerException) : base(message, innerException) { }
public AuthentificationError() : base() { }
public AuthentificationError(string? message) : base(message) { }
}
public class SPWrapper public class SPWrapper
{ {
public readonly HttpClient client; public readonly HttpClient client;
/// <summary> /// <summary>
/// Асинхронный wrapper для работы напрямую с сайтом, а не с API SPWorlds.ru /// Асинхронный wrapper для работы напрямую с сайтом, а не с API SPWorlds.ru
@@ -21,7 +30,11 @@ namespace SPWorldsWrapper
client = new(handler); client = new(handler);
client.BaseAddress = new Uri("https://spworlds.ru/api/"); client.BaseAddress = new Uri("https://spworlds.ru/api/");
cookieContainer.Add(client.BaseAddress, new Cookie("jeff", token)); cookieContainer.Add(client.BaseAddress, new Cookie("jeff", token));
spwLogin(); var spwLoginMethod = spwLogin();
if (!spwLoginMethod.Result)
{
throw new AuthentificationError("Ошибка парсинга данных от сайта. Проверьте токен, IP сервера и статус сайта SPWORLDS.RU");
}
} }
/// <summary> /// <summary>
@@ -30,11 +43,24 @@ namespace SPWorldsWrapper
/// Если иное - токен слетел! /// Если иное - токен слетел!
/// </summary> /// </summary>
/// <returns>Ничего</returns> /// <returns>Ничего</returns>
public async Task spwLogin() public async Task<bool> spwLogin()
{ {
var content = new StringContent(@"{}"); var content = new StringContent(@"{}");
var responseMessage = await client.PostAsync("auth/refresh_token", content); var responseMessage = await client.PostAsync("auth/refresh_token", content);
await Console.Out.WriteLineAsync(await responseMessage.Content.ReadAsStringAsync()); var bodyFromSPW = await responseMessage.Content.ReadAsStringAsync();
var serializedBody = JsonNode.Parse(bodyFromSPW);
if (serializedBody == null)
{
Console.Error.WriteLine("error: Some error returned from site.");
Console.WriteLine("debug: please, check your authorization token");
}
else
{
Console.WriteLine(await responseMessage.Content.ReadAsStringAsync());
}
return serializedBody != null;
} }
/// <summary> /// <summary>
/// Получение данных пользователя по юзернейму. /// Получение данных пользователя по юзернейму.
@@ -54,12 +80,11 @@ namespace SPWorldsWrapper
/// </summary> /// </summary>
/// <param name="userName">Никнейм пользователя(из майнкрафта)</param> /// <param name="userName">Никнейм пользователя(из майнкрафта)</param>
/// <example> var a = "a";</example> /// <example> var a = "a";</example>
/// <returns><see cref="SPUser" /> пользователь от сайта, или ошибку.</returns> /// <returns><see cref="SPUser" /> пользователь от сайта, или null.</returns>
public async Task<SPUser> getUserData(string userName) public async Task<SPUser?> getUserData(string userName)
{ {
var request = await client.GetAsync($"pl/accounts/{userName}"); var request = await client.GetAsync($"pl/accounts/{userName}");
await Console.Out.WriteLineAsync(request.Content.ReadAsStringAsync().Result); SPUser? response = JsonConvert.DeserializeObject<SPUser>(request.Content.ReadAsStringAsync().Result.ToString());
SPUser response = JsonConvert.DeserializeObject<SPUser>(request.Content.ReadAsStringAsync().Result.ToString());
return response; return response;
} }
/// <summary> /// <summary>