Created
August 26, 2014 23:07
-
-
Save hazzik/e24c492cc34e8b0bfe6c to your computer and use it in GitHub Desktop.
Simple dynamic REST client
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
public class RestClient : DynamicObject | |
{ | |
private readonly string _address; | |
public RestClient(string address) | |
{ | |
this._address = address; | |
} | |
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
var request = new RestRequest(_address + "/" + binder.Name); | |
for (int index = 0; index < binder.CallInfo.ArgumentNames.Count; index++) | |
{ | |
var name = binder.CallInfo.ArgumentNames[index]; | |
var value = args[index]; | |
request.Add(name, value); | |
} | |
result = request.GetAsync(); | |
return true; | |
} | |
} |
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
public class RestRequest : IEnumerable | |
{ | |
private readonly string _uri; | |
private readonly NameValueCollection _qs = HttpUtility.ParseQueryString(String.Empty); | |
public RestRequest(string uri, IEnumerable<KeyValuePair<string, object>> args) | |
: this(uri) | |
{ | |
_uri = uri; | |
foreach (var kvp in args) | |
Add(kvp.Key, kvp.Value); | |
} | |
public RestRequest(string uri) | |
{ | |
_uri = uri; | |
} | |
public void Add(string key, object value) | |
{ | |
// ReSharper is wrong here. Null is perfectly valid value. | |
// ReSharper disable once AssignNullToNotNullAttribute | |
_qs.Add(key, value == null ? null : value.ToString()); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return _qs.GetEnumerator(); | |
} | |
public Task<object> GetAsync() | |
{ | |
return GetAsync<object>(); | |
} | |
public async Task<T> GetAsync<T>() | |
{ | |
using (var client = new HttpClient()) | |
{ | |
var uri = BuildUri(); | |
var response = await client.GetAsync(uri); | |
var stream = await response.Content.ReadAsStreamAsync(); | |
if (response.StatusCode != HttpStatusCode.OK) | |
{ | |
var exception = Deserialize<ExceptionInfo>(stream); | |
throw new ApplicationException(exception.Message); | |
} | |
return Deserialize<JsonResult<T>>(stream).d; | |
} | |
} | |
private string BuildUri() | |
{ | |
if (_qs.Count > 0) | |
{ | |
return _uri + "?" + _qs; | |
} | |
return _uri; | |
} | |
private static T Deserialize<T>(Stream stream) | |
{ | |
JsonSerializer serializer = JsonSerializer.Create(); | |
using (var reader = new JsonTextReader(new StreamReader(stream))) | |
{ | |
return serializer.Deserialize<T>(reader); | |
} | |
} | |
public class ExceptionInfo | |
{ | |
public string ExceptionDetail { get; set; } | |
public string ExceptionType { get; set; } | |
public string Message { get; set; } | |
public string StackTrace { get; set; } | |
} | |
public class JsonResult<T> | |
{ | |
public T d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment