Created
June 14, 2017 16:00
-
-
Save alexander-williamson/9255a9dfdb63e87d44a432911f016ea0 to your computer and use it in GitHub Desktop.
How to send files to logz.io kibana and have properties accepted as fields
This file contains 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
using System; | |
using System.Net; | |
using RestSharp; | |
namespace Alexw.ParkingAvailable | |
{ | |
public class KibanaClient | |
{ | |
private readonly IRestClient _client; | |
private readonly string _token; | |
public KibanaClient(IRestClient client, string token) | |
{ | |
_client = client; | |
_token = token; | |
} | |
public void Upload<T>(params T[] objectsToUpload) | |
{ | |
if(objectsToUpload == null) | |
throw new ArgumentOutOfRangeException("objectsToUpload"); | |
var relativeUrl = ""; | |
if (!string.IsNullOrEmpty(_token)) | |
{ | |
relativeUrl += relativeUrl.Contains("?") ? "&" : "?"; | |
relativeUrl += "token=" + _token; | |
} | |
foreach (var element in objectsToUpload) | |
{ | |
var request = new RestRequest(relativeUrl); | |
request.AddJsonBody(element); | |
var response = _client.Post(request); | |
if (response.StatusCode != HttpStatusCode.OK) | |
{ | |
throw new Exception(response.ErrorMessage); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment