working requests

This commit is contained in:
femsci 2024-02-22 15:13:38 +01:00
parent 88b851b79a
commit 865dfea03d
Signed by: femsci
GPG key ID: 21AC2CC03E5BBCD6
3 changed files with 50 additions and 10 deletions

View file

@ -1,4 +1,11 @@
namespace Nyanbyte.PPCheck.Lib; using System.Data;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Nyanbyte.PPCheck.Lib.Models;
namespace Nyanbyte.PPCheck.Lib;
public class ApiClient : IDisposable public class ApiClient : IDisposable
{ {
@ -7,6 +14,18 @@ public class ApiClient : IDisposable
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly bool _isHttpExternal; private readonly bool _isHttpExternal;
private static readonly JsonSerializerOptions _jsonOpts = new()
{
AllowTrailingCommas = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
NumberHandling = JsonNumberHandling.Strict,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReadCommentHandling = JsonCommentHandling.Disallow,
WriteIndented = false
};
public ApiClient(HttpClient http) public ApiClient(HttpClient http)
{ {
_http = http; _http = http;
@ -21,6 +40,24 @@ public class ApiClient : IDisposable
}; };
} }
public async Task<SearchResponse> Search(SearchRequest req)
{
var request = new HttpRequestMessage(HttpMethod.Post, "persone/search");
request.Headers.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0");
request.Headers.Add("Referer", "https://rps.ms.gov.pl/pl-PL/Public");
request.Headers.Add("Accept", "application/json, text/plain, */*");
request.Headers.Add("Accept-Language", "en-US,en;q=0.5");
request.Content = new StringContent(JsonSerializer.Serialize(req, _jsonOpts), null, "application/json");
var httpResponse = await _http.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
var resp = await httpResponse.Content.ReadFromJsonAsync<SearchResponse>() ?? throw new DataException("Invalid data returned.");
return resp;
}
public void Dispose() public void Dispose()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);

View file

@ -8,7 +8,7 @@ public record OffenderInformation
public int AnnotationCount { get; set; } public int AnnotationCount { get; set; }
[JsonPropertyName("persones")] [JsonPropertyName("persones")]
public ICollection<OffenderPersona> Personas { get; set; } = new List<OffenderPersona>(); public ICollection<OffenderPersona> Personas { get; set; } = [];
} }
public record OffenderPersona public record OffenderPersona
@ -17,14 +17,17 @@ public record OffenderPersona
public Guid PersonIdentityId { get; set; } public Guid PersonIdentityId { get; set; }
public string FirstName { get; set; } = string.Empty; public string FirstName { get; set; } = string.Empty;
public string? SecondName { get; set; } public string? SecondName { get; set; }
public string LastName { get; set; } public string LastName { get; set; } = string.Empty;
public string CityOfBirth { get; set; } public string CityOfBirth { get; set; } = string.Empty;
public DateTime DateOfBirth { get; set; } public DateTime DateOfBirth { get; set; }
public string FamilyName { get; set; } public char Sex { get; set; }
public string FamilyName { get; set; } = string.Empty;
public string? FathersName { get; set; } public string? FathersName { get; set; }
public string? MothersName { get; set; } public string? MothersName { get; set; }
public Sex Sex { get; set; } public string CountryOfBirth { get; set; } = string.Empty;
public string CountryOfBirth { get; set; } public string Nationalities { get; set; } = string.Empty;
public string Nationalities { get; set; } public string DwellingPlace { get; set; } = string.Empty;
public string DwellingPlace { get; set; }
[JsonIgnore]
public Sex OffenderSex => (Sex)Sex;
} }

View file

@ -1,6 +1,6 @@
namespace Nyanbyte.PPCheck.Lib.Models; namespace Nyanbyte.PPCheck.Lib.Models;
public record Response public record SearchResponse
{ {
public int Total { get; set; } public int Total { get; set; }
public bool HasError { get; set; } public bool HasError { get; set; }