Skip to content

Commit

Permalink
test(DemoApp): add GetJobHttpInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed May 24, 2023
1 parent d538cbc commit 15c2b5b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 18 deletions.
24 changes: 14 additions & 10 deletions src/ConsoleAppDemo/ConsoleAppDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7</TargetFramework>
<StartupObject>ConsoleAppDemo.Program</StartupObject>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7</TargetFramework>
<StartupObject>ConsoleAppDemo.Program</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PollinationSDK\PollinationSDK.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PollinationSDK\PollinationSDK.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions src/ConsoleAppDemo/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppDemo
{
public static class LogHelper
{
private static string logFolderName = "logs";
public static Serilog.Core.Logger Log { get; private set; }
//public static Serilog.Log Log = Serilog.Log;
/// <summary>
/// Setup Log.Logger
/// </summary>
/// <param name="logFileName">rename log file name, otherwise it uses log.txt</param>
public static void SetupLogger(string logFileName = default)
{
var file = string.IsNullOrEmpty(logFileName) ? "log.txt" : logFileName;
var logPath = Path.Combine(LogFolder, file);
Log = new LoggerConfiguration().
WriteTo.File(
logPath,
rollingInterval: RollingInterval.Hour,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 3
).CreateLogger();
Serilog.Log.Logger = Log;

//Log.Information($"Rhino: {Rhino.RhinoApp.ExeVersion}.{Rhino.RhinoApp.ExeServiceRelease} ({Rhino.RhinoApp.BuildDate})");
//Log.Information(Utility.GetAboutInfo());
}

public static string LogFolder
{
get
{
var userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var podir = Path.Combine(userAppData, "pollination");
if (!Directory.Exists(podir))
Directory.CreateDirectory(podir);

podir = Path.Combine(podir, logFolderName);
if (!Directory.Exists(podir))
Directory.CreateDirectory(podir);
return podir;
}
}

public static string GetTheLatestLog()
{
var folder = new DirectoryInfo(LogFolder);
if (!folder.Exists) return null;

var lastLog = folder.GetFiles().OrderBy(_ => _.LastWriteTime).LastOrDefault();
if (lastLog.Exists) return lastLog.FullName;
else return null;

}



}
}
78 changes: 70 additions & 8 deletions src/ConsoleAppDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using PollinationSDK;
using PollinationSDK.Wrapper;
using System.Threading;
using Serilog;

