/// <summary> /// Gets a list of supported time zones and, if the supplied timeZoneId is not in the list, prompts the user to input a supported time zone until one from the list is entered. Returns the valid time zone ID. /// </summary> /// <param name="myGeotabApi">An authenticated myGeotabApi (<see cref="API"/>) object.</param> /// <param name="timeZoneId">The time zone ID to be validated.</param> /// <returns>A valid time zone ID.</returns> public static async Task <string> ValidateTimeZoneIdAsync(API myGeotabApi, string timeZoneId) { ConsoleUtility.LogInfoStart($"Validating input time zone Id ('{timeZoneId}')..."); List <Geotab.Checkmate.ObjectModel.TimeZoneInfo> supportedTimeZones = await myGeotabApi.CallAsync <List <Geotab.Checkmate.ObjectModel.TimeZoneInfo> >("GetTimeZones"); if (supportedTimeZones.Where(supportedTimeZone => timeZoneId.Equals(supportedTimeZone.Id)).Any()) { ConsoleUtility.LogComplete(); return(timeZoneId); } ConsoleUtility.LogError($"The specified time zone Id '{timeZoneId}' is not supported. Following is a list of supported time zone IDs:"); foreach (Geotab.Checkmate.ObjectModel.TimeZoneInfo timeZone in supportedTimeZones) { ConsoleUtility.LogListItem(timeZone.Id); } bool tryAgain = true; string proposedTimeZoneId = timeZoneId; while (tryAgain) { ConsoleUtility.LogError($"The specified time zone Id '{proposedTimeZoneId}' is not supported."); proposedTimeZoneId = ConsoleUtility.GetUserInput($"a supported time zone Id"); tryAgain = !supportedTimeZones.Where(supportedTimeZone => proposedTimeZoneId.Equals(supportedTimeZone.Id)).Any(); } return(proposedTimeZoneId); }
/// <summary> /// Creates a new MyGeotab database. /// </summary> /// <param name="myGeotabApi">An authenticated MyGeotab <see cref="API"/> object.</param> /// <param name="database">The database name (short company name). Spaces and non alphanumeric characters will be converted to the underscore character. Maximum 58 characters.</param> /// <param name="userName">The database administrator email address.</param> /// <param name="password">The database administrator password.</param> /// <param name="companyName">The company name.</param> /// <param name="firstName">The account administrator's first name.</param> /// <param name="lastName">The account administrator's last name.</param> /// <param name="phoneNumber">The company phone number.</param> /// <param name="resellerName">The reseller name.</param> /// <param name="fleetSize">The number of vehicles in the company fleet.</param> /// <param name="signUpForNews">A value indicating whether sign-up to receive news about new telematics products, events and promotions.</param> /// <param name="timeZoneId">The IANA time zone Id of the device used to determine local work times. This is typically the "home location" of the admin user. Retrieve a list of valid timezone Id's by querying "GetTimeZones".</param> /// <returns>A string with the direct server the database was created on and database name. Ex. "my0.geotab.com/abc_company".</returns> public static async Task <string> CreateDatabaseAsync(API myGeotabApi, string database, string userName, string password , string companyName, string firstName, string lastName, string phoneNumber , string resellerName, int fleetSize, bool signUpForNews, string timeZoneId) { ConsoleUtility.LogInfoStartMultiPart($"Creating customer database named '{database}'.", $"THIS MAY TAKE UP TO 10 MINUTES...", ConsoleColor.Magenta); // Set timeout to 10 minutes. myGeotabApi.Timeout = 600000; string result = await myGeotabApi.CallAsync <string>("CreateDatabase", new { database, userName, password, companyDetails = new { companyName, firstName, lastName, phoneNumber, resellerName, fleetSize, comments = "", signUpForNews, timeZoneId } }); ConsoleUtility.LogComplete(); ConsoleUtility.LogInfo($"Created database: {result}"); return(result); }
/// <summary> /// Requests user's MyAdmin credentials and authenticates. Returns: /// <list type="bullet"> /// <item> /// <term>myAdminApi</term> /// <description>>The authenticated MyAdmin API (<see cref="MyAdminInvoker"/>) object.</description> /// </item> /// <item> /// <term>myAdminApiUser</term> /// <description>The <see cref="ApiUser"/> object containing the API key and Session ID required for subsequent API calls.</description> /// </item> /// <item> /// <term>myAdminUsername</term> /// <description>The user-entered username.</description> /// </item> /// <item> /// <term>myAdminPassword</term> /// <description>The user-entered password.</description> /// </item> /// </list> /// </summary> /// <returns></returns> public static async Task <(MyAdminInvoker myAdminApi, ApiUser myAdminApiUser, string myAdminUsername, string myAdminPassword)> AuthenticateMyAdminApi() { var myAdminUsername = ConsoleUtility.GetUserInput("MyAdmin Username"); var myAdminPassword = ConsoleUtility.GetUserInputMasked("MyAdmin Password"); var myAdminApi = new MyAdminInvoker("https://myadminapi.geotab.com/v2/MyAdminApi.ashx", 60000); Dictionary <string, object> parameters = new() { { "username", myAdminUsername }, { "password", myAdminPassword } };
/// <summary> /// Authenticates and returns the authenticated MyGeotab <see cref="API"/> object required for subsequent API calls. /// </summary> /// <param name="server">The MyGeotab server to authenticate against.</param> /// <param name="database">The MyGeotab database to authenticate against.</param> /// <param name="userName">The MyGeotab username.</param> /// <param name="password">The MyGeotab password.</param> /// <returns>>An authenticated MyGeotab <see cref="API"/> object.</returns> public static async Task <API> AuthenticateMyGeotabApiAsync(string server, string database, string userName, string password) { API myGeotabApi = new(userName, password, null, database, server); ConsoleUtility.LogInfoStart($"Authenticating MyGeotab API (User: '******', Database: '{myGeotabApi.Database}', Server: '{myGeotabApi.Server}')..."); await myGeotabApi.AuthenticateAsync(); ConsoleUtility.LogComplete(); return(myGeotabApi); }
/// <summary> /// Requests user's MyGeotab credentials, authenticates and returns the authenticated MyGeotab <see cref="API"/> object required for subsequent API calls. /// </summary> /// <param name="server">The MyGeotab server to authenticate against.</param> /// <param name="database">The MyGeotab database to authenticate against.</param> /// <returns>An authenticated MyGeotab <see cref="API"/> object.</returns> public static async Task <API> AuthenticateMyGeotabApiAsync(string server, string database) { string userName = ConsoleUtility.GetUserInput($"MyGeotab username for '{database}' database"); string password = ConsoleUtility.GetUserInputMasked($"MyGeotab password for '{database}' database"); API myGeotabApi = new(userName, password, null, database, server); ConsoleUtility.LogInfoStart($"Authenticating MyGeotab API (User: '******', Database: '{myGeotabApi.Database}', Server: '{myGeotabApi.Server}')..."); await myGeotabApi.AuthenticateAsync(); ConsoleUtility.LogComplete(); return(myGeotabApi); }
/// <summary> /// Prompts the user to input a file path followed by a file name until valid entries are made. The validated full path is returned. /// </summary> /// <param name="fileTypeDescription">A description of the file type being sought (e.g. '<c>config</c>'). For use in user prompts.</param> /// <returns>The validated full path.</returns> public static string GetUserInputFilePath(string fileTypeDescription) { string filePath = string.Empty; string fileFullPath = string.Empty; bool filePathIsValid = false; bool fileNameIsValid = false; // Get the user to enter a valid directory path. while (!filePathIsValid) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write($"> Enter {fileTypeDescription} folder (e.g. 'C:\\Temp'):"); Console.ForegroundColor = ConsoleColor.Cyan; filePath = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.White; if (Directory.Exists(filePath)) { filePathIsValid = true; } else { ConsoleUtility.LogError($"The folder entered does not exist."); } } // Get the use to enter a valid filename. while (!fileNameIsValid) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write($"> Enter {fileTypeDescription} file name (e.g. 'FileName.csv'):"); Console.ForegroundColor = ConsoleColor.Cyan; string fileName = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.White; fileFullPath = Path.Combine(filePath, fileName); if (File.Exists(fileFullPath)) { fileNameIsValid = true; } else { ConsoleUtility.LogError($"The file '{fileName}' does not exist in folder '{filePath}'."); } } return(fileFullPath); }
/// <summary> /// Prompts the user to input the path to a config file and then loads the contents of the config file into a list of <see cref="ConfigItem"/> objects. /// </summary> /// <param name="configFilePathPromptMessage">A description of the file type being sought (e.g. '<c>config</c>'). For use in user prompts.</param> /// <returns>A list of <see cref="ConfigItem"/> objects.</returns> public static IList <ConfigItem> GetConfigItems(string configFilePathPromptMessage) { // Get the config file path. string configFilePath = ConsoleUtility.GetUserInputFilePath(configFilePathPromptMessage); // Load the config file contents. ConsoleUtility.LogInfoStart($"Loading configuration information from file '{configFilePath}'..."); IList <ConfigItem> configItems = null; using (FileStream configFile = File.OpenRead(configFilePath)) { configItems = configFile.CsvToList <ConfigItem>(); } if (!configItems.Any()) { throw new Exception($"No configuration information was loaded from the CSV file '{configFilePath}'."); } ConsoleUtility.LogComplete(); return(configItems); }
/// <summary> /// Validate a user has groups assigned and does not exist. /// </summary> /// <param name="user">The user to validate.</param> /// <param name="existingUsers">IList of existing users.</param> /// <returns>True if user is valid otherwise False.</returns> public static bool ValidateUser(User user, IList <User> existingUsers) { ConsoleUtility.LogInfoStart($"Validating user '{user.Name}'..."); if (user.CompanyGroups == null || user.CompanyGroups.Count == 0) { ConsoleUtility.LogWarning($"Invalid user: {user.Name}. Must have organization groups."); return(false); } if (user.SecurityGroups == null || user.SecurityGroups.Count == 0) { ConsoleUtility.LogWarning($"Invalid user: {user.Name}. Must have security groups."); return(false); } if (UserExists(user.Name, existingUsers)) { ConsoleUtility.LogWarning($"Invalid user: {user.Name}. User already exists."); return(false); } ConsoleUtility.LogComplete(); return(true); }