sitecore-xamarin-pcl-sdk
sitecore-xamarin-pcl-sdk copied to clipboard
Allow developers to pass a custom HttpMessageHandler for requests
This is useful for:
- testing purposes (mocking)
- native HttpMessageHandler implementations (ModernHttpClient) - as the mono implementation of Httpclient is very slow.
The HttpMessageHandler can be passed optionally, defaulting back to the existing implementation.
Hi, Could you please specify real use case when changing HttpMessageHandler is required? It was hidden by design.
The HttpMessageHandler is the handler used to send http requests from the HttpClient. HttpClient basically bulds the requests and delegates the actual transmission to the handler.
- modernhttpclient provides a NativeMessageHandler that uses native libraries (Android: okhttp, iOS: NSURLSession) to increase performance from the mono implementation.
- We use an OfflineMessageHandler to load data from disk instead of making HTTPRequests, as the static files produce reproducable test results.
@charri , I guess, the point, provided by @painie is :
The HttpMessageHandler class is different for PCL and iOS/Android.
Meaning, the NativeMessageHandler will be a sub-class of iOS.HttpMessageHandler
. You will not be able to use it since the SDK accepts the PCL.HttpMessageHandler
instances.
It would be nice to have some interface
for HTTP transport.
Unfortunately, HttpMessageHandler is inherited only from IDisposable
. As far as I know, there is no way to change it. @charri , any ideas?
Updated my message, being able to use ModernHttpClient definitely makes sense. We'll not merge this request now, but will review it before next release of SDK (no ETA yet)
@dodikk if you look at modernhttpclient implementation you can see that the signature for NativeMessageHandler the iOS and the android are the same as the facade in the pcl. Thus, the plattform specific implementation is used depending on the current plattform running.
If an interface is essential, you could provide a ServiceFacade with a property / method to resolve the desired HttpMessageHandler.
public interface HttpMessageHandlerFacade
{
HttpMessageHandler Handler {get;}
}
public class NativeMessageHandlerConfiguration : HttpMessageHandlerFacade
{
HttpMessageHandler Handler
{
get { return new NativeHttpMessageHandler(); } // example
}
}
@painie great - thanks!