DMax
DMax
CC#
Created by DMax on 8/20/2023 in #help
✅ How to return List from RestClient?
Hi, I am making rest client for my application using RestSharp (like their twitter client in guide https://restsharp.dev/usage.html#api-client). Everywhere in code I have something like
public record Entity1(string SomeCoolString);

public record GetSomethingResponse
{
[JsonPropertyName("inconsistent_name_within_whole_api")]
public int Id { get; set; }

[JsonPropertyName("Cool_Entities")]
public List<Entity1> CoolEntities { get; set; }
}

public async Task<GetSomethingResponse> GetSomething()
{
var request = new RestRequest("endpoint");
var response = await _client.ExecuteAsync<GetSomethingResponse>(request);
return response.Data;
}
public record Entity1(string SomeCoolString);

public record GetSomethingResponse
{
[JsonPropertyName("inconsistent_name_within_whole_api")]
public int Id { get; set; }

[JsonPropertyName("Cool_Entities")]
public List<Entity1> CoolEntities { get; set; }
}

public async Task<GetSomethingResponse> GetSomething()
{
var request = new RestRequest("endpoint");
var response = await _client.ExecuteAsync<GetSomethingResponse>(request);
return response.Data;
}
But one endpoint returns just an array of entities and I don't like how Task<List<CoolerEntity2>> looks In TypeScript I could make alias type GetEntities = CoolerEntity2[] and use it. Is there something similar in C#? Also, should I use ICollection instead of List? P.S. Models (or DTO?) and client code are in separated files
5 replies