Skip to content

Instantly share code, notes, and snippets.

@joshmarlow
Created June 9, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshmarlow/3b3d49b698c7a47889d8 to your computer and use it in GitHub Desktop.
Save joshmarlow/3b3d49b698c7a47889d8 to your computer and use it in GitHub Desktop.
Ambition API - C# Example
/*
* upload_to_ambition.cs
* Simple program demonstrating how to upload data to the Ambition Data API
*
* This code needs to be linked to the System.Web.Extensions assembly.
*
* For mono, use 'gmcs ambition_api_c#_example.cs -r:System.Web.Extensions'
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
namespace Pusher
{
class MainClass
{
public static void Main (string[] args)
{
if (args.Length < 1) {
Console.WriteLine("usage: upload_to_ambition.exe <FILENAME>");
return;
}
string filename = args [0];
if (! File.Exists(filename)) {
Console.WriteLine("File '{0}' cannot be found", filename);
}
string authToken = <AUTH-TOKEN>;
string endpoint = "https://<SUBDOMAIN>.ambition.com/public-api/integration/v1/data/new/";
Dictionary<String, Object> uploadPayload = buildUploadPayload(filename);
try {
Console.WriteLine(UploadData(endpoint, uploadPayload, authToken));
} catch (Exception e) {
Console.WriteLine("Failed to upload");
Console.WriteLine(e);
}
}
public static string UploadData (string endpoint, Dictionary<String, Object> payload, string authToken)
{
// Prepare web request...
var jsonSerializer = new JavaScriptSerializer();
String data = jsonSerializer.Serialize(payload);
using (WebClient client = new WebClient()) {
client.Headers.Add("Authorization", String.Format("Token {0}", authToken));
client.Headers.Add("Content-Type", "application/json");
Console.WriteLine(String.Format("Uploading data '{0}'", data));
return client.UploadString(endpoint, "POST", data);
}
}
public static Dictionary<String, Object> buildUploadPayload(string filename)
{
Dictionary<String, Object> payload = new Dictionary<String, Object>();
string fileContent = File.ReadAllText(filename);
payload.Add("depot", "custom_depot");
payload.Add("data", fileContent);
return payload;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment