Skip to content

Commit

Permalink
Type tokens in cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaughub committed Dec 11, 2020
1 parent 1ffa025 commit 39306da
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
2 changes: 2 additions & 0 deletions PostTripletex/Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private static async Task<string> GetSessionToken(Credentials credentials)

var response = await client.PutAsync<SingleValueResponse<AuthResponse>>(request);

if (response.Value == null) throw new Exception("Authentication failed");

return response.Value.Token;
}
}
Expand Down
1 change: 1 addition & 0 deletions PostTripletex/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand Down
31 changes: 31 additions & 0 deletions PostTripletex/FileDoc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace PostTripletex
{
Expand Down Expand Up @@ -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);
}
}
}
}
52 changes: 38 additions & 14 deletions PostTripletex/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using PostTripletex.Model;

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;

Expand All @@ -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();
Expand All @@ -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");
}
}
}
}
}

0 comments on commit 39306da

Please sign in to comment.