Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • Issue/1910-MigrationtoNET6.0
  • Issue/2259-updatePids
  • Issue/2309-docs
  • Issue/2463-newCoscinePIDTypes
  • Product/1287-dotnet5Sharepoint
  • Product/407-net5migration
  • Sprint/2021-01
  • Sprint/2021-03
  • Sprint/2022-01
  • Topic/1226-proxyApiLibraryMigration
  • Topic/1334-dotnet5migration
  • dev
  • gitkeep
  • master
  • v1.0.0
  • v1.1.0
  • v1.2.0
  • v1.2.1
  • v1.3.0
  • v1.4.0
  • v2.0.0
  • v2.1.0
  • v3.0.0
  • v3.0.1
  • v3.0.2
  • v3.0.3
26 results

Target

Select target project
  • coscine/backend/libraries/proxyapi
1 result
Select Git revision
  • Issue/1910-MigrationtoNET6.0
  • Issue/2259-updatePids
  • Issue/2309-docs
  • Issue/2463-newCoscinePIDTypes
  • Product/1287-dotnet5Sharepoint
  • Product/407-net5migration
  • Sprint/2021-01
  • Sprint/2021-03
  • Sprint/2022-01
  • Topic/1226-proxyApiLibraryMigration
  • Topic/1334-dotnet5migration
  • dev
  • gitkeep
  • master
  • v1.0.0
  • v1.1.0
  • v1.2.0
  • v1.2.1
  • v1.3.0
  • v1.4.0
  • v2.0.0
  • v2.1.0
  • v3.0.0
  • v3.0.1
  • v3.0.2
  • v3.0.3
26 results
Show changes

Commits on Source 4

Showing
with 173 additions and 205 deletions
...@@ -4,8 +4,6 @@ using NUnit.Framework; ...@@ -4,8 +4,6 @@ using NUnit.Framework;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Tests namespace Coscine.ProxyApi.Tests
{ {
...@@ -15,6 +13,7 @@ namespace Coscine.ProxyApi.Tests ...@@ -15,6 +13,7 @@ namespace Coscine.ProxyApi.Tests
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly string _prefix; private readonly string _prefix;
private readonly EpicClient _epicClient; private readonly EpicClient _epicClient;
private readonly string _guid;
public EpicClientTests() public EpicClientTests()
{ {
...@@ -29,38 +28,88 @@ namespace Coscine.ProxyApi.Tests ...@@ -29,38 +28,88 @@ namespace Coscine.ProxyApi.Tests
_configuration.GetString("coscine/global/epic/user"), _configuration.GetString("coscine/global/epic/user"),
_configuration.GetString("coscine/global/epic/password") _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] [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 EpicData url = new EpicData
{ {
Type = "URL", 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 EpicData metaurl = new EpicData
{ {
Type = "METAURL", 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}"
)
}; };
EpicData dataurl = new EpicData
List<EpicData> list = new List<EpicData> {
Type = "DATAURL",
ParsedData = _configuration.GetStringAndWait(
"coscine/global/epic/pid/dataurl",
$"{baseUrl}/{subpath}"
)
};
return new List<EpicData>
{ {
url, url,
metaurl, 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());
} }
} }
} }
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<RootNamespace>Coscine.ProxyApi.Tests</RootNamespace> <RootNamespace>Coscine.ProxyApi.Tests</RootNamespace>
<AssemblyName>Coscine.ProxyApi.Tests</AssemblyName> <AssemblyName>Coscine.ProxyApi.Tests</AssemblyName>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
</PropertyGroup> <Version>3.0.0</Version></PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ProxyApi\ProxyApi.csproj" /> <ProjectReference Include="..\ProxyApi\ProxyApi.csproj" />
</ItemGroup> </ItemGroup>
......
...@@ -2,11 +2,6 @@ ...@@ -2,11 +2,6 @@
using Coscine.ProxyApi.TokenValidator; using Coscine.ProxyApi.TokenValidator;
using Coscine.ProxyApi.Utils; using Coscine.ProxyApi.Utils;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Tests namespace Coscine.ProxyApi.Tests
{ {
......
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Coscine.ProxyApi namespace Coscine.ProxyApi
{ {
......
using System.Collections.Generic; using Coscine.ProxyApi.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Coscine.ProxyApi.Utils;
using Newtonsoft.Json;
namespace Coscine.ProxyApi namespace Coscine.ProxyApi
{ {
......
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
<RootNamespace>Coscine.ProxyApi</RootNamespace> <RootNamespace>Coscine.ProxyApi</RootNamespace>
<AssemblyName>Coscine.ProxyApi</AssemblyName> <AssemblyName>Coscine.ProxyApi</AssemblyName>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Version>2.1.0</Version> <Version>3.0.0</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<Authors>RWTH Aachen University</Authors> <Authors>RWTH Aachen University</Authors>
<Company>IT Center, RWTH Aachen University</Company> <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> <Description>ProxyApi is a part of the Coscine group.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://git.rwth-aachen.de/coscine/backend/libraries/ProxyApi</PackageProjectUrl> <PackageProjectUrl>https://git.rwth-aachen.de/coscine/backend/libraries/ProxyApi</PackageProjectUrl>
......
using System; namespace Coscine.ProxyApi
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi
{ {
public static class Settings public static class Settings
{ {
private static string oAuth2ContextUrlJson = "https://oauth.campus.rwth-aachen.de/oauth2waitress/oauth2.svc/context2"; 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; } get { return oAuth2ContextUrlJson; }
set { oAuth2ContextUrlJson = value; } set { oAuth2ContextUrlJson = value; }
} }
......
using Coscine.Configuration; using Coscine.Configuration;
using Coscine.ProxyApi.Utils; using Coscine.ProxyApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator namespace Coscine.ProxyApi.TokenValidator
{ {
......
using System; namespace Coscine.ProxyApi.TokenValidator
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator
{ {
public interface ITokenValidator public interface ITokenValidator
{ {
......
using Coscine.Configuration; using Coscine.Configuration;
using Coscine.ProxyApi.Utils; using 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.TokenValidator namespace Coscine.ProxyApi.TokenValidator
{ {
......
using Coscine.ProxyApi.Utils; using Coscine.ProxyApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator namespace Coscine.ProxyApi.TokenValidator
{ {
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.TokenValidator namespace Coscine.ProxyApi.TokenValidator
{ {
......
using System; using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
......
using Coscine.ProxyApi.Utils.Exceptions; using Coscine.ProxyApi.Utils.Exceptions;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
......
using System; using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
......
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
......
using System; using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web; using System.Web;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
public class EpicClient : IEpicClient public class EpicClient : IEpicClient
{ {
private readonly RestClient restClient = new RestClient(); private readonly HttpClient _httpClient = new();
public const int errorPrefix = 12000; public const int errorPrefix = 12000;
private string ServiceEndpoint private string ServiceEndpoint { get; set; }
{
get; set;
}
private string User
{
get; set;
}
private string Password
{
get; set;
}
public string Prefix public string Prefix { get; private set; }
{
get; private set;
}
public EpicClient(string url, string prefix, string user, string password) public EpicClient(string url, string prefix, string user, string password)
{ {
ServiceEndpoint = url; ServiceEndpoint = url;
User = user;
Password = password;
Prefix = prefix; 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) private void ReplacePayload(string suffix, List<EpicData> payload)
...@@ -56,122 +47,115 @@ namespace Coscine.ProxyApi.Utils ...@@ -56,122 +47,115 @@ namespace Coscine.ProxyApi.Utils
public EpicData Update(string suffix, List<EpicData> payload) public EpicData Update(string suffix, List<EpicData> payload)
{ {
ReplacePayload(suffix, 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) public EpicData Create(List<EpicData> payload, string guid = null)
{ {
string suffix = guid; string suffix = guid;
if (guid == null) if (suffix is null)
{ {
suffix = Guid.NewGuid().ToString(); suffix = Guid.NewGuid().ToString();
} }
ReplacePayload(suffix, payload);
return Update(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>(); var pidList = new List<EpicData>();
using (var reader = new StreamReader(result))
{ // Handle additional query parameters
while (!reader.EndOfStream) var parameters = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(searchUrl))
{ {
pidList.Add(new EpicData() { EpicPid = reader.ReadLine() }); parameters.Add("URL", searchUrl);
}
}
return pidList;
} }
if (limit > 0)
public IEnumerable<EpicData> ListAll(int limit = 1000, int startPage = 1)
{ {
var pidList = new List<EpicData>(); parameters.Add("limit", limit.ToString());
var tempList = List(limit, startPage); }
// Build the request URL
while (tempList.Count() > 0) 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); var content = result.Content.ReadAsStringAsync().Result;
startPage++; content.Split('\n').ToList().ForEach(e => pidList.Add(new EpicData() { EpicPid = e }));
tempList = List(limit, startPage);
} }
return pidList; 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>(); 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)); parameters.Add("limit", limit.ToString());
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;
} }
else if (page > 0)
{ {
pidList.Add(new EpicData() { EpicPid = line }); parameters.Add("page", page.ToString());
counter++;
}
} }
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 else
{ {
while (!reader.EndOfStream) Console.WriteLine();
{
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));
} }
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) 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) public IEnumerable<EpicData> Get(string suffix)
{ {
try 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) catch (WebException e)
{ {
...@@ -188,7 +172,7 @@ namespace Coscine.ProxyApi.Utils ...@@ -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 // 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 count = 0;
var maxCount = 10; var maxCount = 10;
...@@ -197,7 +181,8 @@ namespace Coscine.ProxyApi.Utils ...@@ -197,7 +181,8 @@ namespace Coscine.ProxyApi.Utils
{ {
try try
{ {
return request(); var requestTask = request();
return requestTask.Result;
} }
catch (Exception e) catch (Exception e)
{ {
......
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils namespace Coscine.ProxyApi.Utils
{ {
......
using System; namespace Coscine.ProxyApi.Utils.Exceptions
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.ProxyApi.Utils.Exceptions
{ {
public class ApiRequestException : CustomException public class ApiRequestException : CustomException
{ {
......
using System; 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
{ {
......