Skip to content

Commit 067b70d

Browse files
authored
Add files via upload
1 parent c102fd9 commit 067b70d

10 files changed

+13494
-0
lines changed

HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections/ECGridOSClient.cs

Lines changed: 10222 additions & 0 deletions
Large diffs are not rendered by default.

HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections/ECGridOS_Classes.cs

Lines changed: 2482 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>HTTP_ECGridOS_API_v4._1_.NET_6_Console_App_ServiceCollections</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace HTTP_ECGridOS_API_v4._1_.NET_6_Console_App_ServiceCollections;
8+
9+
internal interface IManageEDI
10+
{
11+
Task<string> CheckECGridOSIsRunning();
12+
13+
Task<string> CheckECGridOSSession(string APIKey);
14+
15+
Task<int> DownloadFiles(string APIKey);
16+
17+
Task<int> UploadloadFile(string APIKey);
18+
}
19+
20+
internal class ManageEDI : IManageEDI
21+
{
22+
#region Class Variables
23+
24+
private readonly IECGridOSClient _ECGridOSClient;
25+
26+
#endregion
27+
28+
#region Constructor
29+
30+
public ManageEDI(IECGridOSClient ECGridOSClient)
31+
{
32+
_ECGridOSClient = ECGridOSClient;
33+
}
34+
35+
#endregion
36+
37+
public async Task<string> CheckECGridOSIsRunning()
38+
{
39+
// Check if the EGridOS API is running
40+
string ecgridos_version = await _ECGridOSClient.Version();
41+
if (!string.IsNullOrEmpty(ecgridos_version))
42+
{
43+
return $"ECGridOS API is Running: {ecgridos_version}";
44+
}
45+
46+
return String.Empty;
47+
}
48+
49+
public async Task<string> CheckECGridOSSession(string APIKey)
50+
{
51+
// Check that My API Key can access a persistant session.
52+
SessionInfo sessionInfo = await _ECGridOSClient.WhoAmI(APIKey);
53+
if (!string.IsNullOrEmpty(sessionInfo.SessionID))
54+
{
55+
Console.WriteLine($"ECGridOS API has a persistant Session ID: {sessionInfo.SessionID}");
56+
}
57+
58+
return String.Empty;
59+
}
60+
61+
public async Task<int> DownloadFiles(string APIKey)
62+
{
63+
int downloadedFiles = 0;
64+
65+
// Check if there are any Inbound Files to Download
66+
ParcelIDInfoCollection parcelPending = await _ECGridOSClient.ParcelInBox(APIKey);
67+
68+
// Check for Pending Documents to Download
69+
if (parcelPending != null && parcelPending.TotalRecords > 0)
70+
{
71+
// Do For Each File
72+
foreach (ParcelIDInfo parcel2Download in parcelPending.ParcelIDInfoList)
73+
{
74+
long parcelID = parcel2Download.ParcelID;
75+
76+
// Download the File
77+
FileInfo file2Save = await _ECGridOSClient.ParcelDownload(APIKey, parcelID);
78+
if (file2Save != null)
79+
{
80+
// Save Base64 String
81+
string FileAsBase64 = file2Save.ContentBase64String;
82+
83+
// Make the Filename
84+
string fileName = Path.Combine("c:/temp/download", file2Save.FileName);
85+
86+
// Write to File however you want
87+
File.WriteAllBytes(fileName, file2Save.Content);
88+
89+
// File was written
90+
if (File.Exists(fileName))
91+
{
92+
// Confirm that the File was Download and remove it from the Pending List on ECGridOS
93+
if (await _ECGridOSClient.ParcelDownloadConfirm(APIKey, parcelID))
94+
{
95+
Console.WriteLine($"ECGridOS API File Downloaded and written to disk: {fileName}");
96+
downloadedFiles++;
97+
}
98+
}
99+
}
100+
}
101+
}
102+
103+
return downloadedFiles;
104+
}
105+
106+
public async Task<int> UploadloadFile(string APIKey)
107+
{
108+
// Upload File to ECGridOS API
109+
string file2Upload = Path.Combine("c:/temp/upload", "Upload.edi");
110+
string FilenameNoPath = Path.GetFileName(file2Upload);
111+
byte[] contents2Upload = File.ReadAllBytes(file2Upload);
112+
113+
// Upload A File
114+
int returnedParcelID = await _ECGridOSClient.ParcelUpload(APIKey, FilenameNoPath, contents2Upload.Length, contents2Upload);
115+
116+
// Check the File Was Uploaded
117+
if (returnedParcelID > 0)
118+
{
119+
Console.WriteLine($"ECGridOS API File Uploaded: {FilenameNoPath}");
120+
return returnedParcelID;
121+
}
122+
123+
return -1;
124+
}
125+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* ECGridOS API Console App Example for .NET 6 using Service Collections
3+
* Copyright: Loren Data Corp.
4+
* Last Updated: 09/28/2022
5+
* Author: Greg Kolinski
6+
* Description: Example Calls to provide users with a basic example of using the ECGridOS API Service with .NET 6 Projects
7+
* Examples are not production ready and you will need to impliment your own best practices.
8+
* Provided as Example only
9+
*
10+
* Only Covers Connecting Via HTTP in a ServiceCollection
11+
*
12+
* Example uses Nuget Packages
13+
* Microsoft.Extensions.DependencyInjection
14+
* Microsoft.Extensions.Http
15+
*
16+
*/
17+
18+
using HTTP_ECGridOS_API_v4._1_.NET_6_Console_App_ServiceCollections;
19+
20+
using Microsoft.Extensions.DependencyInjection;
21+
22+
// See https://aka.ms/new-console-template for more information
23+
Console.WriteLine("Hello, World!");
24+
25+
// Use the SecretManager or keep/reference the APIKey somewhere secure, we do not recommend having the key in your source code.
26+
string MyAPIKey = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
27+
28+
var services = new ServiceCollection();
29+
services.AddScoped<IManageEDI, ManageEDI>();
30+
services.AddHttpClient<IECGridOSClient, ECGridOSClient>(client =>
31+
{
32+
client.BaseAddress = new Uri("https://os.ecgrid.io/v4.1/prod/ECGridOS.asmx/");
33+
client.Timeout = TimeSpan.FromMinutes(10);
34+
}).SetHandlerLifetime(TimeSpan.FromMinutes(720)); // Reuse connection pool for 12 hour before getting a new one
35+
36+
using var servicesProvider = services.BuildServiceProvider();
37+
38+
var ediManager = servicesProvider.GetRequiredService<IManageEDI>();
39+
40+
// Check if the EGridOS API is running
41+
Console.WriteLine(ediManager.CheckECGridOSIsRunning().Result);
42+
43+
// Check that My API Key can access a persistant session.
44+
Console.WriteLine(ediManager.CheckECGridOSSession(MyAPIKey).Result);
45+
46+
int downloadedFileCount = ediManager.DownloadFiles(MyAPIKey).Result;
47+
48+
int uploadFileParcelID = ediManager.UploadloadFile(MyAPIKey).Result;
49+
50+
Console.ReadKey();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"format": 1,
3+
"restore": {
4+
"F:\\LD_Code\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections.csproj": {}
5+
},
6+
"projects": {
7+
"F:\\LD_Code\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections.csproj": {
8+
"version": "1.0.0",
9+
"restore": {
10+
"projectUniqueName": "F:\\LD_Code\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections.csproj",
11+
"projectName": "HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections",
12+
"projectPath": "F:\\LD_Code\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections.csproj",
13+
"packagesPath": "C:\\Users\\gkolinski\\.nuget\\packages\\",
14+
"outputPath": "F:\\LD_Code\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\HTTP ECGridOS API v4.1 .NET 6 Console App ServiceCollections\\obj\\",
15+
"projectStyle": "PackageReference",
16+
"fallbackFolders": [
17+
"C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
18+
],
19+
"configFilePaths": [
20+
"C:\\Users\\gkolinski\\AppData\\Roaming\\NuGet\\NuGet.Config",
21+
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
22+
],
23+
"originalTargetFrameworks": [
24+
"net6.0"
25+
],
26+
"sources": {
27+
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
28+
"https://api.nuget.org/v3/index.json": {}
29+
},
30+
"frameworks": {
31+
"net6.0": {
32+
"targetAlias": "net6.0",
33+
"projectReferences": {}
34+
}
35+
},
36+
"warningProperties": {
37+
"warnAsError": [
38+
"NU1605"
39+
]
40+
}
41+
},
42+
"frameworks": {
43+
"net6.0": {
44+
"targetAlias": "net6.0",
45+
"dependencies": {
46+
"Microsoft.Extensions.DependencyInjection": {
47+
"target": "Package",
48+
"version": "[6.0.0, )"
49+
},
50+
"Microsoft.Extensions.Http": {
51+
"target": "Package",
52+
"version": "[6.0.0, )"
53+
}
54+
},
55+
"imports": [
56+
"net461",
57+
"net462",
58+
"net47",
59+
"net471",
60+
"net472",
61+
"net48",
62+
"net481"
63+
],
64+
"assetTargetFallback": true,
65+
"warn": true,
66+
"frameworkReferences": {
67+
"Microsoft.NETCore.App": {
68+
"privateAssets": "all"
69+
}
70+
},
71+
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\6.0.400\\RuntimeIdentifierGraph.json"
72+
}
73+
}
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
4+
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
5+
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
6+
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
7+
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
8+
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\gkolinski\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder</NuGetPackageFolders>
9+
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
10+
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.3.0</NuGetToolVersion>
11+
</PropertyGroup>
12+
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
13+
<SourceRoot Include="C:\Users\gkolinski\.nuget\packages\" />
14+
<SourceRoot Include="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\" />
15+
</ItemGroup>
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

0 commit comments

Comments
 (0)