private void RegisterClientsWithFactory(ClientFactorySettings factorySettings) { RequestClientFactory.Instance.RegisterMediaTypeSerializer <JsonMediaTypeSerializer>(); RequestClientFactory.Instance.RegisterMediaTypeSerializer <JsonMediaTypeSerializer>(InternetMediaTypes.HalJson); // Register each defined client within the configuration settings with the RequestClientFactory. foreach (ClientSettings clientSettings in factorySettings.Clients) { IRequestSettings requestSettings = BuildRequestSettings(clientSettings); RequestClientFactory.Instance.RegisterBaseAddress(clientSettings.BaseAddress, requestSettings); } }
// For each service API with a configured entry point, invoke the entry-point URL and cache // the entry-point resource. The entry-point resource contains template URLs used to load // initial resources after which links returned directly on resources are used for navigation. // This is a common pattern providing an entry into an API. private void LoadClientEntryPoints(ClientFactorySettings factorySettings) { HalEntryPointResource GetEntry(ClientSettings clientSettings) { var client = RequestClientFactory.Instance.GetClient(clientSettings.BaseAddress); var request = ApiRequest.Get(clientSettings.EntryPointUrl); var errorMsg = "Could not load entry resource at {EntryAddress} for base address {BaseAddress}"; HalEntryPointResource entryPoint = null; try { var response = client.Send <HalEntryPointResource>(request).Result; if (!response.IsSuccessStatusCode) { Context.Logger.LogError(LogEvents.EntryPoint, errorMsg, clientSettings.EntryPointUrl, clientSettings.BaseAddress); } entryPoint = response.Content; } catch (Exception ex) { Context.Logger.LogError(LogEvents.EntryPoint, ex, errorMsg, clientSettings.EntryPointUrl, clientSettings.BaseAddress); } return(entryPoint); } var clientsWithEntryPoints = factorySettings.Clients .Where(c => !string.IsNullOrWhiteSpace(c.EntryPointUrl)); foreach (ClientSettings clientSettings in clientsWithEntryPoints) { _entryPointMappings[clientSettings.BaseAddress] = GetEntry(clientSettings); } }
// Create a mapping between a simple name to identify an endpoint and the base URL. private void CreateAddressNameMapping(ClientFactorySettings factorySettings) { _addressNameMappings = factorySettings.Clients .ToDictionary(c => c.ClientName, c => c.BaseAddress); }