From 39306da239e29f65f8c38c13ce587ec5d6d54d22 Mon Sep 17 00:00:00 2001 From: Vaughub Date: Fri, 11 Dec 2020 13:44:40 +0100 Subject: [PATCH] Type tokens in cmd --- PostTripletex/Authentication.cs | 2 ++ PostTripletex/Command.cs | 1 + PostTripletex/FileDoc.cs | 31 ++++++++++++++++++++ PostTripletex/Program.cs | 52 ++++++++++++++++++++++++--------- 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/PostTripletex/Authentication.cs b/PostTripletex/Authentication.cs index 33b3d74..d6dc310 100644 --- a/PostTripletex/Authentication.cs +++ b/PostTripletex/Authentication.cs @@ -35,6 +35,8 @@ private static async Task GetSessionToken(Credentials credentials) var response = await client.PutAsync>(request); + if (response.Value == null) throw new Exception("Authentication failed"); + return response.Value.Token; } } diff --git a/PostTripletex/Command.cs b/PostTripletex/Command.cs index b001aec..3753cde 100644 --- a/PostTripletex/Command.cs +++ b/PostTripletex/Command.cs @@ -10,6 +10,7 @@ public static void Welcome() Console.WriteLine(" Commands:"); Console.WriteLine(" q - quit program"); Console.WriteLine(" sync - sync to local file"); + Console.WriteLine(" token - delete tokens"); Console.WriteLine(" post - post to Tripletex"); Console.WriteLine(" del - delete from Tripletex"); Console.WriteLine(" Options:"); diff --git a/PostTripletex/FileDoc.cs b/PostTripletex/FileDoc.cs index 79514ae..5f3c901 100644 --- a/PostTripletex/FileDoc.cs +++ b/PostTripletex/FileDoc.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace PostTripletex { @@ -73,5 +74,35 @@ public static string GetNumber(string fileName) return i; } + + public static async Task GetTokens() + { + if (File.Exists("Tokens.txt")) + { + var tokens = await File.ReadAllLinesAsync("Tokens.txt"); + + await Authentication.CreateSessionToken(new Credentials(tokens[0], tokens[1])); + } + else + { + var tokens = new string[2]; + + Console.WriteLine("consumerToken"); + Console.Write("> "); + tokens[0] = Console.ReadLine()?.Trim(); + + Console.WriteLine(); + + Console.WriteLine("employeeToken"); + Console.Write("> "); + tokens[1] = Console.ReadLine()?.Trim(); + + Console.WriteLine(); + + await Authentication.CreateSessionToken(new Credentials(tokens[0], tokens[1])); + + File.WriteAllLines("Tokens.txt", tokens); + } + } } } diff --git a/PostTripletex/Program.cs b/PostTripletex/Program.cs index 0b3d15b..41449fc 100644 --- a/PostTripletex/Program.cs +++ b/PostTripletex/Program.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Linq; using System.Threading.Tasks; using PostTripletex.Model; @@ -6,18 +8,15 @@ namespace PostTripletex { class Program { - static async Task Main(string[] args) + static async Task Main() { - var consumerToken = ""; - var employeeToken = ""; - - await Authentication.CreateSessionToken(new Credentials(consumerToken, employeeToken)); + await Authenticate(); Command.Welcome(); while (true) { - var command = Console.ReadLine()?.Split(' '); + var command = Console.ReadLine()?.Split(' ').Select(s => s.ToLower()).ToArray(); if (command?[0] == "q") break; @@ -27,24 +26,32 @@ static async Task Main(string[] args) continue; } + if (command?[0] == "token") + { + File.Delete("Tokens.txt"); + Console.WriteLine("Tokens deleted\n"); + + await Authenticate(); + } + if (command?.Length != 3 || !int.TryParse(command[1], out var number)) { Command.Invalid(); continue; } - if (command[0].ToLower() == "post") + if (command[0] == "post") { - if (command[2].ToLower() == "p") await Post.Product(MakeEmployee(), number); - else if (command[2].ToLower() == "co") await Post.Contact(MakeEmployee(), number); - else if (command[2].ToLower() == "e") await Post.Employee(number); - else if (command[2].ToLower() == "cu") await Post.Customer(number); + if (command[2] == "p") await Post.Product(MakeEmployee(), number); + else if (command[2] == "co") await Post.Contact(MakeEmployee(), number); + else if (command[2] == "e") await Post.Employee(number); + else if (command[2] == "cu") await Post.Customer(number); else Command.Invalid(); } - else if (command[0].ToLower() == "del") + else if (command[0] == "del") { - if (command[2].ToLower() == "p") await Delete.Product(number); - else if (command[2].ToLower() == "cu") await Delete.Customer(number); + if (command[2] == "p") await Delete.Product(number); + else if (command[2] == "cu") await Delete.Customer(number); else Command.Invalid(); } else Command.Invalid(); @@ -60,5 +67,22 @@ private static Employee MakeEmployee() return employee; } + + private static async Task Authenticate() + { + while (true) + { + try + { + await FileDoc.GetTokens(); + + break; + } + catch (Exception e) + { + Console.WriteLine(e.Message + "\n"); + } + } + } } } \ No newline at end of file