Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Windows.Forms;
- using PXtech.LiveIQAPI;
- using System.Collections;
- using System.IO;
- using System.Text;
- using System.Xml;
- namespace APIClient
- {
- public partial class APIClient : Form
- {
- // class for adding items to combo box
- private class ListItem
- {
- public string Name { get; set; }
- public string Value { get; set; }
- public ListItem(string name, string value)
- {
- Name = name;
- Value = value;
- }
- public override string ToString()
- {
- return Name;
- }
- }
- public APIClient()
- {
- InitializeComponent();
- // default the date to be yesterday
- dtDate.Value = DateTime.Now.AddDays(-1);
- }
- private void ListAPIs(APIService apiService)
- {
- cboAPIs.Items.Clear();
- Dictionary<string, string> apis = apiService.ListAPIs();
- IDictionaryEnumerator dicEnum = apis.GetEnumerator();
- while (dicEnum.MoveNext())
- {
- string reportName = (string)dicEnum.Key;
- string reportDescription = (string)dicEnum.Value;
- cboAPIs.Items.Add(new ListItem(reportName + " (" + reportDescription + ")", reportName));
- }
- }
- private void ListGroups(APIService apiService)
- {
- cboGroups.Items.Clear();
- List<string> groups = apiService.ListGroups();
- foreach (string group in groups)
- {
- cboGroups.Items.Add(new ListItem(group, group));
- }
- }
- private void btnRunAPI_Click(object sender, EventArgs e)
- {
- try
- {
- var apiIndexStart = cboAPIs.SelectedIndex;
- var apiIndexEnd = cboAPIs.SelectedIndex;
- var groupIndexStart = cboGroups.SelectedIndex;
- var groupIndexEnd = cboGroups.SelectedIndex;
- if (getAll)
- {
- apiIndexStart = 0;
- apiIndexEnd = cboAPIs.Items.Count - 1;
- groupIndexStart = 0;
- groupIndexEnd = cboGroups.Items.Count - 1;
- }
- for (var i = apiIndexStart; i <= apiIndexEnd; i++)
- {
- for (var j = groupIndexStart; j <= groupIndexEnd; j++)
- {
- Cursor.Current = Cursors.WaitCursor;
- // create new API service, passing in user name and password
- APIService apiService = new APIService(txtUsername.Text, txtPassword.Text);
- // get the selected inputs
- string apiToRun = ((ListItem)cboAPIs.Items[i]).Value;
- string groupName = ((ListItem)cboGroups.Items[j]).Name;
- // run the API
- string xml = apiService.RunAPI(apiToRun, groupName, dtDate.Value);
- WriteToCSV(apiToRun, groupName, dtDate.Value, xml);
- Cursor.Current = Cursors.Default;
- }
- }
- }
- catch (Exception exc)
- {
- Cursor.Current = Cursors.Default;
- MessageBox.Show("An error occurred running the API: " + exc.Message, "Portal API Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void WriteToCSV(string api, string group, DateTime date, string xml)
- {
- string exeFile = Application.ExecutablePath;
- string reportsPath = exeFile.Substring(0, exeFile.LastIndexOf(@"\")) + @"\Reports";
- if (!Directory.Exists(reportsPath))
- Directory.CreateDirectory(reportsPath);
- string filePathAndName = reportsPath + @"\" + api + "-" + group + "-" + dtDate.Value.ToString("yyyy-MM-dd") + ".csv";
- StringBuilder sb = new StringBuilder();
- const string dblQuote = "\"";
- // open up the results in an XML document
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(xml);
- // find the row tag
- XmlNode rootNode = doc.SelectSingleNode("//Data");
- // get the rows
- XmlNodeList rowNodes = rootNode.ChildNodes;
- if (rowNodes != null && rowNodes.Count > 0)
- {
- // get the column names from the first row
- XmlNode firstRow = rowNodes[0];
- bool firstCol = true;
- foreach (XmlNode col in firstRow.ChildNodes)
- {
- if (!firstCol)
- sb.Append(",");
- sb.Append(dblQuote + col.Name + dblQuote);
- firstCol = false;
- }
- sb.AppendLine();
- // now write out all the rows
- foreach (XmlNode row in rowNodes)
- {
- firstCol = true;
- foreach (XmlNode col in row.ChildNodes)
- {
- if (!firstCol)
- sb.Append(",");
- string innerText = col.InnerText;
- // ensure any double-quotes are escaped
- innerText = innerText.Replace(@"""", @"""""");
- sb.Append(dblQuote + innerText + dblQuote);
- firstCol = false;
- }
- sb.AppendLine();
- }
- // write out CSV file
- File.WriteAllText(filePathAndName, sb.ToString(), Encoding.UTF8);
- MessageBox.Show("Data written to file: " + "\r\n\r\n" + filePathAndName, "Portal API Client", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("No data found for the selected inputs", "Portal API Client", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- private void btnListGroupsAndAPIs_Click(object sender, EventArgs e)
- {
- // check that a username and password have been entered
- if (txtUsername.Text == "" || txtPassword.Text == "")
- {
- MessageBox.Show("Please enter a username and password to connect to Restaurant Partners", "Portal API Client", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- try
- {
- Cursor.Current = Cursors.WaitCursor;
- // create new API service, passing in user name and password
- APIService apiService = new APIService(txtUsername.Text, txtPassword.Text);
- // get the list of available APIs
- ListAPIs(apiService);
- // get the list of groups available for the logged-in user
- ListGroups(apiService);
- // default to the first API
- if (cboAPIs.Items.Count > 0)
- cboAPIs.SelectedIndex = 0;
- // default to the first group
- if (cboGroups.Items.Count > 0)
- cboGroups.SelectedIndex = 0;
- Cursor.Current = Cursors.Default;
- }
- catch (Exception exc)
- {
- Cursor.Current = Cursors.Default;
- MessageBox.Show("An error occurred listing groups/APIs: " + exc.Message, "Portal API Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement