public async Task <HttpClient> GetHttpClient(SupportedServices serviceToCall) { //METHOD FLOW: // // Reasoning for method: // We need this to be treadsafe and as such we use a concurrentDictionary // This is not enough as the call to create a new client is time consuming (needs security token) // To solve this we have the following flow: // // - Try to get a cached value // - If none is found create a new and make a concurrent GetOrAdd // This is done to ensure that we; get one that has been added by another tread OR add ours to the cache // - return the found client HttpClient client; var isCachedClientFound = _serviceToClientMap.TryGetValue(serviceToCall, out client); if (!isCachedClientFound) { var newClientToAdd = await CreateNewHttpClient(serviceToCall); client = _serviceToClientMap.GetOrAdd(serviceToCall, newClientToAdd); } return(client); }
public RestClientBase( IServiceDiscovererDeprecated serviceDiscovererDeprecated, SupportedServices serviceToCall) { _serviceToCall = serviceToCall; _httpClientFactory = new HttpClientFactory(serviceDiscovererDeprecated); }
public async Task <string> RequestTokenAsync(SupportedServices serviceToCall) { var payload = "grant_type=password&username={EC}&password={EC}"; var content = new StringContent(payload, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await PostAsyncStringContent("Token", content, serviceToCall); dynamic tokenPayload = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return(tokenPayload.access_token); }
private async Task Configure(HttpClient client, SupportedServices serviceToCall) { client.Timeout = _timeOut; client.BaseAddress = _baseAddressResolver.ResolveBaseAddress(serviceToCall); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); //var bearerToken = await _tokenCreator.RequestTokenAsync(_bioCredentials, serviceToCall); //client.SetBearerToken(bearerToken); }
private async Task <HttpResponseMessage> PostAsyncStringContent( string uriExtension, StringContent stringContent, SupportedServices serviceToCall) { uriExtension = UriFormatter.FormatUriAsExtension(uriExtension); using (var client = new HttpClient()) { client.BaseAddress = _baseAddressResolver.ResolveBaseAddress(serviceToCall); return(await client.PostAsync(uriExtension, stringContent)); } }
public Uri ResolveBaseAddress(SupportedServices serviceToCall) { //TODO support serviceLookup switch (serviceToCall) { case SupportedServices.ApiService: return(new Uri(_serviceDiscovererDeprecated.GetApiServiceAddress())); default: throw new ArgumentOutOfRangeException(serviceToCall.ToString(), serviceToCall, null); } }
private async Task <HttpClient> CreateNewHttpClient(SupportedServices serviceToCall) { //_logger.LogDebug($"New HTTP client created for {serviceToCall}"); var client = new HttpClient( new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip }); await Configure(client, serviceToCall); return(client); }
/// <summary> /// Finds the service identified by the request type. /// </summary> protected ServiceDefinition FindService(ExpandedNodeId requestTypeId) { ServiceDefinition service = null; if (!SupportedServices.TryGetValue(requestTypeId, out service)) { throw ServiceResultException.Create( StatusCodes.BadServiceUnsupported, "'{0}' is an unrecognized service identifier.", requestTypeId); } return(service); }
/// <summary> /// Dispatches an incoming binary encoded request. /// </summary> /// <param name="incoming">Incoming request.</param> public virtual IServiceResponse ProcessRequest(IServiceRequest incoming) { try { SetRequestContext(RequestEncoding.Binary); ServiceDefinition service = null; // find service. if (!SupportedServices.TryGetValue(incoming.TypeId, out service)) { throw new ServiceResultException(StatusCodes.BadServiceUnsupported, Utils.Format("'{0}' is an unrecognized service identifier.", incoming.TypeId)); } // invoke service. return(service.Invoke(incoming)); } catch (Exception e) { // create fault. return(CreateFault(incoming, e)); } }
protected DomainServiceRestClientBase( IServiceDiscovererDeprecated serviceDiscovererDeprecated, SupportedServices serviceToCall) { _restClient = new RestClientBase(serviceDiscovererDeprecated, serviceToCall); }