Skip to content

Instantly share code, notes, and snippets.

@diegodfsd
Last active October 16, 2018 19:05
Show Gist options
  • Save diegodfsd/256836c3d2a2d7051a6d68663821e6a9 to your computer and use it in GitHub Desktop.
Save diegodfsd/256836c3d2a2d7051a6d68663821e6a9 to your computer and use it in GitHub Desktop.
public class AuthClient {
private readonly IAppLogger<StandardHttpClient> _logger = new Logger<StandardHttpClient>();
private readonly IHttpClient _httpClient = new StandardHttpClient(_logger);
public async Task<dynamic> Authenticate(string clientId, string clientSecret)
{
var baseAddress = "http://api.logstore.com.br";
var body = new { client_id = clientId, client_secret = clientSecret, grant_typpe = "client_credentials" };
var data = await _httpClient.PostAsync("/sessions",
with =>
{
with.Host(baseAddress).Body(body, HttpRequestDataType.FormData);
with.Header("ContentType", "application/x-www-form-urlencoded; charset=UTF-8");
});
_logger.Debug($"response from trace request: {data}");
return Json.Parse(data);
}
}
public class DeviceClient {
private readonly IAppLogger<StandardHttpClient> _logger = new Logger<StandardHttpClient>();
private readonly IHttpClient _httpClient = new StandardHttpClient(_logger);
public async Task<dynamic> GetDevices(string token, HttpRequestDataType dataType = HttpRequestDataType.JSON)
{
var baseAddress = "http://api.logstore.com.br";
var data = await _httpClient.GetAsync("/devices",
with => with.Host(baseAddress).Credential(token).Query(new {offset=0, limit=10}));
_logger.Debug($"response from trace request: {data}");
return Json.Parse(data);
}
public async Task<dynamic> GetDevice(string token, int id, HttpRequestDataType dataType = HttpRequestDataType.JSON)
{
var baseAddress = "http://api.logstore.com.br";
var data = await _httpClient.GetAsync($"/device/{id}",
with => with.Host(baseAddress).Credential(token));
_logger.Debug($"response from trace request: {data}");
return Json.Parse(data);
}
}
public class Logger<T> : IAppLogger<T>
{
private readonly Log _log = Log.Get(typeof(T));
public void Information(string message, params object[] args)
{
}
public void Debug(string message, params object[] args)
{
}
public void Warning(string message, params object[] args)
{
}
public void Error(Exception ex, string message, params object[] args)
{
}
public void Critical(Exception ex, string message, params object[] args)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment