-
-
Save redmanmale/b834679eea9d275d3d31e521f9c40809 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Obsolete("Use BaseUriClient instead")] | |
public abstract class BaseClient<TService> : IDisposable | |
{ | |
private static readonly Dictionary<MethodBase, string> _templatesForMethods = new Dictionary<MethodBase, string>(); | |
private const int MAX_STACKFRAME_NESTING = 10; | |
private readonly RestClient _client; | |
private readonly Uri _baseSubUri; | |
protected BaseClient(Uri baseUri, Uri baseSubUri, ILogger logger, TimeSpan? timeout = null) | |
{ | |
_baseSubUri = baseSubUri; | |
_client = new RestClient(baseUri, logger, timeout); | |
} | |
protected Task<TResult> GetAsync<TResult>(Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.GetAsync<TResult>(uri); | |
} | |
protected Task<TResult> PostAsync<TResult>(object body = null, Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.PostAsync<TResult>(body, uri); | |
} | |
protected Task<TResult> PutAsync<TResult>(object body = null, Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.PutAsync<TResult>(body, uri); | |
} | |
protected Task PostAsync(object body = null, Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.PostAsync(body, uri); | |
} | |
protected Task PutAsync(object body = null, Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.PutAsync(body, uri); | |
} | |
protected Task DeleteAsync(Dictionary<string, object> parameters = null) | |
{ | |
var uri = GetUri(parameters); | |
return _client.DeleteAsync(uri); | |
} | |
private Uri GetUri(Dictionary<string, object> parameters = null) | |
{ | |
MethodBase method = null; | |
for (var i = 0; i < MAX_STACKFRAME_NESTING; i++) | |
{ | |
var tempMethod = new StackFrame(i).GetMethod(); | |
if (typeof(TService).IsAssignableFrom(tempMethod.DeclaringType)) | |
{ | |
method = tempMethod; | |
break; | |
} | |
} | |
if (method == null) | |
{ | |
var stringBulder = new StringBuilder(); | |
stringBulder.Append("Search for Type=").AppendLine(typeof(TService).FullName); | |
for (var i = 0; i < MAX_STACKFRAME_NESTING; i++) | |
{ | |
var tempMethod = new StackFrame(i).GetMethod(); | |
stringBulder.AppendFormat("Level #{0}: DeclaringType={1}, MethodName={2}{3}", i, | |
tempMethod.DeclaringType, tempMethod.Name, Environment.NewLine); | |
} | |
throw new IndexOutOfRangeException("Interface method behind more than " + MAX_STACKFRAME_NESTING + " levels\r\n" + stringBulder); | |
} | |
string template; | |
if (_templatesForMethods.ContainsKey(method)) | |
{ | |
template = _templatesForMethods[method]; | |
} | |
else | |
{ | |
var methodInfo = typeof(TService).GetMethod(method.Name); | |
if (methodInfo == null) | |
{ | |
foreach (var interfaceType in typeof(TService).GetInterfaces()) | |
{ | |
var interfaceMethodInfo = interfaceType.GetMethod(method.Name); | |
if (interfaceMethodInfo != null) | |
{ | |
methodInfo = interfaceMethodInfo; | |
} | |
} | |
} | |
template = GetUriTemplate(methodInfo); | |
_templatesForMethods.Add(method, template); | |
} | |
var subUri = WcfUri.GetStringFromTemplate(template, parameters ?? new Dictionary<string, object>(0)); | |
return _baseSubUri.Concat(subUri); | |
} | |
private static string GetUriTemplate(MethodInfo methodInfo) | |
{ | |
var webGet = methodInfo.GetCustomAttributes(typeof(WebGetAttribute), true); | |
if (webGet.Length > 0) | |
{ | |
return ((WebGetAttribute)webGet[0]).UriTemplate; | |
} | |
var webInvoke = methodInfo.GetCustomAttributes(typeof(WebInvokeAttribute), true); | |
if (webInvoke.Length > 0) | |
{ | |
return ((WebInvokeAttribute)webInvoke[0]).UriTemplate; | |
} | |
throw new ArgumentException("Method doesn't have WebGet or WebInvoke attribute"); | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
_client?.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment