Skip to content

Instantly share code, notes, and snippets.

@couellet
Last active November 10, 2015 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save couellet/b78dcc0226957f891a92 to your computer and use it in GitHub Desktop.
Save couellet/b78dcc0226957f891a92 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators;
/*
You will need to install these two NuGet packages:
Install-Package RestSharp
Install-Package Newtonsoft.Json
*/
namespace Demo
{
class Program
{
private const string SNIPCART_API_KEY = "SECRET_API_KEY";
static void Main(string[] args)
{
Console.WriteLine("Getting orders from Snipcart");
var client = new RestClient("https://app.snipcart.com");
client.Authenticator = new HttpBasicAuthenticator(SNIPCART_API_KEY, "");
var request = new RestRequest("api/orders");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddQueryParameter("offset", "0");
request.AddQueryParameter("limit", "20");
var response = client.Get(request);
// Response content will be the raw data in JSON
Console.WriteLine(response.Content);
// With JSON.NET parse the response into a JSON object.
var json = JObject.Parse(response.Content);
// To access the orders from the paged result, use the items
var raw = json["items"];
// You can create an object to deserialize items to or use dynamic keyword if you wish to. This list will contain all the orders returned from the API call.
var items = raw.ToObject<List<dynamic>>();
foreach (var i in items)
{
Console.WriteLine(i.token);
}
Console.WriteLine(items.Count);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment