Skip to content
Snippets Groups Projects
Commit 97fc9ce9 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

Merge branch 'dev' into 'master'

Release: Sprint/2022 22 :robot:

See merge request !23
parents 183f1906 3733fcc7
Branches
Tags
1 merge request!23Release: Sprint/2022 22 :robot:
Showing
with 171 additions and 208 deletions
......@@ -4,8 +4,6 @@ using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Tests
{
......@@ -15,6 +13,7 @@ namespace Coscine.ProxyApi.Tests
private readonly IConfiguration _configuration;
private readonly string _prefix;
private readonly EpicClient _epicClient;
private readonly string _guid;
public EpicClientTests()
{
......@@ -29,38 +28,88 @@ namespace Coscine.ProxyApi.Tests
_configuration.GetString("coscine/global/epic/user"),
_configuration.GetString("coscine/global/epic/password")
);
_guid = Guid.NewGuid().ToString();
}
[TearDown]
public void DeleteCreatedPid()
{
_epicClient.Delete(_guid);
}
[Test]
public void TestCreate()
{
List<EpicData> list = GetPIDValues("pid/?pid={{pid}}");
var epicData = _epicClient.Create(list, _guid.ToString());
Assert.NotNull(epicData);
Assert.IsTrue(epicData.EpicPid == _prefix + "/" + _guid.ToString());
var epicDataList = _epicClient.Get(_guid.ToString());
Assert.IsTrue(epicDataList.Any());
}
[Test]
public void ClientTest()
public void TestUpdate()
{
Guid guid = Guid.NewGuid();
// Use old URL
List<EpicData> list = GetPIDValues("coscine/apps/pidresolve/?pid={{pid}}");
var epicData = _epicClient.Create(list, _guid.ToString());
Assert.NotNull(epicData);
Assert.IsTrue(epicData.EpicPid == _prefix + "/" + _guid.ToString());
var epicDataList = _epicClient.Get(_guid.ToString());
Assert.IsTrue(epicDataList.Any(e => e.ParsedData.ToString().Contains("coscine/apps/pidresolve/?pid=")));
Assert.IsFalse(epicDataList.Any(e => e.ParsedData.ToString().Contains("pid/?pid=")));
// Use new URL
List<EpicData> listNew = GetPIDValues("pid/?pid={{pid}}");
_epicClient.Update(_guid.ToString(), listNew);
var epicDataListNew = _epicClient.Get(_guid.ToString());
Assert.IsFalse(epicDataListNew.Any(e => e.ParsedData.ToString().Contains("coscine/apps/pidresolve/?pid=")));
Assert.IsTrue(epicDataListNew.Any(e => e.ParsedData.ToString().Contains("pid/?pid=")));
}
private List<EpicData> GetPIDValues(string subpath)
{
var baseUrl = _configuration.GetStringAndWait(
"coscine/local/app/additional/url",
"https://coscine.rwth-aachen.de"
);
EpicData url = new EpicData
{
Type = "URL",
ParsedData = "https://app.rwth-aachen.de/resolvehandle/?pid={{pid}}"
ParsedData = _configuration.GetStringAndWait(
"coscine/global/epic/pid/url",
$"{baseUrl}/{subpath}"
)
};
EpicData metaurl = new EpicData
{
Type = "METAURL",
ParsedData = "https://moped.ecampus.rwth-aachen.de/proxy/api/v2/eScience/Metadata/Download?pid={{pid}}"
ParsedData = _configuration.GetStringAndWait(
"coscine/global/epic/pid/metaurl",
$"{baseUrl}/{subpath}"
)
};
List<EpicData> list = new List<EpicData>
EpicData dataurl = new EpicData
{
Type = "DATAURL",
ParsedData = _configuration.GetStringAndWait(
"coscine/global/epic/pid/dataurl",
$"{baseUrl}/{subpath}"
)
};
return new List<EpicData>
{
url,
metaurl,
dataurl
};
var epicData = _epicClient.Create(list, guid.ToString());
Assert.IsTrue(epicData.EpicPid == _prefix + "/" + guid.ToString());
var epicDataList = _epicClient.Get(guid.ToString());
Assert.IsTrue(epicDataList.Count() > 0);
_epicClient.Delete(guid.ToString());
}
}
}
......@@ -2,11 +2,6 @@
using Coscine.ProxyApi.TokenValidator;
using Coscine.ProxyApi.Utils;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Tests
{
......
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Coscine.ProxyApi
{
......
using System.Collections.Generic;
using Coscine.ProxyApi.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Coscine.ProxyApi.Utils;
using Newtonsoft.Json;
namespace Coscine.ProxyApi
{
......
......@@ -9,7 +9,7 @@
<PropertyGroup>
<Authors>RWTH Aachen University</Authors>
<Company>IT Center, RWTH Aachen University</Company>
<Copyright>2021 IT Center, RWTH Aachen University</Copyright>
<Copyright>2022 IT Center, RWTH Aachen University</Copyright>
<Description>ProxyApi is a part of the Coscine group.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://git.rwth-aachen.de/coscine/backend/libraries/ProxyApi</PackageProjectUrl>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi
namespace Coscine.ProxyApi
{
public static class Settings
{
private static string oAuth2ContextUrlJson = "https://oauth.campus.rwth-aachen.de/oauth2waitress/oauth2.svc/context2";
public static string OAuth2ContextUrlJson {
public static string OAuth2ContextUrlJson
{
get { return oAuth2ContextUrlJson; }
set { oAuth2ContextUrlJson = value; }
}
......
using Coscine.Configuration;
using Coscine.ProxyApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
namespace Coscine.ProxyApi.TokenValidator
{
public interface ITokenValidator
{
......
using Coscine.Configuration;
using Coscine.ProxyApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
{
......
using Coscine.ProxyApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
{
......
......@@ -2,8 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Coscine.ProxyApi.Utils
{
......
using Coscine.ProxyApi.Utils.Exceptions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
namespace Coscine.ProxyApi.Utils
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace Coscine.ProxyApi.Utils
{
......
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils
{
......
using System;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Coscine.ProxyApi.Utils
{
public class EpicClient : IEpicClient
{
private readonly RestClient restClient = new RestClient();
private readonly HttpClient _httpClient = new();
public const int errorPrefix = 12000;
private string ServiceEndpoint
{
get; set;
}
private string User
{
get; set;
}
private string Password
{
get; set;
}
private string ServiceEndpoint { get; set; }
public string Prefix
{
get; private set;
}
public string Prefix { get; private set; }
public EpicClient(string url, string prefix, string user, string password)
{
ServiceEndpoint = url;
User = user;
Password = password;
Prefix = prefix;
var byteArray = Encoding.ASCII.GetBytes($"{user}:{password}");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json, */*");
}
private void ReplacePayload(string suffix, List<EpicData> payload)
......@@ -56,122 +47,115 @@ namespace Coscine.ProxyApi.Utils
public EpicData Update(string suffix, List<EpicData> payload)
{
ReplacePayload(suffix, payload);
return EpicRequestWrapper(() => restClient.HttpJson<EpicData, List<EpicData>>("PUT", new Uri(ServiceEndpoint + suffix), null, payload, User, Password));
using var requestContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
var result = EpicRequestWrapper(() => _httpClient.PutAsync(ServiceEndpoint + suffix, requestContent));
if (result.IsSuccessStatusCode)
{
var content = result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<EpicData>(content);
}
return null;
}
public EpicData Create(List<EpicData> payload, string guid = null)
{
string suffix = guid;
if (guid == null)
if (suffix is null)
{
suffix = Guid.NewGuid().ToString();
}
ReplacePayload(suffix, payload);
return Update(suffix, payload);
}
public IEnumerable<EpicData> List(int count, int page)
public IEnumerable<EpicData> Search(string searchUrl, int limit = 0)
{
var query = string.Format("limit={0}&page={1}", count, page);
var result = EpicRequestWrapper(() => restClient.HttpRaw("GET", new Uri(ServiceEndpoint), query, null, User, Password));
var pidList = new List<EpicData>();
using (var reader = new StreamReader(result))
{
while (!reader.EndOfStream)
// Handle additional query parameters
var parameters = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(searchUrl))
{
pidList.Add(new EpicData() { EpicPid = reader.ReadLine() });
}
}
return pidList;
parameters.Add("URL", searchUrl);
}
public IEnumerable<EpicData> ListAll(int limit = 1000, int startPage = 1)
if (limit > 0)
{
var pidList = new List<EpicData>();
var tempList = List(limit, startPage);
while (tempList.Count() > 0)
parameters.Add("limit", limit.ToString());
}
// Build the request URL
var url = new Uri(QueryHelpers.AddQueryString(ServiceEndpoint, parameters));
// Execute the request
var result = EpicRequestWrapper(() => _httpClient.GetAsync(url));
if (result is not null && result.IsSuccessStatusCode)
{
pidList.AddRange(tempList);
startPage++;
tempList = List(limit, startPage);
var content = result.Content.ReadAsStringAsync().Result;
content.Split('\n').ToList().ForEach(e => pidList.Add(new EpicData() { EpicPid = e }));
}
return pidList;
}
public int CountForKpis(int lastPage, int last, int limit)
public IEnumerable<EpicData> List(int limit = 0, int page = 0)
{
var pidList = new List<EpicData>();
int page = lastPage;
var query = string.Format("limit={0}&page={1}", limit, lastPage);
try
// Handle additional query parameters
var parameters = new Dictionary<string, string>();
if (limit > 0)
{
var result = EpicRequestWrapper(() => restClient.HttpRaw("GET", new Uri(ServiceEndpoint), query, null, User, Password));
while (result.Length != 0)
{
using (var reader = new StreamReader(result))
{
if (page == lastPage)
{
//Go on and find out last counted element
int counter = 1;
var div = (double)last / limit;
double truncate = Math.Truncate(div);
var rest = div - truncate;
int lastElem = (int)(rest * limit);
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (counter <= lastElem)
{
//skip
counter++;
continue;
parameters.Add("limit", limit.ToString());
}
else
if (page > 0)
{
pidList.Add(new EpicData() { EpicPid = line });
counter++;
}
parameters.Add("page", page.ToString());
}
page++;
// Build the request URL
var url = new Uri(QueryHelpers.AddQueryString(ServiceEndpoint, parameters));
// Execute the request
var result = EpicRequestWrapper(() => _httpClient.GetAsync(url));
if (result.IsSuccessStatusCode)
{
var content = result.Content.ReadAsStringAsync().Result;
content.Split('\n').ToList().ForEach(e => pidList.Add(new EpicData() { EpicPid = e }));
}
else
{
while (!reader.EndOfStream)
{
pidList.Add(new EpicData() { EpicPid = reader.ReadLine() });
}
page++;
}
}
query = string.Format("limit={0}&page={1}", limit, page);
result = EpicRequestWrapper(() => restClient.HttpRaw("GET", new Uri(ServiceEndpoint), query, null, User, Password));
Console.WriteLine();
}
return pidList;
}
catch (Exception) //404 Exception
public IEnumerable<EpicData> ListAll(int limit = 1000, int startPage = 1)
{
var pidList = new List<EpicData>();
var tempList = List(limit, startPage);
while (tempList.Any())
{
return -1;
pidList.AddRange(tempList);
startPage++;
tempList = List(limit, startPage);
}
return pidList.Count();
return pidList;
}
public void Delete(string suffix)
{
EpicRequestWrapper(() => restClient.HttpText("DELETE", new Uri(ServiceEndpoint + suffix), null, null, User, Password));
var result = EpicRequestWrapper(() => _httpClient.DeleteAsync(ServiceEndpoint + suffix));
}
public IEnumerable<EpicData> Get(string suffix)
{
try
{
return EpicRequestWrapper(() => restClient.HttpGetJson<List<EpicData>>(new Uri(ServiceEndpoint + suffix), null, User, Password));
var pidList = new List<EpicData>();
var result = EpicRequestWrapper(() => _httpClient.GetAsync(ServiceEndpoint + suffix));
if (result.IsSuccessStatusCode)
{
var content = result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<List<EpicData>>(content);
}
return pidList;
}
catch (WebException e)
{
......@@ -188,7 +172,7 @@ namespace Coscine.ProxyApi.Utils
}
// Wrapper for requests to the Epic server since sometimes it gives a 500, however the request was valid
public T EpicRequestWrapper<T>(Func<T> request)
public static HttpResponseMessage EpicRequestWrapper(Func<Task<HttpResponseMessage>> request)
{
var count = 0;
var maxCount = 10;
......@@ -197,7 +181,8 @@ namespace Coscine.ProxyApi.Utils
{
try
{
return request();
var requestTask = request();
return requestTask.Result;
}
catch (Exception e)
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils.Exceptions
namespace Coscine.ProxyApi.Utils.Exceptions
{
public class ApiRequestException : CustomException
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils.Exceptions
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Coscine.ProxyApi.Utils
{
public interface IEpicClient
{
string Prefix { get; }
int CountForKpis(int lastPage, int last, int limit);
EpicData Create(List<EpicData> payload, string guid = null);
void Delete(string suffix);
IEnumerable<EpicData> Get(string suffix);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment