forked from Acumatica/AcumaticaRESTAPIClientForCSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor example to make it easier to read
- Loading branch information
dnaumov
committed
Jan 25, 2022
1 parent
0667524
commit 29b12e6
Showing
2 changed files
with
65 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |