Example API Usage – Tech Status

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 Management Console.
  2. The manager used to run this must be a real manager. We suggest you setup a specific manager for API interaction stuff.

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

namespace TechStatusExample {

    public class Program {

        public static System.Text.Json.JsonSerializerOptions DefaultJSONOptions = new System.Text.Json.JsonSerializerOptions() {
            AllowTrailingCommas = true,
            IncludeFields = true,
            PropertyNameCaseInsensitive = true,
            DictionaryKeyPolicy = null,
            PropertyNamingPolicy = null, // Note: necessary to make sure that, eg, "ClientGuid" does not become "clientGuid"
            ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles
        };

        public class TechClientVersion {
            public string Version = "";
            public DateTime LastContact;
        }

        public class Tech {
            public string Name = "";
            public string FirstName = "";
            public string LastName = "";
            public string Email = "";
            public string Phone = "";

            public int TechId;
            public string PublicKey = "";
            public string Status = "";

            public string UserNameAddon = "";

            public List ADGroups = new List();
            public List Groups = new List();

            public DateTime LastUpdated;

            public string TechClientVersion = ""; // legacy
            public string TechClientMobileVersion = ""; // legacy

            public Guid TechGuid;

            public List TechClientVersions = new List();

            public bool AllowMFAReset;
        };

        public class TechStatus {
            public static readonly string newTech = "new";
            public static readonly string active = "active";
            public static readonly string keychange = "keychange";
            public static readonly string pending = "pending";
            public static readonly string disable = "disable";
            public static readonly string delete = "delete";
        }

        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 it 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 techname = args[3];
            string newstatus = args[4];

            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);
            nvc.Add("authenticationmethod", "local");

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

            // This section gets the current tech status
            {
                nvc.Add("TechName", techname);
                string full_address = $"{host}/client/tech/info?{nvc}";
                Console.WriteLine(full_address);

                HttpResponseMessage response = await client.GetAsync(full_address);
                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(t);
                Tech tech = System.Text.Json.JsonSerializer.Deserialize(t, DefaultJSONOptions);
            }

            // This section sets the status 
            {
                Tech tech = new Tech() {
                    Name = techname,
                    Status = newstatus,                    
                };

                HttpContent content = new StringContent(System.Text.Json.JsonSerializer.Serialize(tech, DefaultJSONOptions), System.Text.Encoding.UTF8, "application/json");
                string full_address = $"{host}/client/tech/status?{nvc}";
                Console.WriteLine(full_address);

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

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

            }
        }
    }
}