Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using async all the way #111

Merged
merged 3 commits into from
Apr 12, 2024

Conversation

rahulpnath
Copy link
Contributor

  • Update all method to be Async
  • Updated the Function Names to reflect Async
    This will force to update the code and make sure to add await in the consumer code
  • Returning AsyncEnumerables for the paged requests endpoints
  • Currently not supporting the sync endpoints
    Should it be?

#55

@mroloux
Copy link
Member

mroloux commented Apr 4, 2024

Thanks, that's quite a PR 😆

Could you explain to us non-experts what the advantage is of switching to async calls? And whether it makes sense to support both sync and async calls?

@rahulpnath
Copy link
Contributor Author

rahulpnath commented Apr 5, 2024

Thank you for the quick response @mroloux . I'll try my best to explain it in simple terms and also put in some recommended links/articles 😀

Async/Await is the way to perform asynchronous programming in .NET.

When synchronously calling the HTTP methods you are essentially blocking the calling thread until you get back a response from the other side. Whereas with using asynchronous programming, as soon as you submit your HTTP request, the calling thread is free to do other work, until you get back a response from the other side.

This is a great reference on introduction to Asynchronous Programming in .NET

Support both sync and async? I don't think there is a need to do this. Given that you are already using RestSharp the Execute method is just a wrapper around the async call as you can see here

    public static RestResponse Execute(this IRestClient client, RestRequest request, Method httpMethod)
        => AsyncHelpers.RunSync(() => client.ExecuteAsync(request, httpMethod));

You can also read more about it here. Do you see a need to support sync versions as well?

If required by any of the consumers, they can use the same AsyncHelpers as used in the RestSharp code above.

Let me know if that help.

@mroloux
Copy link
Member

mroloux commented Apr 5, 2024

Alright. Let's go with the async version then.

I would use the original method names though, e.g. Create instead of CreateAsync.

Also, should we add an optional CancellationToken token parameter to each method?

@rahulpnath
Copy link
Contributor Author

@mroloux Great! Adding in a cancellation token sounds excellent. I will update the PR.

As for keeping the same Function name, this might cause issues for anyone using it today unless they make sure to add the await keyword. Having the function name different will force them to look at all the places explicitly and change to the XXXAsync version and have them think to add in the await as well.

But this could be something that can be called out in the release doc/Readme. Happy to go with whatever way you decide to go ahead with!

@mroloux
Copy link
Member

mroloux commented Apr 5, 2024

Yeah, it's a tradeoff. But I'm afraid we'll never get rid of the XXXAsync naming if we don't bite the bullet now.

The release notes will need to be very clear about this of course.

@rahulpnath
Copy link
Contributor Author

@mroloux Having the Async suffix is a general convention in .NET. Here is a guideline from Microsoft.

Add "Async" as the suffix of every async method name you write.

It is even more helpful in this case since it's a breaking change.

That said, I will leave it to you to finalize before I work on the PR again next week. Have a good weekend 😀

@mroloux
Copy link
Member

mroloux commented Apr 5, 2024

Ok, let's keep the XXXAsync names then if it's a convention 👍

@mroloux
Copy link
Member

mroloux commented Apr 5, 2024

One small remark: it would be nice if ListChartsTest.MultiplePages() would still assert that the order of retrieved charts is correct.

This assertion should work:

Assert.Equal(charts.Select(c => c.Id).OrderByDescending(x => x), retrievedCharts.Select(c => c.Id));

- Ensure to create the charts one after the other to
test that the charts returned are in expected order.
@rahulpnath
Copy link
Contributor Author

@mroloux Added in cancellation token support and also ensured that ListChartsTest.MultiplePages() checks the order.

@mroloux mroloux merged commit a34500f into seatsio:master Apr 12, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants