-
-
Save HakanL/1250422 to your computer and use it in GitHub Desktop.
basic client for StatsMix REST API (updated to RestSharp 102)
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 RestSharp; | |
namespace StatsMix | |
{ | |
// Requires RestSharp | |
// http://restsharp.org | |
// http://github.com/johnsheehan/RestSharp | |
// Usage | |
// var client = new StatsMixClient("apikeyhere"); | |
// var response1 = client.CreateStat(1, 2, 3, DateTime.UtcNow); | |
// var statId = response1.Data.Id; | |
// var status = response1.StatusCode; | |
// var response2 = client.GetStat(statId); | |
// var response3 = client.UpdateStat(statId, 2, 3, 4); | |
// var response4 = client.DeleteStat(1); | |
public class StatsMixClient | |
{ | |
const string _baseUrl = "http://statsmix.com/api/v1"; | |
RestClient _client; | |
public StatsMixClient(string apiKey) | |
{ | |
_client = new RestClient(_baseUrl); | |
_client.AddDefaultHeader("X-StatsMix-Token", apiKey); | |
} | |
public RestResponse<StatsMixResponse> GetStat(int id) | |
{ | |
var request = new RestRequest("stats/{id}.xml"); | |
request.AddUrlSegment("id", id.ToString()); | |
return _client.Execute<StatsMixResponse>(request); | |
} | |
public RestResponse<StatsMixResponse> CreateStat(int value, int profileId, int metricId, DateTime generatedAt) | |
{ | |
var request = new RestRequest("stats", Method.POST); | |
request.AddParameter("value", value); | |
request.AddParameter("profile_id", profileId); | |
request.AddParameter("metric_id", metricId); | |
request.AddParameter("generated_at", generatedAt.ToString("yyyy-MM-dd'T'HH:mm:ss")); | |
return _client.Execute<StatsMixResponse>(request); | |
} | |
public RestResponse<StatsMixResponse> UpdateStat(int id, int value, int profileId, int metricId, DateTime generatedAt) | |
{ | |
var request = new RestRequest("stats/{id}", Method.PUT); | |
request.AddUrlSegment("id", id.ToString()); | |
request.AddParameter("value", value); | |
request.AddParameter("profile_id", profileId); | |
request.AddParameter("metric_id", metricId); | |
return _client.Execute<StatsMixResponse>(request); | |
} | |
public RestResponse DeleteStat(int id) | |
{ | |
var request = new RestRequest("stats/{id}", Method.DELETE); | |
request.AddUrlSegment("id", id.ToString()); | |
return _client.Execute(request); | |
} | |
} | |
public class StatsMixResponse | |
{ | |
public DateTime CreatedAt { get; set; } | |
public DateTime GeneratedAt { get; set; } | |
public int Id { get; set; } | |
public int MetricId { get; set; } | |
public int ProfileId { get; set; } | |
public int ServiceId { get; set; } | |
public int? ServiceTypeId { get; set; } | |
public int StatTypeId { get; set; } | |
public DateTime UpdatedAt { get; set; } | |
public int Value { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to work with RestSharp version 102.1