Comments (3)
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.
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.
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)
- C# Friendly Entity Classes
- HADotNet stopped working after 0.118 update HOT 16
- ClientFactory.Initialize, how to check if connection is established ? HOT 1
- Issue with the Await and <EntityClient> HOT 3
- Deserializing array in attributes does not work HOT 1
- Unexpected response code 0 from Home Assistant API endpoint /api/states. HOT 2
- TLS 1.3 - Authentication failed because the remote party sent a TLS alert: 'ProtocolVersion'. HOT 2
- project test HOT 4
- Exception: Unexpected response code 404 from Home Assistant API endpoint /api/states/. HOT 1
- Error deserializing /API/SERVICES HOT 3
- Update Documentation and Add Code Examples
- Read /api/stream to receive server events HOT 3
- Donation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hadotnet.