pawotter / mastodon-api-cs

The Mastodon API Client Library for C#
https://github.com/tootsuite/mastodon
MIT License
27 stars 4 forks source link

Internalize usage of HttpClient #16

Closed toryalsip closed 7 years ago

toryalsip commented 7 years ago

I'm opening this up as a suggestion in the event it isn't already being worked on. The usage of the library feels a little cumbersome with having the consumer create an instance of HttpClient and then pass that instance down into the various objects that would use it. I think it would be an improvement if the consumer doesn't need to be concerned with that and have the library manage this creation and lifetime of those HttpClient objects. That would also make it necessary for the library to enforce a certain hierarchy to how its objects are created, and there is the likelihood that this could be a breaking change.

If this isn't something being worked on right now, I'd like to put together some suggested ways to implement this in the next few days.

53ningen commented 7 years ago

@jbalsip

Thanks for suggestion πŸ™

I decided to inject HttpClientinto MastodonApi to set custom user-agent header. However you are absolutely right. It is cumbersome with having the consumer create an instance of HttpClient.

So, I would like to make changes as following...

If you would like this and have time, could you try to implement for this suggestion?

(Sorry for my late reply. I’m afraid my expressions may be rude or hard to read, because I’m not so good at English. But please, be patient.)

53ningen commented 7 years ago

I’ve invited you @jbalsip to api maintainer team of this repository. If you want to join, please confirm the email send from GitHub. Thanks!

toryalsip commented 7 years ago

Thanks for the invite. Yes, I was thinking this would be something I could take on since I have some ideas. I know my time gets fairly limited myself so I can be slow with replies too, not to mention timezone differences.

Oh and don't worry if your English isn't perfect, I know it is a very challenging language. And if I say anything you find confusing, please feel free to ask. I also have some experience with Japanese, although I need more practice for certain, if that helps.

γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™οΌδΈ€η·’γ«ι ‘εΌ΅γ‚ŠγΎγ—γ‚‡γ†γ€‚

toryalsip commented 7 years ago

Well I do like the idea of having the HttpClient be injectable, especially if there are custom headers you or another consumer may want to set. It could also be useful for setting up mock behavior to expand unit test coverage.

For a first pass at this, I'd like to make the parameter an optional parameter, and if it is null then it can return a default instance created from a static method call. This means we can keep it injectable, but I think in the future we could make the default instance further configurable through additional options in the config class.

Before I go to bed I'll put together a pull request for what I'm thinking so far so you can let me know what you think.