From 865dfea03d782dd7d1c49482553a5e2181a25565 Mon Sep 17 00:00:00 2001 From: femsci Date: Thu, 22 Feb 2024 15:13:38 +0100 Subject: [PATCH] working requests --- src/Nyanbyte.PPCheck.Lib/ApiClient.cs | 39 ++++++++++++++++++- .../Models/OffenderInformation.cs | 19 +++++---- .../Models/{Response.cs => SearchResponse.cs} | 2 +- 3 files changed, 50 insertions(+), 10 deletions(-) rename src/Nyanbyte.PPCheck.Lib/Models/{Response.cs => SearchResponse.cs} (88%) diff --git a/src/Nyanbyte.PPCheck.Lib/ApiClient.cs b/src/Nyanbyte.PPCheck.Lib/ApiClient.cs index 1f797ed..eb031fa 100644 --- a/src/Nyanbyte.PPCheck.Lib/ApiClient.cs +++ b/src/Nyanbyte.PPCheck.Lib/ApiClient.cs @@ -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 { @@ -7,6 +14,18 @@ public class ApiClient : IDisposable private readonly HttpClient _http; 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) { _http = http; @@ -21,6 +40,24 @@ public class ApiClient : IDisposable }; } + public async Task 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() ?? throw new DataException("Invalid data returned."); + + return resp; + } + public void Dispose() { GC.SuppressFinalize(this); diff --git a/src/Nyanbyte.PPCheck.Lib/Models/OffenderInformation.cs b/src/Nyanbyte.PPCheck.Lib/Models/OffenderInformation.cs index 7c79fa7..b931119 100644 --- a/src/Nyanbyte.PPCheck.Lib/Models/OffenderInformation.cs +++ b/src/Nyanbyte.PPCheck.Lib/Models/OffenderInformation.cs @@ -8,7 +8,7 @@ public record OffenderInformation public int AnnotationCount { get; set; } [JsonPropertyName("persones")] - public ICollection Personas { get; set; } = new List(); + public ICollection Personas { get; set; } = []; } public record OffenderPersona @@ -17,14 +17,17 @@ public record OffenderPersona public Guid PersonIdentityId { get; set; } public string FirstName { get; set; } = string.Empty; public string? SecondName { get; set; } - public string LastName { get; set; } - public string CityOfBirth { get; set; } + public string LastName { get; set; } = string.Empty; + public string CityOfBirth { get; set; } = string.Empty; 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? MothersName { get; set; } - public Sex Sex { get; set; } - public string CountryOfBirth { get; set; } - public string Nationalities { get; set; } - public string DwellingPlace { get; set; } + public string CountryOfBirth { get; set; } = string.Empty; + public string Nationalities { get; set; } = string.Empty; + public string DwellingPlace { get; set; } = string.Empty; + + [JsonIgnore] + public Sex OffenderSex => (Sex)Sex; } diff --git a/src/Nyanbyte.PPCheck.Lib/Models/Response.cs b/src/Nyanbyte.PPCheck.Lib/Models/SearchResponse.cs similarity index 88% rename from src/Nyanbyte.PPCheck.Lib/Models/Response.cs rename to src/Nyanbyte.PPCheck.Lib/Models/SearchResponse.cs index 179f87b..9298def 100644 --- a/src/Nyanbyte.PPCheck.Lib/Models/Response.cs +++ b/src/Nyanbyte.PPCheck.Lib/Models/SearchResponse.cs @@ -1,6 +1,6 @@ namespace Nyanbyte.PPCheck.Lib.Models; -public record Response +public record SearchResponse { public int Total { get; set; } public bool HasError { get; set; }