Skip to content

Commit

Permalink
Improve Invoking Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaumov committed Jul 22, 2020
1 parent 69df04f commit c45a582
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 17 deletions.
6 changes: 6 additions & 0 deletions Acumatica REST API Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Acumatica.POS_17.200.001",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Acumatica.ISVCB_19.1", "Acumatica.ISVCB_19.1\Acumatica.ISVCB_19.1.csproj", "{18D5146E-4B20-4343-8A38-AD95EE15B797}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RESTClientTests", "RESTClientTests\RESTClientTests.csproj", "{C56AC405-E788-41DF-B530-D964B55625E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -71,6 +73,10 @@ Global
{18D5146E-4B20-4343-8A38-AD95EE15B797}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18D5146E-4B20-4343-8A38-AD95EE15B797}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18D5146E-4B20-4343-8A38-AD95EE15B797}.Release|Any CPU.Build.0 = Release|Any CPU
{C56AC405-E788-41DF-B530-D964B55625E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C56AC405-E788-41DF-B530-D964B55625E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C56AC405-E788-41DF-B530-D964B55625E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C56AC405-E788-41DF-B530-D964B55625E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
43 changes: 26 additions & 17 deletions Acumatica.RESTClient/Api/EntityAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,36 +324,43 @@ public async System.Threading.Tasks.Task DeleteByIdAsync(Guid? id)
#endregion

#region Implementation
private struct Location
public struct Location
{
public string Site;
public string Entity;
public string Status;
public string EndpointName;
public string EndpointVersion;
public string EntityName;
public string ActionName;
public string Status;
public string ID;
}
private Location ParseLocation(string location)
public static Location ParseLocation(string location)
{
string entityKeyword = "/entity/";
char[] pathSeparators = new char[] { '/' };

var result = new Location();

var parts = location.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
result.Site = parts[0];
result.Entity = parts[1];
result.EndpointName = parts[2];
result.EndpointVersion = parts[3];
result.EntityName = parts[4];
if (parts.Length == 6)
var parts = location.Split(pathSeparators, StringSplitOptions.RemoveEmptyEntries);
result.ID = parts[parts.Length - 1];

int indexOfEntity = location.IndexOf(entityKeyword, StringComparison.OrdinalIgnoreCase);
if (indexOfEntity < 0)
throw new Exception("Incorrect location");
if (location.Substring(indexOfEntity + entityKeyword.Length).IndexOf(entityKeyword, StringComparison.OrdinalIgnoreCase) >= 0)
throw new Exception("Location cannot be parsed as it contains more than 1 entity keyword");
result.Site = location.Substring(0, indexOfEntity);
string restOfLocation = location.Substring(indexOfEntity);
parts = restOfLocation.Split(pathSeparators, StringSplitOptions.RemoveEmptyEntries);

result.EndpointName = parts[1];
result.EndpointVersion = parts[2];
result.EntityName = parts[3];
if (parts.Length == 7)
{
result.ID = parts[5];
return result;
result.ActionName = parts[4];
result.Status = parts[5];
}

result.ActionName = parts[5];
result.Status = parts[6];
result.ID = parts[7];
return result;
}

Expand All @@ -368,6 +375,8 @@ public int GetProcessStatus(string invokeResult)
ThrowMissingParameter("GetProcessStatus", nameof(invokeResult));

var parsedLocation = ParseLocation(invokeResult);
if (parsedLocation.ActionName == null)
return 204;
var localVarPath = "/" + parsedLocation.EntityName + "/" + parsedLocation.ActionName + "/" + parsedLocation.Status + "/" + parsedLocation.ID;

var localVarFileParams = new Dictionary<String, FileParameter>();
Expand Down
46 changes: 46 additions & 0 deletions RESTClientTests/ActionLocationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentAssertions;
using System.Linq;
using Xunit;
using System;
using System.Globalization;

using Acumatica.RESTClient.Model;
using Acumatica.RESTClient.Api;

namespace RESTClientTests
{
public class ActionLocationTests
{
[Theory]
[InlineData("/demo", "Default", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/9bab77d3-61dd-488a-ba84-d6820f06d114")]
[InlineData("", "Default", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/42e1c8e2-6346-452e-a604-cff94e7d751a")]
[InlineData("/demo/test", "Default", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/1c1a0de3-2bc1-41ea-93fd-c4e3d98e7f03")]
[InlineData("/AcumaticaEntity", "CustomEntityEndpoint", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/1c1a0de3-2bc1-41ea-93fd-c4e3d98e7f03")]
[InlineData("http://localhost:1231", "Default", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/1c1a0de3-2bc1-41ea-93fd-c4e3d98e7f03")]
[InlineData("https://int.acumatica.com/TestsSite", "Default", "{0}/entity/{1}/18.200.001/Bill/ReleaseBill/status/1c1a0de3-2bc1-41ea-93fd-c4e3d98e7f03")]
public void ParseLocationTestWithAction(string expectedSite, string expectedEndpoint, string inputLocation)
{
var parsedLocation = EntityAPI<Entity>.ParseLocation(string.Format(inputLocation, expectedSite, expectedEndpoint));
parsedLocation.EndpointName.Should().Be(expectedEndpoint);
parsedLocation.EndpointVersion.Should().Be("18.200.001");
parsedLocation.ActionName.Should().Be("ReleaseBill");
parsedLocation.Site.Should().Be(expectedSite);
}

[Theory]
[InlineData("/demo", "Default", "{0}/entity/{1}/18.200.001/Bill/")]
[InlineData("", "Default", "{0}/entity/{1}/18.200.001/Bill")]
[InlineData("/demo/test", "Default", "{0}/entity/{1}/18.200.001/Bill/")]
[InlineData("/AcumaticaEntity", "CustomEntityEndpoint", "{0}/entity/{1}/18.200.001/Bill")]
[InlineData("http://localhost:1231", "Default", "{0}/entity/{1}/18.200.001/Bill")]
[InlineData("https://int.acumatica.com/TestsSite", "Default", "{0}/entity/{1}/18.200.001/Bill")]
public void ParseLocationTestWithoutAction(string expectedSite, string expectedEndpoint, string inputLocation)
{
var parsedLocation = EntityAPI<Entity>.ParseLocation(string.Format(inputLocation, expectedSite, expectedEndpoint));
parsedLocation.EndpointName.Should().Be(expectedEndpoint);
parsedLocation.EndpointVersion.Should().Be("18.200.001");
parsedLocation.ActionName.Should().BeNull();
parsedLocation.Site.Should().Be(expectedSite);
}
}
}
44 changes: 44 additions & 0 deletions RESTClientTests/RESTClientTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<AssemblyTitle>RESTClientTests</AssemblyTitle>
<Product>RESTClientTests</Product>
<Copyright>Copyright © 2020</Copyright>
<OutDir>bin\$(Configuration)\</OutDir>
<WarningLevel>3</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Properties\**" />
<EmbeddedResource Remove="Properties\**" />
<None Remove="Properties\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.core" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System.ServiceModel" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Acumatica.RESTClient\Acumatica.RESTClient.csproj" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions RESTClientTests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
10 changes: 10 additions & 0 deletions RESTClientTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="5.10.3" targetFramework="net48" />
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net48" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net48" />
<package id="xunit.abstractions" version="2.0.3" targetFramework="net48" />
<package id="xunit.extensibility.core" version="2.4.1" targetFramework="net48" />
<package id="xunit.runner.console" version="2.4.1" targetFramework="net48" developmentDependency="true" />
<package id="xunit.runner.visualstudio" version="2.4.1" targetFramework="net48" developmentDependency="true" />
</packages>

0 comments on commit c45a582

Please sign in to comment.