Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Forked from joshmarlow/ambition_api_c#_example.cs
Last active September 28, 2015 20:55
Show Gist options
  • Save thinkt4nk/854a1c74fea3ff2704eb to your computer and use it in GitHub Desktop.
Save thinkt4nk/854a1c74fea3ff2704eb 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/api/v1/data/<DATA_TYPE>";
string uploadPayload = File.ReadAllText(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, string payload, string authToken)
{
using (WebClient client = new WebClient()) {
client.Headers.Add("Authorization", String.Format("Token {0}", authToken));
client.Headers.Add("Content-Type", "text/csv");
Console.WriteLine(String.Format("Uploading data '{0}'", payload));
return client.UploadString(endpoint, "POST", payload);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment