Skip to content

Instantly share code, notes, and snippets.

@HakanL
Forked from johnsheehan/StatsMixClient.cs
Created September 29, 2011 09:57
Show Gist options
  • Save HakanL/1250422 to your computer and use it in GitHub Desktop.
Save HakanL/1250422 to your computer and use it in GitHub Desktop.
basic client for StatsMix REST API (updated to RestSharp 102)
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; }
}
}
@HakanL
Copy link
Author

HakanL commented Sep 29, 2011

Updated to work with RestSharp version 102.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment