Skip to content

Feature / Import Postman Collection extensions added #3

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

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .build/Targets/Extension_Targets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ partial class Build
.DependsOn(Extension_Build)
.Executes(() =>
{
var path = Directory.GetFiles(ExtensionDirectory, "*.nupkg", SearchOption.AllDirectories).FirstOrDefault();
var path = Directory.GetFiles(ExtensionDirectory, $"{ExtensionName}.*.nupkg", SearchOption.AllDirectories).FirstOrDefault();
if (string.IsNullOrWhiteSpace(path)) throw new FileNotFoundException("Extension library wasn't found.");

DotNetNuGetPush(x => x
Expand Down
8 changes: 7 additions & 1 deletion .build/Targets/Solution_Targets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using Nuke.Common;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;
using static Nuke.Common.Tools.DotNet.DotNetTasks;

Expand All @@ -16,6 +15,13 @@ partial class Build
{
if (Directory.Exists(OutputDirectory)) Directory.Delete(OutputDirectory, true);
DotNetClean(x => x.SetProject(Solution));

// delete all 'bin' directories
foreach (var project in Solution.AllProjects.Where(x => !x.Name.Equals("Builder.Shared", StringComparison.Ordinal)))
{
var binDir = project.Directory / "bin";
if (Directory.Exists(binDir)) Directory.Delete(binDir, true);
}
});

Target Solution_Restore => _ => _
Expand Down
1 change: 0 additions & 1 deletion src/.editorconfig → .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ dotnet_diagnostic.SA1024.severity = none
dotnet_diagnostic.SA1028.severity = none # Code should not contain trailing whitespace
dotnet_diagnostic.SA1503.severity = none

[{*Model.cs, *Settings.cs, *Entity.cs}]
dotnet_diagnostic.SA1313.severity = none # incorrect for records

[*Tests.cs]
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions RestApia.Shared.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Builder", "Builder", "{07A0
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder.Shared", ".build\Builder.Shared.csproj", "{35A67EB2-CF2D-40B2-B598-685D70F9D94D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestApia.Extensions.Import.Postman", "src\Extensions\RestApia.Extensions.Import.Postman\RestApia.Extensions.Import.Postman.csproj", "{00CAA372-8954-4A50-94C5-6574EBACC467}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Playground", "Playground", "{B9D6F201-FDF7-42BB-A688-C8CBC51B4718}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestApia.Experiments.Tests", "src\Playground\RestApia.Experiments.Tests\RestApia.Experiments.Tests.csproj", "{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,10 +40,20 @@ Global
{35A67EB2-CF2D-40B2-B598-685D70F9D94D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35A67EB2-CF2D-40B2-B598-685D70F9D94D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35A67EB2-CF2D-40B2-B598-685D70F9D94D}.Release|Any CPU.Build.0 = Release|Any CPU
{00CAA372-8954-4A50-94C5-6574EBACC467}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00CAA372-8954-4A50-94C5-6574EBACC467}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00CAA372-8954-4A50-94C5-6574EBACC467}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00CAA372-8954-4A50-94C5-6574EBACC467}.Release|Any CPU.Build.0 = Release|Any CPU
{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9B2C67A1-83B9-157D-5EAF-ADD07B0E8D3F} = {E2CF6B65-E284-39DA-C77D-4FDAB55B16AD}
{AE97EEF8-CE4E-0976-B0FB-8C4FC7616E01} = {E2CF6B65-E284-39DA-C77D-4FDAB55B16AD}
{35A67EB2-CF2D-40B2-B598-685D70F9D94D} = {07A0437C-C5B5-4FC9-AA80-71C96A11FF54}
{00CAA372-8954-4A50-94C5-6574EBACC467} = {E2CF6B65-E284-39DA-C77D-4FDAB55B16AD}
{200B00D8-9D09-464F-B4A8-B7DEC68EE9E8} = {B9D6F201-FDF7-42BB-A688-C8CBC51B4718}
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions build-all.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
param(
[string]$version = ""
)

# call script in cli
nuke Solution_Clean
nuke Shared_Build --SharedLibVersion $version

# find all in Extensions folder
$extensions = Get-ChildItem -Path .\src\Extensions -Directory
foreach ($extension in $extensions) {
nuke Extension_Build --ExtensionName $extension.Name --ExtensionLibVersion $version
}

# find all nupkg files and store to one folder
$nugetsFolder = ".\.local\nuget"
if (Test-Path .\.local\nuget) {
Remove-Item -Path $nugetsFolder -Recurse -Force
}
New-Item -ItemType Directory -Path $nugetsFolder -Force

$files = Get-ChildItem -Path .\.local\builds -Recurse -Filter *.nupkg
$files | ForEach-Object { Copy-Item $_.FullName -Destination $nugetsFolder -Force }
1 change: 1 addition & 0 deletions src/Extensions/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RepositoryUrl>https://github.com/RestApia/RestApia.Shared</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>RestApia: 0.3.0</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;
using RestApia.Shared.Extensions.Interfaces;
using RestApia.Shared.Extensions.Models;
using RestApia.Shared.Values.Enums;
using RestApia.Shared.Common.Enums;
using RestApia.Shared.Common.Models;
using RestApia.Shared.Extensions.AuthService;
namespace RestApia.Extensions.Auth.Basic;

public class BasicAuthService : IAuthService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RestApia.Shared.Values.Attributes;
using RestApia.Shared.Common.Attributes;
namespace RestApia.Extensions.Auth.Basic;

public record BasicAuthSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<Title>Basic Authorization extension</Title>
<Description>Generate Authorization header based on settings for requests.</Description>
<RepositoryUrl>https://github.com/RestApia/RestApia.Shared/tree/main/src/Extensions/RestApia.Extensions.Auth.Basic</RepositoryUrl>
<PackageTags>RestApiaEmbedded, Extension</PackageTags>
<PackageTags>RestApiaEmbedded, Extension, Authorization</PackageTags>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestApia.Shared.Common;
using RestApia.Shared.Extensions.Interfaces;
using RestApia.Shared.Extensions.Models;
using RestApia.Shared.Values.Enums;
using RestApia.Shared.Common.Enums;
using RestApia.Shared.Common.Interfaces;
using RestApia.Shared.Common.Models;
using RestApia.Shared.Extensions.AuthService;
namespace RestApia.Extensions.Auth.OAuth2.AuthCode;

public class OAuth2AuthCodeService : IAuthService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RestApia.Shared.Values.Attributes;
using RestApia.Shared.Common.Attributes;
namespace RestApia.Extensions.Auth.OAuth2.AuthCode;

public record OAuth2AuthCodeSettings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Web;
using RestApia.Shared.Common;
using RestApia.Shared.Extensions.Interfaces;
using RestApia.Shared.Extensions.Models;
using RestApia.Shared.Values.Enums;
using RestApia.Shared.Common.Enums;
using RestApia.Shared.Common.Interfaces;
using RestApia.Shared.Common.Models;
using RestApia.Shared.Extensions.AuthService;
namespace RestApia.Extensions.Auth.OAuth2.Implicit;

public class OAuth2ImplicitService : IAuthService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RestApia.Shared.Values.Attributes;
using RestApia.Shared.Common.Attributes;
namespace RestApia.Extensions.Auth.OAuth2.Implicit;

public record OAuth2ImplicitSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Diagnostics.CodeAnalysis;
using System.IdentityModel.Tokens.Jwt;
using System.Web;
using RestApia.Shared.Extensions.Interfaces;
using RestApia.Shared.Extensions.Models;
using RestApia.Shared.Values.Enums;
using RestApia.Shared.Common.Enums;
using RestApia.Shared.Common.Interfaces;
using RestApia.Shared.Common.Models;

namespace RestApia.Extensions.Auth.OAuth2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Title>OAuth2 Authorization extension</Title>
<Description>Extension provides OAuth2 Implicit and AuthCode authorizations</Description>
<RepositoryUrl>https://github.com/RestApia/RestApia.Shared/tree/main/src/Extensions/RestApia.Extensions.Auth.OAuth2</RepositoryUrl>
<PackageTags>RestApiaEmbedded, Extension</PackageTags>
<PackageTags>RestApiaEmbedded, Extension, Authorization</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Newtonsoft.Json.Linq;
namespace RestApia.Extensions.Import.Postman.Abstract;

internal record JDataModel(JObject Data, PostmanJsonType Type);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RestApia.Extensions.Import.Postman.Abstract;

internal enum PostmanJsonType
{
Unknown,
Collection,
Environment,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json.Linq;
using RestApia.Shared.Common.Models;
namespace RestApia.Extensions.Import.Postman.Logic;

public static class ImportAuthBasic
{
public static AuthContentModel Import(JToken data)
{
var values = Tools.ParseValues(data["basic"] as JArray ?? [])
.ToDictionary(x => x.Key, x => x.Value);

var settings = new Dictionary<string, string>
{
{ "UserName", values.GetValueOrDefault("username", string.Empty) },
{ "Password", values.GetValueOrDefault("password", string.Empty) },
};

return new ()
{
Name = "Basic Authorization",
Content = Tools.SerializeValues(settings),
SettingsClassFullName = "RestApia.Extensions.Auth.Basic.BasicAuthSettings",
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Newtonsoft.Json.Linq;
using RestApia.Shared.Common.Models;
namespace RestApia.Extensions.Import.Postman.Logic;

internal static class ImportAuthOAuth2
{
public static AuthContentModel? Import(JToken data)
{
var values = Tools.ParseValues(data["oauth2"] as JArray ?? [])
.ToDictionary(x => x.Key, x => x.Value);

var grandType = values.GetValueOrDefault("grant_type", string.Empty);
return grandType switch
{
"authorization_code" => ParseAuthOAuth2AuthorizationCode(values),
_ => ParseAuthOAuth2UnknownGrandType(data),
};
}

private static AuthContentModel ParseAuthOAuth2AuthorizationCode(Dictionary<string, string> values)
{
// OAuth2AuthCodeSettings object
var settings = new Dictionary<string, string>
{
{ "AuthUrl", values.GetValueOrDefault("authUrl", string.Empty) },
{ "AccessTokenUrl", values.GetValueOrDefault("accessTokenUrl", string.Empty) },
{ "RedirectUrl", values.GetValueOrDefault("redirect_uri", string.Empty) },
{ "ClientId", values.GetValueOrDefault("clientId", string.Empty) },
{ "ClientSecret", values.GetValueOrDefault("clientSecret", string.Empty) },
{ "Scopes", values.GetValueOrDefault("scope", string.Empty) },
{ "CredentialsSendMethod", values.GetValueOrDefault("client_authentication", string.Empty).Equals("body", StringComparison.OrdinalIgnoreCase) ? "Body" : "Header" },
};

return new AuthContentModel
{
Name = "OAuth2 - Authorization Code",
SettingsClassFullName = "RestApia.Extensions.Auth.OAuth2.AuthCode.OAuth2AuthCodeSettings",
Content = Tools.SerializeValues(settings),
};
}

private static AuthContentModel? ParseAuthOAuth2UnknownGrandType(JToken data)
{
var result = ImportCollection.ParseAuthUnsupported(data);
if (result == null) return null;

return result with
{
Name = "OAuth2 - Unsupported grant type",
Content = $"// Unsupported OAuth2 grant type: {data["grant_type"]}\r\n" + result.Content,
};
}
}
Loading
Loading