private TApi GetRestApiImplementation <TApi>() { if (_implementations.ContainsKey(typeof(TApi))) { return((TApi)_implementations[typeof(TApi)]); } var httpClientHandlerType = ApiDefinitionAttributeExtension.GetHttpClientHandlerType <TApi>(); var httpClientMessageHandler = GetHandler(httpClientHandlerType); var httpClient = new HttpClient(httpClientMessageHandler) { BaseAddress = ApiDefinitionAttributeExtension.GetUri <TApi>(), Timeout = ApiDefinitionAttributeExtension.GetTimeout <TApi>() }; if (httpClient.BaseAddress == null) { throw new InvalidOperationException($"HttpClient Base Address has not been set. " + $"In case you are not using {nameof(ApiDefinitionAttribute)} on your interface, set global constant in:" + $"{nameof(BaseApiConfiguration)} class."); } var restApi = default(TApi); try { restApi = _refitSettings != null ? RestService.For <TApi>(httpClient, _refitSettings) : RestService.For <TApi>(httpClient); _implementations.Add(typeof(TApi), restApi); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } return(restApi); }
private DelegatingHandler GetConfiguredHandler <TApi>() { var httpClientHandlerType = ApiDefinitionAttributeExtension.GetHttpClientHandlerType <TApi>(); var httpClientMessageHandler = default(DelegatingHandler); if (_handlerFactories.ContainsKey(httpClientHandlerType) && !_handlerImplementations.ContainsKey(httpClientHandlerType)) { var factory = _handlerFactories[httpClientHandlerType]; _handlerImplementations.Add(httpClientHandlerType, factory()); } if (_handlerImplementations.ContainsKey(httpClientHandlerType)) { httpClientMessageHandler = _handlerImplementations[httpClientHandlerType]; } else { httpClientMessageHandler = Activator.CreateInstance(httpClientHandlerType) as DelegatingHandler; _handlerImplementations.Add(httpClientHandlerType, httpClientMessageHandler); } return(httpClientMessageHandler); }