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

Add CancellationToken cancellationToken = default to generated Methods #19

Closed
kirides opened this issue Apr 24, 2023 · 8 comments · Fixed by #23
Closed

Add CancellationToken cancellationToken = default to generated Methods #19

kirides opened this issue Apr 24, 2023 · 8 comments · Fixed by #23
Assignees
Labels
enhancement New feature, bug fix, or request

Comments

@kirides
Copy link
Contributor

kirides commented Apr 24, 2023

Currently the generated code looks like this

[Post("/v1/Endpoint")]
Task EndpointPOST([Body] EndpointV1 body);

To support timeouts etc. it would be nice to get refitter to add the CancellationToken-parameter like this

[Post("/v1/Endpoint")]
Task EndpointPOST([Body] EndpointV1 body, CancellationToken cancellationToken = default);
@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Apr 24, 2023
@christianhelle christianhelle self-assigned this Apr 24, 2023
@christianhelle
Copy link
Owner

@kirides This is trivial to implement and I can get started with this immediately. Unless you want to give it a shot yourself?

@kirides
Copy link
Contributor Author

kirides commented Apr 24, 2023

We're currently in the evaluation phase of deciding to use this tool to generate our clients, sadly i can't provide an implementation at this stage... bureaucracy

@christianhelle
Copy link
Owner

I'm currently trying this out and I can see that Refit doesn't seem to like CancellationToken cancellationToken = default

I generated this code

[Get("/pet/{petId}")]
Task<Pet> GetPetById(long petId, CancellationToken cancellationToken = default);

But when I run it I just get a Make sure it has at least one method with a Refit HTTP method attribute and Refit is installed in the project. error when I run the generated code

Unhandled exception. System.InvalidOperationException: ISwaggerPetstore doesn't look like a Refit interface. Make sure it has at least one method with a Refit HTTP method attribute and Refit is installed in the project.
   at Refit.RestService.GetGeneratedType(Type refitInterfaceType) in /_/Refit/RestService.cs:line 173
   at Refit.RestService.For(Type refitInterfaceType, HttpClient client, IRequestBuilder builder) in /_/Refit/RestService.cs:line 76
   at Refit.RestService.For[T](HttpClient client, IRequestBuilder`1 builder) in /_/Refit/RestService.cs:line 20
   at Refit.RestService.For[T](HttpClient client, RefitSettings settings) in /_/Refit/RestService.cs:line 34
   at Refit.RestService.For[T](String hostUrl, RefitSettings settings) in /_/Refit/RestService.cs:line 54
   at Refit.RestService.For[T](String hostUrl) in /_/Refit/RestService.cs:line 65
   at MyNamespace.Program.TestPetstoreUsingDirectTypes() in C:\projects\christianhelle\refitter\test\ConsoleApp\Program.cs:line 34
   at MyNamespace.Program.Main(String[] args) in C:\projects\christianhelle\refitter\test\ConsoleApp\Program.cs:line 11
   at MyNamespace.Program.<Main>(String[] args)

I'm investigating as why this is

@kirides
Copy link
Contributor Author

kirides commented Apr 25, 2023

Sounds interesting, manually specifying it in a .NET 6.0 project works fine.

Here is the code from refit that checks for its existence (many places call that, and other chains also check for its existence)
https://github.com/reactiveui/refit/blob/ef463956936fcdfd248d120672fe0bb7ff93d5ac/Refit/RequestBuilderImplementation.cs#L474

@christianhelle
Copy link
Owner

@kirides did you get the interface containing a CancellationToken in the arguments to work? I have no luck with this. I already have the code for generating an interface that uses CancellationToken as the last argument but I can't get the interface to work

@kirides
Copy link
Contributor Author

kirides commented Apr 26, 2023

Using Refit 6.0.1 & 6.3.2 this works flawlessly:
(Cancellation also works like it should)

image

@christianhelle
Copy link
Owner

christianhelle commented Apr 28, 2023

@kirides I honestly couldn't get CancellationToken to work, but if your example works then I trust you on that. Generating code that uses CancellationTokens was trivial to implement and I expect to release this feature this in the afternoon

@christianhelle
Copy link
Owner

This feature is now released as v0.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants