Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(samples)print request response #13

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 2 additions & 3 deletions src/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
public class Api
{
public string ApiFamily { get; set; }
public string ApiFunctionCall { get; set; }
}


public string ApiFunctionCall { get; set; }
}
}
37 changes: 18 additions & 19 deletions src/SampleCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public static void Main(string[] args)
// initializing logger object
// LogManager.DisableLogging();
logger = LogManager.GetCurrentClassLogger();
logger.Trace("\n");
logger.Trace("PROGRAM EXECUTION BEGINS");

// Set Network Settings (To Avoid SSL/TLS Secure Channel Error)
SetNetworkSettings();
Expand All @@ -56,16 +54,13 @@ public static void Main(string[] args)

// Run the Sample Code as per user input
RunSample();

logger.Trace("PROGRAM EXECUTION ENDS");
}

public static void RunSample(string cmdLineArg = null)
{
try
{
_sampleToRun = string.IsNullOrEmpty(cmdLineArg) ? Console.ReadLine() : cmdLineArg;
logger.Trace($"Input provided for Sample Code to Run: {_sampleToRun}");

Console.WriteLine("\n");
Type className = null;
Expand All @@ -76,38 +71,47 @@ public static void RunSample(string cmdLineArg = null)

if (className != null)
{
logger.Trace($"Sample Code found in the namespace: {path}");
break;
}
}

// Sample Code not found in the project files
if (className == null)
{
logger.Warn("No Sample Code Found with the name: {0}", _sampleToRun);
Console.WriteLine("No Sample Code Found with the name: {0}", _sampleToRun);

// Holding the full display of sample codes to show the response of current action
Console.WriteLine("Press any key to continue...");
Console.ReadLine();

// Running the App from visual studio or from command line without passing any args
if (cmdLineArg == null)
{
ShowMethods();
RunSample();
}

return;

}

// Sample Code is found in the project files, invoking it...
var obj = Activator.CreateInstance(className);
var methodInfo = className.GetMethod("Run");
if (methodInfo != null)
{
logger.Trace($"Invoking Run() method of {_sampleToRun}");
methodInfo.Invoke(obj, null);
}
else
{
logger.Warn($"No Run Method Found in the class: {_sampleToRun}");
Console.WriteLine("No Run Method Found in the class: {0}", _sampleToRun);

// Holding the full display of sample codes to show the response of current action
Console.WriteLine("Press any key to continue...");
Console.ReadLine();

// Running the App from visual studio or from command line without passing any args
if (cmdLineArg == null)
{
ShowMethods();
Expand All @@ -130,6 +134,12 @@ public static void RunSample(string cmdLineArg = null)
Console.WriteLine(e.StackTrace);
}

// Holding the full display of sample codes to show the response of current action
Console.WriteLine("Press any key to continue...");
Console.ReadLine();

// Previous Sample Code executed successfully. Displaying the sample codes again for the next run
// Running the App from visual studio or from command line without passing any args
if (cmdLineArg == null)
{
ShowMethods();
Expand All @@ -139,8 +149,6 @@ public static void RunSample(string cmdLineArg = null)

private static void ShowMethods()
{
logger.Trace("Beginning to Show All Sample Codes on Console");

Console.WriteLine(" ---------------------------------------------------------------------------------------------------");
Console.WriteLine(" - Code Sample Names -");
Console.WriteLine(" ---------------------------------------------------------------------------------------------------");
Expand All @@ -154,8 +162,6 @@ private static void ShowMethods()

foreach (var apiFamily in apiFamilies)
{
logger.Trace($"Showing Sample Codes for Api Family: {apiFamily}");

Console.WriteLine(" " + apiFamily.ToUpper() + " API'S ");
Console.WriteLine(" ---------------------------------------------------------------------------------------------------");

Expand Down Expand Up @@ -183,7 +189,6 @@ private static void ShowMethods()
Console.WriteLine(" ---------------------------------------------------------------------------------------------------");
}

logger.Trace("All Sample Codes Shown on Console");
Console.WriteLine(string.Empty);
Console.Write("Type a sample name & then press <Return> : ");
}
Expand All @@ -202,8 +207,6 @@ 4. Add the Api Object to the ApiList List.
*/

// 1. Find the Api Families (Folders inside the main 'Samples' Folder)
logger.Trace($"Samples Folder At:{Path.GetFullPath(PathOfSamplesFolder)}");

var dirList = Directory.GetDirectories(PathOfSamplesFolder, "*");
var apiFamilies = new List<string>();

Expand All @@ -221,9 +224,6 @@ 4. Add the Api Object to the ApiList List.
// 2.Fetch all the Files Paths inside Api Family folder (and all of its subfoldes)
var allfiles = Directory.GetFileSystemEntries(PathOfSamplesFolder + @"\" + apiFamily, "*.cs*", SearchOption.AllDirectories);

logger.Trace($"Api Family: {apiFamily}");
logger.Trace($"Total Sample Codes Detected: {allfiles.Count()}");

foreach (var file in allfiles)
{
var lastBackSlashIndex = file.LastIndexOf(@"\", StringComparison.Ordinal);
Expand Down Expand Up @@ -275,7 +275,6 @@ private static void InitializeSampleClassesPathList()
{
// dirList has got all the folders and sub-folders for the Samples Folder Path
var dirList = Directory.GetDirectories(PathOfSamplesFolder, "*", SearchOption.AllDirectories);
logger.Trace($"Project Namespace value provided: {ProjectNamespace}");

foreach (var dir in dirList)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SampleCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
56 changes: 51 additions & 5 deletions src/Samples/Flex/CoreServices/GenerateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,69 @@ public class GenerateKey
{
public static FlexV1KeysPost200Response Run()
{
Console.WriteLine($"\n[BEGIN] EXECUTION OF SAMPLE CODE: {nameof(GenerateKey)}");

CyberSource.Client.Configuration clientConfig = null;
FlexV1KeysPost200Response result = null;

var requestObj = new GeneratePublicKeyRequest("None");

try
{
var configDictionary = new Configuration().GetConfiguration();
var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary);
clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary);
var apiInstance = new KeyGenerationApi(clientConfig);

var result = apiInstance.GeneratePublicKey(requestObj);
Console.WriteLine(result);
result = apiInstance.GeneratePublicKey(requestObj);
return result;
}
catch (Exception e)
{
Console.WriteLine("Exception on calling the API: " + e.Message);
Console.WriteLine($"\nException on calling the Sample Code({nameof(GenerateKey)}):{e.Message}");
return null;
}
finally
{
if (clientConfig != null)
{
// PRINTING REQUEST DETAILS
if (clientConfig.ApiClient.Configuration.RequestHeaders != null)
{
Console.WriteLine("\nAPI REQUEST HEADERS:");
foreach (var requestHeader in clientConfig.ApiClient.Configuration.RequestHeaders)
{
Console.WriteLine(requestHeader);
}
}

if (!string.IsNullOrEmpty(clientConfig.ApiClient.Configuration.RequestBody))
{
Console.WriteLine("\nAPI REQUEST BODY:");
Console.WriteLine(clientConfig.ApiClient.Configuration.RequestBody);
}

// PRINTING RESPONSE DETAILS
if (clientConfig.ApiClient.ApiResponse != null)
{
if (!string.IsNullOrEmpty(clientConfig.ApiClient.ApiResponse.StatusCode.ToString()))
{
Console.WriteLine($"\nAPI RESPONSE CODE: {clientConfig.ApiClient.ApiResponse.StatusCode}");
}

Console.WriteLine("\nAPI RESPONSE HEADERS:");

foreach (var responseHeader in clientConfig.ApiClient.ApiResponse.HeadersList)
{
Console.WriteLine(responseHeader);
}

Console.WriteLine("\nAPI RESPONSE BODY:");
Console.WriteLine(clientConfig.ApiClient.ApiResponse.Data);
}

Console.WriteLine($"\n[END] EXECUTION OF SAMPLE CODE: {nameof(GenerateKey)}");
}
}
}
}
}
}
58 changes: 52 additions & 6 deletions src/Samples/Flex/CoreServices/TokenizeCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class TokenizeCard
{
public static void Run()
{
Console.WriteLine($"\n[BEGIN] EXECUTION OF SAMPLE CODE: {nameof(TokenizeCard)}");

CyberSource.Client.Configuration clientConfig = null;
FlexV1TokensPost200Response result = null;
bool tokenVerificationResult = false;

var generateKeyResult = GenerateKey.Run();
var keyId = generateKeyResult.KeyId;
var derFormat = generateKeyResult.Der.Format;
Expand All @@ -35,11 +41,10 @@ public static void Run()
try
{
var configDictionary = new Configuration().GetConfiguration();
var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary);
clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary);
var apiInstance = new FlexTokenApi(clientConfig);

var result = apiInstance.Tokenize(requestObj);
Console.WriteLine(result);
result = apiInstance.Tokenize(requestObj);

var flexPublicKey = new FlexPublicKey(keyId, new FlexServerSDK.Model.DerPublicKey(derFormat, derAlgo, derPublicKey), null);
var flexToken = new FlexToken()
Expand All @@ -63,12 +68,53 @@ public static void Run()
postParameters["token"] = flexToken.token;
postParameters["timestamp"] = Convert.ToString(flexToken.timestamp);

var tokenVerificationResult = Verify(flexPublicKey, postParameters);
Console.WriteLine(tokenVerificationResult);
tokenVerificationResult = Verify(flexPublicKey, postParameters);
}
catch (Exception e)
{
Console.WriteLine("Exception on calling the API: " + e.Message);
Console.WriteLine($"\nException on calling the Sample Code({nameof(TokenizeCard)}):{e.Message}");
}
finally
{
if (clientConfig != null)
{
// PRINTING REQUEST DETAILS
if (clientConfig.ApiClient.Configuration.RequestHeaders != null)
{
Console.WriteLine("\nAPI REQUEST HEADERS:");
foreach (var requestHeader in clientConfig.ApiClient.Configuration.RequestHeaders)
{
Console.WriteLine(requestHeader);
}
}

if (!string.IsNullOrEmpty(clientConfig.ApiClient.Configuration.RequestBody))
{
Console.WriteLine("\nAPI REQUEST BODY:");
Console.WriteLine(clientConfig.ApiClient.Configuration.RequestBody);
}

// PRINTING RESPONSE DETAILS
if (clientConfig.ApiClient.ApiResponse != null)
{
if (!string.IsNullOrEmpty(clientConfig.ApiClient.ApiResponse.StatusCode.ToString()))
{
Console.WriteLine($"\nAPI RESPONSE CODE: {clientConfig.ApiClient.ApiResponse.StatusCode}");
}

Console.WriteLine("\nAPI RESPONSE HEADERS:");

foreach (var responseHeader in clientConfig.ApiClient.ApiResponse.HeadersList)
{
Console.WriteLine(responseHeader);
}

Console.WriteLine("\nAPI RESPONSE BODY:");
Console.WriteLine(clientConfig.ApiClient.ApiResponse.Data);
}

Console.WriteLine($"\n[END] EXECUTION OF SAMPLE CODE: {nameof(TokenizeCard)}");
}
}
}

Expand Down
Loading