Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Forked from redmanmale/BaseClient.cs
Created July 21, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pzixel/8ba801f24bd82a793797b59edcc07e98 to your computer and use it in GitHub Desktop.
Save Pzixel/8ba801f24bd82a793797b59edcc07e98 to your computer and use it in GitHub Desktop.
[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