Example API Usage – CreateLeaf

At Ruffian Software, Inc. we are developers and we love to see our product and community grow….alright enough with the niceties….to the code! This code is available under the MIT License.

 

 

TechIDManager has a REST API to allow you to do more with TechIDManager, and customize it to your will. This example shows how to get the information about a tech and change a tech’s status. Documentation is available at https://chdoc.ruffiansoftware.com/swagger . If you have any questions, just let us know. 

Important things about this C# example:

    1. The API Key and IP address need to be setup in the TechIDPortal.
    2. The manager used to run this must be a real manager. We suggest you setup a specific manager for API interaction stuff.
  1.  

If you have any questions, email Support@TechIDManager.com 

namespace CreateLeaf {

  public class Program {

    public class AccountLeaf {
        public string Path { get; set; }  = "";
        public int AccountLeafId { get; set; } = 0;
    }

    static async Task Main(string[] args) {

        // this is TERRIBLE command line parsing, but command line parsing is not the point of this example...so

        // https://ch100.ruffiansoftware.com/
        string host = args[0];

        // this API key is specific to an IPAddress it is important to store this API key somewhere safe. 
        // the use of commandline is just for example, we suggest you keep it somewhere safer than a scipt file. 
        string apikey = args[1];

        string email = args[2];

        string leaf_to_create = args[3];

        System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(string.Empty); 

        // The user we want any changes logged as
        // must be a valid manager
        nvc.Add("Email", email);

        // if the manager is required to login with a specific authentication method, you must specify it here
        //nvc.Add("authenticationmethod", "local"); 
        nvc.Add("authenticationmethod", "Microsoft");

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "APIKey " + apikey);

        // the ID of zero is to create a new leaf
        string full_address = $"{host}/client/accountleaf/0?{nvc}";
        Console.WriteLine(full_address);

        AccountLeaf accountLeaf = new AccountLeaf() { Path = leaf_to_create };
        string json = System.Text.Json.JsonSerializer.Serialize(accountLeaf);
        HttpContent content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");


        HttpResponseMessage response = await client.PostAsync(full_address, content);
        if(!response.IsSuccessStatusCode) {
            Console.WriteLine($"{response.StatusCode} ({response.StatusCode.ToString()}) : {response.ReasonPhrase}");
            return;
        }

        string t = await response.Content.ReadAsStringAsync();

        Console.WriteLine($"StatusCode = {response.StatusCode}");
        Console.WriteLine($"Result = {t}");

    }
}