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

Integrate AVPR API int arc-validate #92

Closed
wants to merge 14 commits into from
97 changes: 80 additions & 17 deletions src/ARCValidationPackages/AVPRAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,92 @@ open AVPRIndex
open System
open System.Net.Http

module AVPRAPI =

module Errors =

type ResponseNullError(msg : string) =
inherit Exception(msg)

type ServerSideError(msg : string) =
inherit Exception(msg)

type UnexpectedStatusCodeError(msg : string) =
inherit Exception(msg)

type DeserializationError(msg : string) =
inherit Exception(msg)

type GeneralError(msg : string) =
inherit Exception(msg)

type AVPRAPI () =
member private this.BaseUri = Uri("https://avpr.nfdi4plants.org")
Copy link
Member

Choose a reason for hiding this comment

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

is it necessary to use instance methods instead of static methods here?

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
try
this.Client.GetAllPackagesAsync(System.Threading.CancellationToken.None)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.toArray
with
| :? AVPRClient.ApiException as e ->
match e.Message with
| m when m.ToLower().Contains "response was null" ->
raise (AVPRAPI.Errors.ResponseNullError(e.Message))
| m when m.ToLower().Contains "http status code of the response was not expected" ->
raise (AVPRAPI.Errors.UnexpectedStatusCodeError(e.Message))
| m when m.ToLower().Contains "server side error occurred" ->
raise (AVPRAPI.Errors.ServerSideError(e.Message))
| m when m.ToLower().Contains "could not deserialize the response body string" ->
raise (AVPRAPI.Errors.DeserializationError(e.Message))
| _ ->
raise (AVPRAPI.Errors.GeneralError(e.Message))
member this.GetPackageByName (packageName: string): ValidationPackage =
this.Client.GetLatestPackageByNameAsync(packageName)
|> Async.AwaitTask
|> Async.RunSynchronously
try
this.Client.GetLatestPackageByNameAsync(packageName)
|> Async.AwaitTask
|> Async.RunSynchronously
with
| :? AVPRClient.ApiException as e ->
match e.Message with
| m when m.ToLower().Contains "response was null" ->
raise (AVPRAPI.Errors.ResponseNullError(e.Message))
| m when m.ToLower().Contains "http status code of the response was not expected" ->
raise (AVPRAPI.Errors.UnexpectedStatusCodeError(e.Message))
| m when m.ToLower().Contains "server side error occurred" ->
raise (AVPRAPI.Errors.ServerSideError(e.Message))
| m when m.ToLower().Contains "could not deserialize the response body string" ->
raise (AVPRAPI.Errors.DeserializationError(e.Message))
| _ ->
raise (AVPRAPI.Errors.GeneralError(e.Message))
member this.GetPackageByNameAndVersion (packageName: string) (version: string): ValidationPackage =
this.Client.GetPackageByNameAndVersionAsync(packageName, version)
|> Async.AwaitTask
|> Async.RunSynchronously

//member this.downloadPackageScript (packageIndex: ValidationPackageIndex) =
try
this.Client.GetPackageByNameAndVersionAsync(packageName, version)
|> Async.AwaitTask
|> Async.RunSynchronously
with
| :? AVPRClient.ApiException as e ->
match e.Message with
| m when m.ToLower().Contains "response was null" ->
raise (AVPRAPI.Errors.ResponseNullError(e.Message))
| m when m.ToLower().Contains "http status code of the response was not expected" ->
raise (AVPRAPI.Errors.UnexpectedStatusCodeError(e.Message))
| m when m.ToLower().Contains "server side error occurred" ->
raise (AVPRAPI.Errors.ServerSideError(e.Message))
| m when m.ToLower().Contains "could not deserialize the response body string" ->
raise (AVPRAPI.Errors.DeserializationError(e.Message))
| _ ->
raise (AVPRAPI.Errors.GeneralError(e.Message))

// let validationPackage =
// this.GetPackageByNameAndVersion
// packageIndex.Metadata.Name
// (ValidationPackageMetadata.getSemanticVersionString packageIndex.Metadata)
// Text.Encoding.UTF8.GetString validationPackage.PackageContent
member this.downloadPackageScript (packageName: string, ?version: string) =
let vp =
match version with
| Some v -> this.GetPackageByNameAndVersion packageName v
| None -> this.GetPackageByName packageName
let script =
vp.PackageContent
|> Text.Encoding.UTF8.GetString
script
18 changes: 11 additions & 7 deletions src/ARCValidationPackages/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ open AVPRIndex.Domain
type Config = {
PackageIndex: ValidationPackageIndex []
IndexLastUpdated: System.DateTimeOffset
PackageCacheFolder: string
PackageCacheFolderPreview: string
PackageCacheFolderRelease: string
ConfigFilePath: string
} with
static member create (
packageIndex: ValidationPackageIndex [],
indexLastUpdated: System.DateTimeOffset,
packageCacheFolder: string,
packageCacheFolderPreview: string,
packageCacheFolderRelease: string,
configFilePath: string
) =
{
PackageIndex = packageIndex
IndexLastUpdated = indexLastUpdated
PackageCacheFolder = packageCacheFolder
PackageCacheFolderPreview = packageCacheFolderPreview
PackageCacheFolderRelease = packageCacheFolderRelease
ConfigFilePath = configFilePath
}

