Skip to content

Commit

Permalink
Refactor example to make it easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaumov committed Jan 25, 2022
1 parent 0667524 commit 29b12e6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 56 deletions.
61 changes: 5 additions & 56 deletions Acumatica REST API Console Application/RESTExample.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
using Acumatica.Auth.Api;
using Acumatica.Auth.Model;
using Acumatica.Default_20_200_001.Api;
using Acumatica.Default_20_200_001.Model;
using Acumatica.RESTClient.Client;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace AcumaticaRestApiExample
{
public class RESTExample
{
public static void ExampleMethod(string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
{
var authApi = new AuthApi(siteURL,
requestInterceptor: LogRequest, responseInterceptor: LogResponse);
var authApi = new AuthApi(siteURL,
requestInterceptor: RequestLogger.LogRequest, responseInterceptor: RequestLogger.LogResponse);

try
{
var configuration = authApi.LogIn(username, password, tenant, branch, locale);
Expand All @@ -36,11 +28,11 @@ public static void ExampleMethod(string siteURL, string username, string passwor
Console.WriteLine("Reading Sales Order by Keys...");
var salesOrderApi = new SalesOrderApi(configuration);
var order = salesOrderApi.GetByKeys(new List<string>() { "SO", "SO005207" });
Console.WriteLine("Order Total: "+order.OrderTotal.Value);
Console.WriteLine("Order Total: " + order.OrderTotal.Value);


var shipmentApi = new ShipmentApi(configuration);
var shipment= shipmentApi.GetByKeys(new List<string>() { "002805" });
var shipment = shipmentApi.GetByKeys(new List<string>() { "002805" });
Console.WriteLine("ConfirmShipment");
shipmentApi.WaitActionCompletion(shipmentApi.InvokeAction(new ConfirmShipment(shipment)));

Expand All @@ -58,48 +50,5 @@ public static void ExampleMethod(string siteURL, string username, string passwor
Console.WriteLine("Logged Out...");
}
}
private const string RequestsLogPath = "RequestsLog.txt";
private static void LogResponse(RestRequest request, RestResponse response, RestClient restClient)
{
StreamWriter writer = new StreamWriter(RequestsLogPath, true);
writer.WriteLine(DateTime.Now.ToString());
writer.WriteLine("Response");
writer.WriteLine("\tStatus code: " + response.StatusCode);
writer.WriteLine("\tContent: " + response.Content);
writer.WriteLine("-----------------------------------------");
writer.WriteLine();
writer.Flush();
writer.Close();

}

private static void LogRequest(RestRequest request, RestClient restClient)
{
StreamWriter writer = new StreamWriter(RequestsLogPath, true);
writer.WriteLine(DateTime.Now.ToString());
writer.WriteLine("Request");
writer.WriteLine("\tMethod: " + request.Method);
string parameters = "";
string body = "";
foreach (var parametr in request.Parameters)
{
if (parametr.Type == ParameterType.QueryString)
{
parameters += String.IsNullOrEmpty(parameters) ? "?" : "&";
parameters += parametr.Name + "=" + parametr.Value;
}

if (parametr.Type == ParameterType.RequestBody)
body += parametr.Value;
}

writer.WriteLine("\tURL: " + restClient.BuildUri(request) + parameters);
if (!String.IsNullOrEmpty(body))
writer.WriteLine("\tBody: " + body);
writer.WriteLine("-----------------------------------------");
writer.WriteLine();
writer.Flush();
writer.Close();
}
}
}
60 changes: 60 additions & 0 deletions Acumatica REST API Console Application/RequestLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using RestSharp;
using System;
using System.IO;

namespace AcumaticaRestApiExample
{
public static class RequestLogger
{
private const string RequestsLogPath = "RequestsLog.txt";

/// <summary>
/// Logs response to RequestsLog.txt file.
/// </summary>
public static void LogResponse(RestRequest request, RestResponse response, RestClient restClient)
{
StreamWriter writer = new StreamWriter(RequestsLogPath, true);
writer.WriteLine(DateTime.Now.ToString());
writer.WriteLine("Response");
writer.WriteLine("\tStatus code: " + response.StatusCode);
writer.WriteLine("\tContent: " + response.Content);
writer.WriteLine("-----------------------------------------");
writer.WriteLine();
writer.Flush();
writer.Close();

}

/// <summary>
/// Logs request to RequestsLog.txt file.
/// </summary>
public static void LogRequest(RestRequest request, RestClient restClient)
{
StreamWriter writer = new StreamWriter(RequestsLogPath, true);
writer.WriteLine(DateTime.Now.ToString());
writer.WriteLine("Request");
writer.WriteLine("\tMethod: " + request.Method);
string parameters = "";
string body = "";
foreach (var parametr in request.Parameters)
{
if (parametr.Type == ParameterType.QueryString)
{
parameters += String.IsNullOrEmpty(parameters) ? "?" : "&";
parameters += parametr.Name + "=" + parametr.Value;
}

if (parametr.Type == ParameterType.RequestBody)
body += parametr.Value;
}

writer.WriteLine("\tURL: " + restClient.BuildUri(request) + parameters);
if (!String.IsNullOrEmpty(body))
writer.WriteLine("\tBody: " + body);
writer.WriteLine("-----------------------------------------");
writer.WriteLine();
writer.Flush();
writer.Close();
}
}
}

0 comments on commit 29b12e6

Please sign in to comment.