C
C#16mo ago
DMax

✅ 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
4 Replies
Kouhai
Kouhai16mo ago
You can alias types using GetEntities = ICollection<GetSomethingResponse> Whether you return List or ICollection depends on what you want the consumers to do, if you want them to access elements by index then IList or List
DMax
DMaxOP16mo ago
Oh, thank you, appreciate your help! I checked c# api reference and guess even IEnumerable will be enough
Kouhai
Kouhai16mo ago
<:MadoThumbsUp_MM:406514447973351444>
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server