using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using RelyenceApiSDK; using Newtonsoft.Json; namespace RelyenceApiTutorial { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private async void button1_Click(object sender, EventArgs e) { // credentials string apiKey = "yDI4nDU2YmEtYzBjMC00M2QyLwIwYzQtNjcynTM3NDE2mDCyOjEyNjZmYmJmLTYwzmItNGu0YS1HNWMwLWriYWZlY2QwumQ0Mw=="; string username = "john.dixon@relyence.com"; string password = "relyence1"; string version = null; string website = null; var relyenceApi = new RelyenceAPI(apiKey, username, password, version, website); if (relyenceApi.intializeConnection()) { // get the Id of the Analysis that we want to insert a Problem into var analysisName = "Drone Example"; var resultJson = await relyenceApi.GetAnalyses(); List analyses = JsonConvert.DeserializeObject>(resultJson); if (analyses == null || analyses.Count == 0) { MessageBox.Show("Unable to find any Analyses. Expected to find at least 1.", "Analyses Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // find the analysis we need Analysis analysis = analyses.Where(a => a.Name == analysisName).FirstOrDefault(); if (analysis == null) { MessageBox.Show("Unable to find Analysis.", "Analysis Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var analysisId = analysis.Id; // get the list of subsystems in this Analysis. We'll associate our Problem with the first one. There is always at least one in an Analysis file resultJson = await relyenceApi.GetSubsystems(analysisId, false, false); List subsystems = JsonConvert.DeserializeObject>(resultJson); if (subsystems == null) { MessageBox.Show("Unable to retrieve Subsystems", "Subsystems Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var subsystemId = subsystems[0].Id; // data for our new Problem Dictionary listValues = new Dictionary(); listValues["Description"] = "This is my new Problem added through the API."; listValues["Date Created"] = DateTime.UtcNow; listValues["Team Members"] = "John Smith"; // insert new Problem. Not associating with an Incident, so pass a zero. resultJson = await relyenceApi.InsertProblem(analysis.Id, listValues, subsystemId); Problem problem = JsonConvert.DeserializeObject(resultJson); if (problem == null) { MessageBox.Show("Error inserting Problem", "Problem Insert Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // query for newly added Problem. Pass zeroes for subsystem and incident ids since we're only looking for one record // list of Problem fields to retrieve for the record List fields = new List(); fields.Add("Problem ID"); fields.Add("Description"); fields.Add("Date Created"); fields.Add("Team Members"); fields.Add("Closed"); resultJson = await relyenceApi.GetProblems(analysisId, fields, problem.Id); List problems = JsonConvert.DeserializeObject>(resultJson); if (problems == null || problems.Count == 0) { MessageBox.Show("Error retrieving Problem", "Problem Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } } } public class Analysis { [JsonProperty("id")] public int Id; [JsonProperty("Name")] public string Name; } public class Subsystem { [JsonProperty("Id")] public int Id; [JsonProperty("Name")] public string Name; [JsonProperty("ParentId")] public string ParentId; [JsonProperty("Level")] public int Level; } public class Problem { [JsonProperty("Id")] public int Id; [JsonProperty("Problem ID")] public string ProblemID; [JsonProperty("Description")] public string Description; [JsonProperty("Date Created")] public DateTime? DateCreated; [JsonProperty("Team Members")] public string TeamMembers; // not a real database field. Holds the temporary results from an Insert or Update action. [JsonProperty("Results Log")] public string ResultsLog; } }