GithubHelp home page GithubHelp logo

Comments (3)

qJake avatar qJake commented on July 21, 2024

A configure method would be a good idea - if you have time, give it a shot and I'll take a look. Thanks!

from hadotnet.

qJake avatar qJake commented on July 21, 2024

Hey @mitchcapper, in v1.6.0 we refactored from RestSharp to just using the built-in HttpClient.

If you're using the ClientFactory, you can access the HttpClient instance directly, after initializing, like so:

ClientFactory.Initialize("https://my.home.assistant/", "eyJ...............");
ClientFactory.Client // This is the underlying HttpClient that is used for API communication, you cannot set it, but you can manipulate its properties.

I think this will work for you, if not please let me know and I will reopen!

from hadotnet.

mitchcapper avatar mitchcapper commented on July 21, 2024

Sorry I missed this reply. HttpClient is great however to do the bulk of configuration for it you need to use the HttpClientHandler class and it can only be then passed to the HttpClient constructor, you cannot access it after the fact.

The good news is because of the refactor no modification to this library is needed to do so, we just create our own factory. If you like I can put a PR in to add an overload to the current initialize method that takes a HttpClientHandler to make it easy for others.

 public static class OurClientFactory {
            public static bool IsInitialized { get; internal set; }
            public static HttpClient Client { get; private set; }
            public static void Initialize(Uri instanceAddress, string apiKey, HttpClientHandler handler) {
                Client = new HttpClient(handler) {
                    BaseAddress = instanceAddress,
                    DefaultRequestHeaders =
                    {
                    Authorization = new AuthenticationHeaderValue("Bearer", apiKey),
                    AcceptEncoding =
                    {
                        new StringWithQualityHeaderValue("identity")
                    }
                }
                };
                IsInitialized = true;
            }
            public static void Reset() {
                IsInitialized = false;
                try {
                    Client?.Dispose();
                } catch {
                    // Can't dispose? Oh well.
                }
                Client = null;
            }
            public static void Initialize(string instanceAddress, string apiKey, HttpClientHandler handler) => Initialize(new Uri(instanceAddress), apiKey, handler);
            public static TClient GetClient<TClient>() where TClient : BaseClient => (TClient)Activator.CreateInstance(typeof(TClient), Client);

        }

This then allows you to do something like:
OurClientFactory.Initialize(uri, token, new HttpClientHandler { ClientCertificateOptions= ClientCertificateOption.Automatic });
or
OurClientFactory.Initialize(uri, token, new HttpClientHandler { ClientCertificateOptions= ClientCertificateOption.Manual,ClientCertificates.Add(client_cert) });

from hadotnet.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.