sitecore-xamarin-pcl-sdk icon indicating copy to clipboard operation
sitecore-xamarin-pcl-sdk copied to clipboard

Allow developers to pass a custom HttpMessageHandler for requests

Open charri opened this issue 9 years ago • 6 comments

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.

charri avatar Jul 20 '15 08:07 charri

Hi, Could you please specify real use case when changing HttpMessageHandler is required? It was hidden by design.

kzotin avatar Jul 21 '15 07:07 kzotin

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 avatar Jul 23 '15 10:07 charri

@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.

dodikk avatar Jul 23 '15 13:07 dodikk

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?

dodikk avatar Jul 23 '15 13:07 dodikk

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)

kzotin avatar Jul 23 '15 13:07 kzotin

@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!

charri avatar Jul 23 '15 14:07 charri