C#
Requires .Net 4
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
namespace ST_SCM_Upload
{
// object to contain the data you want to upload. Must have a data_key passed into the constructor
class ScmData {
private string data_key;
private ArrayList items = new ArrayList();
public ScmData(string key) {
data_key = key;
}
public void AddItem(Item item) {
items.Add (item);
}
public string toJSON () {
var dk_json = "\"data_key\" : \"" + data_key + "\"";
var items_json = "\"items\" : [";
var c = "";
foreach (Item item in items) {
items_json = items_json + c + item.toJSON ();
c = ", ";
}
items_json = items_json + "]";
return "{" + dk_json + ", " + items_json + "}";
}
}
// object for each item you want to update
class Item {
private List<KeyValuePair<string, string>> fields = new List<KeyValuePair<string, string>>();
public void addField(string key, string value) {
var kv = new KeyValuePair<string, string> (key, value);
fields.Add (kv);
}
public string toJSON () {
var str = "{";
var c = "";
foreach (KeyValuePair<string, string> kv in fields) {
str = str + c + "\"" + kv.Key + "\" : \"" + kv.Value + "\"";
c = ", ";
}
return str + "}";
}
}
// helper class to convert the JWT JSON response to an object
public class JWTjson
{
public string token;
}
// the APIserver object used to login and post SCM data
public class APIServer {
private string baseurl; // base URL for the server
private const string me_path = "/api/v2/me/";
private const string login_path = "/api/v2/auth/jwt/";
private const string scm_path = "/api/v2/scm/upload/";
private HttpClient client;
// constructor must have the baseurl passed with the format "https://<hostname>" WITHOUT trailing slash
public APIServer(string b) {
baseurl = b;
// setup the HTTP client for requests
client = new HttpClient();
client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
// login with email / password credentials and retrieve the JWT token which is then set in the default header
public void login(string email, string password) {
var content = new MultipartFormDataContent();
content.Add (new StringContent(email), "email");
content.Add (new StringContent(password), "password");
HttpResponseMessage response = client.PostAsync(baseurl + login_path,content).Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body.
var json = response.Content.ReadAsStringAsync().Result;
// de-serialize the JSON into an JWTjson object
DataContractJsonSerializer jser = new DataContractJsonSerializer(typeof(JWTjson));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
JWTjson j = (JWTjson)jser.ReadObject(stream);
// set the JWT token in the Authorization header for all future client requests
client.DefaultRequestHeaders.Add("Authorization", "JWT " + j.token);
Console.WriteLine ("LOGIN SUCCESSFUL: User: {0} Token: {1}", email, j.token);
} else {
Console.WriteLine ("LOGIN FAILED for user {0} :{1} {2}",
email, (int)response.StatusCode, response.ReasonPhrase);
}
}
// post json data to the server to update SCM data
public string postSCMdata(string json) {
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseurl + scm_path, content).Result;
var res = "";
if (response.IsSuccessStatusCode)
{
// Parse the response body.
res = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("SCM UPDATE SUCCESSFUL: {0}", res);
}
else
{
Console.WriteLine("ERROR SETTING SCM DATA: {0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
return res;
}
// method to get user status. useful for debugging permission issues
public string getMe() {
return client.GetStringAsync(baseurl + me_path).Result;
}
}
class MainClass
{
static void Main(string[] args)
{
// instantiate the APIserver with the baseurl (without trailing slash!).
var api = new APIServer("https://api.scantrust.com");
//Then log in to get the token
api.login ("test@scantrust.com", "demo");
// you can use api.getMe to find out if you have the right permissions.
Console.WriteLine ("\n=========== ABOUT ME: {0}", api.getMe ());
// let's make an scm update.
// first create the ScmData object with the key field. Here I am using the extended_id.
// You can also use serial number or a logistic unit (if defined as an SCM field)
var scm = new ScmData ("extended_id");
// we now create an item. Note that i HAVE to add the data_key field (in this case the extended_id)
var item = new Item ();
item.addField ("extended_id", "AA01FCE9560410MRP0410F4E4C1BB8A");
item.addField ("text999", "this_is_a_test");
// now add the item to the ScmData object.
scm.AddItem (item);
// Rinse, wash, repeat. One more:
var item2 = new Item ();
item2.addField ("extended_id", "8348C164EB0410MRP041066F4B68BC0");
item2.addField ("text999", "this_is_a_test_again");
scm.AddItem (item2);
// Now convert it all to JSON. You can also create your own JSON, as the example below shows,
// or use any other JSON libraries:
//var json = "{\"data_key\": \"extended_id\", \"items\": [ { \"extended_id\": \"AA01FCE9560410MRP0410F4E4C1BB8A\", \"status\": \"0\" } ] }";
var json = scm.toJSON();
// now post it to the server and watch the magic happen.
api.postSCMdata (json);
}
}
}