namespace ConsoleAppDemo
{
Expand All @@ -25,15 +26,17 @@ static void Main(string[] args)

public static void RunCloudTests()
{
Console.WriteLine("Press any key to sign in...");
Console.WriteLine("Press any key to sign in from the default browser...");
Console.ReadKey();


AuthHelper.SignInAsync(devEnv: false).Wait();

var me = Helper.CurrentUser;
Console.WriteLine($"You are: {me.Username}");

Console.WriteLine("What is the Job ID: (\"ProjectOwnerName\"/\"ProjectName\"/\"JobID\". Such as mingbo/demo/1b933dfb-xxxx-4c18-a463-0d273cf42c43)");
var jobString = Console.ReadLine();
GetJobHttpInfo(jobString);

//Console.WriteLine("--------------------Get recipes-------------------");
//var api = new RecipesApi();
Expand Down Expand Up @@ -63,9 +66,9 @@ public static void RunCloudTests()
//Console.WriteLine($"{rec.Source}/{rec.Metadata.Name}/{rec.Metadata.Tag}");


Console.WriteLine("--------------------Get a project-------------------");
var proj = Helper.GetAProject(me.Username, "demo");
Console.WriteLine($"Getting the project. \nFound this project {proj.Name} ID: {proj.Id}");
//Console.WriteLine("--------------------Get a project-------------------");
//var proj = Helper.GetAProject(me.Username, "demo");
//Console.WriteLine($"Getting the project. \nFound this project {proj.Name} ID: {proj.Id}");


//Console.WriteLine("--------------------Getting Recipe Params-------------------");
Expand Down Expand Up @@ -106,8 +109,8 @@ public static void RunCloudTests()

//CLOUD:mingbo/demo/1b933dfb-009c-4c18-a463-0d273cf42c43/results#OUT_daylight_factor

Console.WriteLine("--------------------get simulation assets-------------------");
DownloadAssets(proj);
//Console.WriteLine("--------------------get simulation assets-------------------");
//DownloadAssets(proj);


//var output2 = runApi.ListRunArtifacts(proj.Owner.Name, proj.Name, run.Id);
Expand All @@ -120,7 +123,7 @@ public static void RunCloudTests()
//}
//var res = runApi.QueryResults(proj.Owner.Name, proj.Name);

Console.WriteLine("Done downloading");
//Console.WriteLine("Done downloading");


//Console.WriteLine("--------------------Download simulation output-------------------");
Expand All @@ -143,6 +146,65 @@ public static void RunCloudTests()


//DownloadBigAssetTest();
Console.WriteLine("You can close the console now");
Console.ReadLine();

}

public static void GetJobHttpInfo(string jobString)
{

try
{
if (string.IsNullOrEmpty(jobString))
throw new ArgumentException("Invalid Job ID");
LogHelper.SetupLogger("logs.DemoApp.txt");
Helper.Logger = LogHelper.Log;

var me = Helper.CurrentUser;
PollinationSDK.Helper.Logger.Information($"You are: {me.Username}");

var texts = jobString.Split('/').Select(_ => _.Trim()).Where(_ => !string.IsNullOrEmpty(_)).ToList();

var projOwner = texts[0];
var projName = texts[1];
var jobID = texts[2];
Console.WriteLine($"=> Project Owner: {projOwner}");
Console.WriteLine($"=> Project Name: {projName}");
Console.WriteLine($"=> Job ID: {jobID}");
Console.WriteLine($"Log path: {LogHelper.GetTheLatestLog()}");

PollinationSDK.Helper.Logger.Information($"DemoApp: getting project [{projOwner}/{projName}].");
var projApi = new PollinationSDK.Api.ProjectsApi();
var proj = projApi.GetProject(projOwner, projName);
PollinationSDK.Helper.Logger.Information($"DemoApp: got the project [{proj.Owner.Name}/{proj.Name}].");
PollinationSDK.Helper.Logger.Information($"DemoApp: Got the Project: {Environment.NewLine}{proj.ToJson()}");
Console.WriteLine($"DemoApp: got the project [{proj.Owner.Name}/{proj.Name}].");

PollinationSDK.Helper.Logger.Information($"DemoApp: getting job [{proj.Owner.Name}/{proj.Name}/{jobID}].");
var api = new PollinationSDK.Api.JobsApi();
var jobHttp = api.GetJobWithHttpInfo(proj.Owner.Name, proj.Name, jobID.ToString());
PollinationSDK.Helper.Logger.Information($"DemoApp: got job with HTTP info:");

PollinationSDK.Helper.Logger.Information($"API Response StatusCode: {jobHttp?.StatusCode}.");
Console.WriteLine($"API Response StatusCode: {jobHttp?.StatusCode}.");
var headers = jobHttp.Headers.Select(_ => $"{_.Key}:{_.Value}");
foreach (var item in headers)
{
var msg = $"API Response Header: {item}.";
PollinationSDK.Helper.Logger.Information(msg);
Console.WriteLine(msg);
}

PollinationSDK.Helper.Logger.Information($"API Response Data: {jobHttp?.Data?.ToJson()}.");
Console.WriteLine($"API Response Data: {jobHttp?.Data?.ToJson()}.");

}
catch (Exception e)
{
PollinationSDK.Helper.Logger.Error(e, $"DemoApp");
Console.WriteLine($"Check the log file for the error.");
}

}

Expand Down

0 comments on commit 15c2b5b

Please sign in to comment.