static member initDefault(?Token, ?ConfigPath, ?CacheFolder) =
static member initDefault(?Token, ?ConfigPath, ?CacheFolderPreview, ?CacheFolderRelease) =
Config.create(
packageIndex = GitHubAPI.getPackageIndex(?Token = Token),
indexLastUpdated = System.DateTimeOffset.Now,
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW()),
packageCacheFolderPreview = defaultArg CacheFolderPreview (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW()),
packageCacheFolderRelease = defaultArg CacheFolderRelease (Defaults.PACKAGE_CACHE_FOLDER_RELEASE()),
configFilePath = defaultArg ConfigPath (Defaults.CONFIG_FILE_PATH())
)
static member indexContainsPackages (packageName: string) (config: Config) =
Expand Down Expand Up @@ -90,11 +94,11 @@ type Config = {
|> File.ReadAllText
|> fun jsonString -> JsonSerializer.Deserialize<Config>(jsonString, Defaults.SERIALIZATION_OPTIONS)

static member get (?Path: string, ?CacheFolder:string, ?Token:string) =
static member get (?Path: string, ?CacheFolderPreview:string, ?CacheFolderRelease:string, ?Token:string) =
if Config.exists(?Path = Path) then
Config.read(?Path = Path)
else
Config.initDefault(?Token = Token, ?ConfigPath = Path, ?CacheFolder = CacheFolder)
Config.initDefault(?Token = Token, ?ConfigPath = Path, ?CacheFolderPreview = CacheFolderPreview, ?CacheFolderRelease = CacheFolderRelease)

static member write (?Path: string) =
fun (config: Config) ->
Expand Down
18 changes: 16 additions & 2 deletions src/ARCValidationPackages/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CachedValidationPackage =
}

/// <summary>
/// Creates a new ARCValidationPackage from a ValidationPackageIndex, with the CacheDate set to the current or optionally a custom date, and the LocalPath set to the default cache folder or custom folder.
/// Creates a new ARCValidationPackage from a ValidationPackageIndex, with the CacheDate set to the current or optionally a custom date, and the LocalPath set to the default preview cache folder or custom folder.
/// </summary>
/// <param name="packageIndex">The input package index entry</param>
/// <param name="Date">Optional. The date to set the CacheDate to. Defaults to the current date.</param>
Expand All @@ -50,7 +50,7 @@ type CachedValidationPackage =
)

/// <summary>
/// Creates a new ARCValidationPackage from a package name only, with the CacheDate set to the current or optionally a custom date, and the LocalPath set to the default cache folder or custom folder.
/// Creates a new ARCValidationPackage from a package name only, with the CacheDate set to the current or optionally a custom date, and the LocalPath set to the default preview cache folder or custom folder.
/// </summary>
/// <param name="packageName"></param>
/// <param name="Date"></param>
Expand All @@ -64,6 +64,20 @@ type CachedValidationPackage =
metadata = ValidationPackageMetadata()
)

/// <summary>
/// Creates a new ARCValidationPackage from a ValidationPackageMetadata, with the CacheDate set to the current or optionally a custom date, and the LocalPath set to the default release cache folder or custom folder.
/// </summary>
/// <param name="packageIndex">The input package index entry</param>
/// <param name="Date">Optional. The date to set the CacheDate to. Defaults to the current date.</param>
static member ofPackageMetadata (packageMetadata: ValidationPackageMetadata, ?Date: System.DateTimeOffset, ?CacheFolder: string) =
let path = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER_RELEASE())
CachedValidationPackage.create(
fileName = packageMetadata.Name,
cacheDate = (defaultArg Date System.DateTimeOffset.Now),
localPath = (System.IO.Path.Combine(path, $"{packageMetadata.Name}.fsx").Replace("\\","/")),
metadata = packageMetadata
)

/// <summary>
/// returns a copy of the input ARCValidationPackage with the CacheDate set to the given date.
/// </summary>
Expand Down
12 changes: 4 additions & 8 deletions src/ARCValidationPackages/PackageCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,25 @@ type PackageCache =
cache.Remove(name) |> ignore
cache

static member exists (?Path: string) =
let path = defaultArg Path (Defaults.PACKAGE_CACHE_FILE_PATH_PREVIEW())
static member exists (path: string) =
File.Exists(path)

static member read (?Path: string) =
let path = defaultArg Path (Defaults.PACKAGE_CACHE_FILE_PATH_PREVIEW())
static member read (path: string) =
path
|> File.ReadAllText
|> fun jsonString -> JsonSerializer.Deserialize<PackageCache>(jsonString, Defaults.SERIALIZATION_OPTIONS)

static member get (?Folder: string, ?FileName: string) =
static member get (folder: string, ?FileName: string) =
let fileName = defaultArg FileName (Defaults.PACKAGE_CACHE_FILE_NAME)
let folder = defaultArg Folder (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW())
let path = Path.Combine(folder, fileName)
if PackageCache.exists(path) then
PackageCache.read(path)
else
PackageCache.create([])

static member write (?Folder: string, ?FileName: string) =
static member write (folder: string, ?FileName: string) =
fun (cache: PackageCache) ->
let fileName = defaultArg FileName (Defaults.PACKAGE_CACHE_FILE_NAME)
let folder = defaultArg Folder (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW())
let path = Path.Combine(folder, fileName)
System.IO.FileInfo(path).Directory.Create(); // ensures directory exists
JsonSerializer.Serialize(cache, Defaults.SERIALIZATION_OPTIONS)
Expand Down
Loading
Loading