Nswag: BaseUrl can be set on injected HttpClient and may not be added to generated Client class

Created on 8 Feb 2017  路  13Comments  路  Source: RicoSuter/NSwag

Hi,

I'm glad to see InjectHttpClient settings was added as I used to have custom templates for this.
In my use cases, HttpClient is built with its BaseAddress set and thus, the Client class do not need to know about it. It just build HttpRequestMessage with relative path.

Could it be possible to add a setting to handle this use case on top of InjectHttpClient ?

All 13 comments

Just make the property private?

Or try to use HttpClient.BaseAddress if not null in place of BaseUrl when formatting url

And expose a ctor with just HttpClient

Can you provide some sample code?

Yes,

Here is the code produced with my modified Template:

public partial class ApiRestClient : IApi
{
    private System.Net.Http.HttpClient _httpClient; 
    public ApiRestClient(System.Net.Http.HttpClient httpClient)
    {
        _httpClient = httpClient; 
    }

Then, as I completely remove BaseUrl field and property, I need to tweak the string format for url

public async System.Threading.Tasks.Task<RoutingResponse> GetAsync(System.Threading.CancellationToken cancellationToken) { var url_ = string.Format("{0}?", "get");

Then, I create a
new ApiRestClient( new HttpClient { BaseAddress = new Uri("...") } );

So basically a setting ExposeBaseUrl (default true) which removes all BaseUrl usages when false?

Yes, that would be nice.

You can test it with the CI artifacts

Perfect, thanks

I'm wondering what the best way to use this property is. Is it only useful when injecting HttpClient?

I suppose I could implement partial void PrepareRequest(HttpClient request, StringBuilder urlBuilder) and insert the host at the beginning when I'm not injecting HttpClient?

It doesn't look like one could implement partial void PrepareRequest(HttpClient request, string url) since string is immutable.

In summary, if this is basically only useful when injecting a HttpClient with base set, I figure the documentation should mention that? Or at least mention that with everything else left as defaults, this is not useful.

--

I'm refactoring a client that someone else started on, and one of the above options seems promising over how it currently works:

  1. manually modifying the generated code :/

    • Removing the generated constructor to have it use one of the same signature in a partial class that loads the BaseUrl from the config parameter

I think you can modify it in base ctor when BaseClass is set

What are you referring to by "it"?

The base class doesn't have access to members of the generated class, and BaseUrl is a member of the generated class

I'm experiencing the same issue as @Eibwen.
I would like to set the BaseUrl in my BaseClass once, instead of setting the BaseUrl every time I create an instance from the generated clients. Only BaseUrl is a member of the generated class.

It would be ideal to have an option like UseHttpClientCreationMethod:
UseHttpClientCreationMethod: Indicates whether to call CreateHttpClientAsync on the base class to create a new HttpClient instance (ClientBaseClass must be defined).
which would be:
UseGetBaseUrlMethod: Indicates whether to call GetBaseUrlMethod on the base class to get the BaseUrl (ClientBaseClass must be defined).

For the record, I ended up just doing "useBaseUrl": false, and "useHttpClientCreationMethod": true, and set it in the CreateHttpClientAsync method:
``` protected async Task CreateHttpClientAsync(CancellationToken cancellationToken)
{
var client = new HttpClient();

        client.BaseAddress = new Uri(_configuration.BaseUrl);

        return client;
    }

```

Was this page helpful?
0 / 5 - 0 ratings