Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a service what provides methods to send a API request. It is a async/await Task and returns a Task<responsestatus>.

The class what launches the service gets this and i want to check, if the ResponseStatus is ResponseStatus.Error.

I understand, that Task<restsharp.responsestatus> isnt directly compareable with Restsharp.ResponseError, but what can i do?

What I have tried:

I used as Service:
<pre lang="C#">
public async Task<ResponseStatus> SendRequest(){
// API Request
var result = response.ResponseStatus;
return result;
}


The Handler who launches the Request:
C#
var result = apiRequest.SendRequest(Method.Post, endpoint, body);
if (result == ResponseStatus.Error)
{ //stuff 
}
Posted
Updated 30-Mar-23 23:48pm
v2

1 solution

Your SendRequest method returns a Task<ResponseStatus>. You can't test the value returned by that task until the task has completed.

The simplest solution is to make your calling method async, and await the task:
C#
var result = await apiRequest.SendRequest(Method.Post, endpoint, body);
if (result == ResponseStatus.Error)
{
    // stuff
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900