Skip to content

Instantly share code, notes, and snippets.

@johnsheehan
Created December 10, 2010 07:54
Show Gist options
  • Save johnsheehan/735931 to your computer and use it in GitHub Desktop.
Save johnsheehan/735931 to your computer and use it in GitHub Desktop.
basic client for StatsMix REST API
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 StatsMixResponse GetStat(int id)
{
var request = new RestRequest("stats/{id}.xml");
request.AddUrlSegment("id", id);
return _client.Execute<StatsMixResponse>(request);
}
public 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 StatsMixResponse UpdateStat(int id, int value, int profileId, int metricId, DateTime generatedAt)
{
var request = new RestRequest("stats/{id}", Method.PUT);
request.AddUrlSegment("id", id);
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);
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