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

Add AVPR Client for Package handling #89

Closed
wants to merge 6 commits into from
Closed
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: 2 additions & 0 deletions src/ARCValidationPackages/ARCValidationPackages.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="Results.fs" />
<Compile Include="Defaults.fs" />
<Compile Include="Domain.fs" />
<Compile Include="AVPRAPI.fs" />
<Compile Include="GitHubAPI.fs" />
<Compile Include="PackageCache.fs" />
<Compile Include="Config.fs" />
Expand All @@ -22,6 +23,7 @@
<ItemGroup>
<PackageReference Include="Fake.DotNet.Cli" Version="[6.0.0]" />
<PackageReference Include="FsHttp" Version="[11.0.0]" />
<PackageReference Include="AVPRClient" Version="[0.0.1]" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions src/ARCValidationPackages/AVPRAPI.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace ARCValidationPackages

open AVPRClient
open ARCValidationPackages
open System
open System.Net.Http

type AVPRAPI () =
member private this.BaseUri = Uri("https://avpr.nfdi4plants.org")
member private this.HttpClienHandler = new HttpClientHandler (UseCookies = false)
member private this.HttpClient = new HttpClient(this.HttpClienHandler, true, BaseAddress=this.BaseUri)
member this.Client = AVPRClient.Client(this.HttpClient)
member this.GetAllPackages (): ValidationPackage [] =
this.Client.GetAllPackagesAsync(System.Threading.CancellationToken.None)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.toArray
member this.GetPackageByName (packageName: string): ValidationPackage =
this.Client.GetLatestPackageByNameAsync(packageName)
|> Async.AwaitTask
|> Async.RunSynchronously
member this.GetPackageByNameAndVersion (packageName: string) (version: string): ValidationPackage =
this.Client.GetPackageByNameAndVersionAsync(packageName, version)
|> Async.AwaitTask
|> Async.RunSynchronously
member this.getPackageIndex(packageName: string, ?version: string) =
let validationPackage =
match version with
| Some v -> this.GetPackageByNameAndVersion packageName v
| None -> this.GetPackageByName packageName
let validationPackageMetadata =
ValidationPackageMetadata.create(
validationPackage.Name,
validationPackage.Description,
validationPackage.MajorVersion,
validationPackage.MinorVersion,
validationPackage.PatchVersion,
true,
(
validationPackage.Authors
|> Array.ofSeq
|> Array.map (fun author ->
Author.create(
author.FullName,
author.Email,
author.Affiliation,
author.AffiliationLink
)
)
),
validationPackage.Tags |> Array.ofSeq,
validationPackage.ReleaseNotes
)
let validationPackageIndex =
ValidationPackageIndex.create(
validationPackage.ReleaseDate,
validationPackageMetadata
)
validationPackageIndex

member this.downloadPackageScript (packageIndex: ValidationPackageIndex) =
let validationPackage =
this.GetPackageByNameAndVersion
packageIndex.Metadata.Name
(ValidationPackageMetadata.getSemanticVersionString packageIndex.Metadata)
Text.Encoding.UTF8.GetString validationPackage.PackageContent

85 changes: 59 additions & 26 deletions src/ARCValidationPackages/Config.fs
Original file line number Diff line number Diff line change
@@ -1,49 +1,82 @@
namespace ARCValidationPackages
open System.IO
open System.Text.Json
open AVPRClient

type Config = {
PackageIndex: ValidationPackageIndex []
IndexLastUpdated: System.DateTimeOffset
PackageIndex: ValidationPackageIndex [] option
IndexLastUpdated: System.DateTimeOffset option
PackageCacheFolder: string
ConfigFilePath: string
IsPreview: bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caroott please note that it must be possible to install both preview and prod packages side-by-side.

This looks to me like that use case would be problematic

} with
static member create (
packageIndex: ValidationPackageIndex [],
indexLastUpdated: System.DateTimeOffset,
packageCacheFolder: string,
configFilePath: string
configFilePath: string,
isPreview: bool,
?packageIndex: ValidationPackageIndex [],
?indexLastUpdated: System.DateTimeOffset
) =
if (isPreview && (packageIndex.IsNone || indexLastUpdated.IsNone)) then
failwith "packageIndex and indexLastUpdated must be provided if the github API is used"
{
PackageIndex = packageIndex
IndexLastUpdated = indexLastUpdated
PackageCacheFolder = packageCacheFolder
ConfigFilePath = configFilePath
IsPreview = isPreview
}

static member initDefault(?Token, ?ConfigPath, ?CacheFolder) =
Config.create(
packageIndex = GitHubAPI.getPackageIndex(?Token = Token),
indexLastUpdated = System.DateTimeOffset.Now,
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER()),
configFilePath = defaultArg ConfigPath (Defaults.CONFIG_FILE_PATH())
)
static member indexContainsPackages (packageName: string) (config: Config) =
config.PackageIndex |> Array.exists (fun package -> package.Metadata.Name = packageName)
static member initDefault(isPreview: bool, ?Token, ?ConfigPath, ?CacheFolder) =
if not isPreview then
Config.create(
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER()),
configFilePath = defaultArg ConfigPath (Defaults.CONFIG_FILE_PATH()),
isPreview = false
)
else
Config.create(
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER()),
configFilePath = defaultArg ConfigPath (Defaults.CONFIG_FILE_PATH()),
isPreview = true,
packageIndex = GitHubAPI.getPackageIndex(?Token = Token),
indexLastUpdated = System.DateTimeOffset.Now
)
static member indexContainsPackages (packageName: string) (config: Config) (client: Client option) =
if not config.IsPreview then
let allPackages =
if client.IsSome then
client.Value.GetAllPackagesAsync(System.Threading.CancellationToken.None)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.toArray
else
failwith "Client must be provided if the NFDI4Plants API is used"
allPackages
|> Array.exists (fun package -> package.Name = packageName)
else
config.PackageIndex.Value |> Array.exists (fun package -> package.Metadata.Name = packageName)

static member indexContainsPackage (packageName: string) (semverString: string) (config: Config) =
config.PackageIndex
|> Array.exists (fun package ->
package.Metadata.Name = packageName
&& (ValidationPackageIndex.getSemanticVersionString package = semverString)

)
if not config.IsPreview then
printfn "Warning: Your Config does not contain an Index"
false
else
config.PackageIndex.Value
|> Array.exists (fun package ->
package.Metadata.Name = packageName
&& (ValidationPackageIndex.getSemanticVersionString package = semverString)
)

static member getIndexedPackagesByName (packageName: string) (config: Config) =
config.PackageIndex |> Array.filter (fun package -> package.Metadata.Name = packageName)
if not config.IsPreview then
printfn "Warning: Your Config does not contain an Index"
[||]
else
config.PackageIndex.Value |> Array.filter (fun package -> package.Metadata.Name = packageName)

static member tryGetLatestPackage (packageName: string) (config: Config) =
if Config.indexContainsPackages packageName config then
if (Config.indexContainsPackages packageName config None) then
config
|> Config.getIndexedPackagesByName packageName
|> Array.maxBy ValidationPackageIndex.getSemanticVersionString
Expand All @@ -52,7 +85,7 @@ type Config = {

static member tryGetIndexedPackageByNameAndVersion (packageName: string) (semverString: string) (config: Config) =
if Config.indexContainsPackage packageName semverString config then
config.PackageIndex
config.PackageIndex.Value
|> Array.find (fun package ->
package.Metadata.Name = packageName
&& (ValidationPackageIndex.getSemanticVersionString package = semverString)
Expand All @@ -67,7 +100,7 @@ type Config = {
|> Array.maxBy ValidationPackageIndex.getSemanticVersionString

static member getIndexedPackageByNameAndVersion (packageName: string) (semverString: string) (config: Config) =
config.PackageIndex
config.PackageIndex.Value
|> Array.find (fun package ->
package.Metadata.Name = packageName
&& (ValidationPackageIndex.getSemanticVersionString package = semverString)
Expand All @@ -76,7 +109,7 @@ type Config = {
static member withIndex (index: ValidationPackageIndex []) (config: Config) =
{
config with
PackageIndex = index
PackageIndex = Some index
}

static member exists (?Path: string) =
Expand All @@ -93,7 +126,7 @@ type Config = {
if Config.exists(?Path = Path) then
Config.read(?Path = Path)
else
Config.initDefault(?Token = Token, ?ConfigPath = Path, ?CacheFolder = CacheFolder)
Config.initDefault(isPreview = true, ?Token = Token, ?ConfigPath = Path, ?CacheFolder = CacheFolder)

static member write (?Path: string) =
fun (config: Config) ->
Expand Down
22 changes: 11 additions & 11 deletions src/ARCValidationPackages/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ type ValidationPackageMetadata() =
/// </summary>
type ValidationPackageIndex =
{
RepoPath: string
RepoPath: string option
FileName:string
LastUpdated: System.DateTimeOffset
Metadata: ValidationPackageMetadata
} with
static member create (
repoPath: string,
fileName: string,
lastUpdated: System.DateTimeOffset,
metadata: ValidationPackageMetadata
metadata: ValidationPackageMetadata,
?repoPath: string

) =
{
Expand All @@ -145,16 +145,16 @@ type ValidationPackageIndex =
Metadata = metadata
}
static member create (
repoPath: string,
lastUpdated: System.DateTimeOffset,
metadata: ValidationPackageMetadata
metadata: ValidationPackageMetadata,
?repoPath: string
) =
ValidationPackageIndex.create(
repoPath = repoPath,
fileName = Path.GetFileNameWithoutExtension(repoPath),
lastUpdated = lastUpdated,
metadata = metadata
)
{
RepoPath = repoPath
FileName = Path.GetFileNameWithoutExtension(repoPath.Value)
LastUpdated = lastUpdated
Metadata = metadata
}

static member getSemanticVersionString(i: ValidationPackageIndex) = $"{i.Metadata.MajorVersion}.{i.Metadata.MinorVersion}.{i.Metadata.PatchVersion}";

Expand Down
2 changes: 1 addition & 1 deletion src/ARCValidationPackages/GitHubAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ type GitHubAPI =
raise (GitHubAPI.Errors.SerializationError($"{e.Message}{System.Environment.NewLine}{json}"))

static member downloadPackageScript (packageIndex: ValidationPackageIndex, ?Token: string) =
GitHubAPI.downloadPackageScript(packageIndex.RepoPath, ?Token = Token)
GitHubAPI.downloadPackageScript(packageIndex.RepoPath.Value, ?Token = Token)
Loading
Loading