SHARE
TWEET

Untitled

a guest Mar 21st, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Ophthalmology.DataAccess.Services
  9. {
  10.     public abstract class GenericService<T> where T : new()
  11.     {
  12.         static string _baseUrl = null;
  13.  
  14.         public GenericService(string serviceDiscoveryUrl)
  15.         {
  16.             ServiceDiscoveryClient.ServiceDiscoveryClient.ServiceDiscoveryUrl = serviceDiscoveryUrl;
  17.         }
  18.  
  19.         public async Task Initialize(AuthenticationHeaderValue authorization)
  20.         {
  21.             if (string.IsNullOrEmpty(GetKey()) || string.IsNullOrEmpty(_baseUrl))
  22.                 _baseUrl = await ServiceDiscoveryClient.ServiceDiscoveryClient.GetEndpoint(GetKey(), authorization);
  23.         }
  24.  
  25.         public async Task Initialize(string applicationID, string applicationVersion, string tenantId, string facilityId)
  26.         {
  27.             if (string.IsNullOrEmpty(GetKey()) || string.IsNullOrEmpty(_baseUrl))
  28.                 _baseUrl = await ServiceDiscoveryClient.ServiceDiscoveryClient.GetEndpoint(GetKey(), applicationID, applicationVersion, tenantId, facilityId);
  29.         }
  30.  
  31.         public string GetBaseUrl()
  32.         {
  33.             return _baseUrl;
  34.         }
  35.  
  36.         public abstract string GetKey();
  37.  
  38.         public abstract T GetClientClass(string baseUrl, HttpClient httpClient);
  39.  
  40.  
  41.         public async Task<T> GetClient(AuthenticationHeaderValue authorization)
  42.         {
  43.             System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
  44.             if (authorization != null)
  45.             {
  46.                 httpClient.DefaultRequestHeaders.Authorization = authorization;
  47.             }
  48.  
  49.             await Initialize(authorization);
  50.  
  51.             return GetClientClass(GetBaseUrl(), httpClient);
  52.         }
  53.  
  54.         public async Task<T> GetClient(string applicationID, string applicationVersion, string tenantId, string facilityId)
  55.         {
  56.             System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
  57.  
  58.             await Initialize(applicationID, applicationVersion, tenantId, facilityId);
  59.  
  60.             return GetClientClass(GetBaseUrl(), httpClient);
  61.         }
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top