more work on api & rename to Pilz.Net
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public delegate void GetOcsApiAuthCredentailsEventHandler(object sender, GetOcsApiAuthCredentailsEventArgs eventArgs);
|
||||
|
||||
public class GetOcsApiAuthCredentailsEventArgs : EventArgs
|
||||
{
|
||||
public OcsApiAuthCredentials? Credentials { get; set; }
|
||||
}
|
||||
228
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApi.cs
Normal file
228
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApi.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
/* Nicht gemergte Änderung aus Projekt "Pilz.Networking.CloudProviders.Nextcloud (net6.0)"
|
||||
Vor:
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Http.Headers;
|
||||
using Newtonsoft.Json;
|
||||
Nach:
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Client.Cloud;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Client.Core;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Client.LoginFlowV2;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.Ocs.Responses;
|
||||
using System;
|
||||
using System.Json;
|
||||
*/
|
||||
|
||||
/* Nicht gemergte Änderung aus Projekt "Pilz.Networking.CloudProviders.Nextcloud (net6.0)"
|
||||
Vor:
|
||||
using
|
||||
Nach:
|
||||
using
|
||||
*/
|
||||
using Pilz.Net.CloudProviders.Nextcloud;
|
||||
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public class OcsApi : IDisposable
|
||||
{
|
||||
public const string CONTENT_TYPE_JSON = "application/json";
|
||||
|
||||
public event GetOcsApiAuthCredentailsEventHandler? GetOcsApiAuthCredentails;
|
||||
|
||||
private readonly HttpClient client = new();
|
||||
private readonly List<OcsApiBase> apis = new();
|
||||
|
||||
public string BaseUrl { get; set; } = string.Empty;
|
||||
|
||||
public OcsApiLoginFlowV2 LoginFlowV2 => GetApi<OcsApiLoginFlowV2>();
|
||||
public OcsApiCore Core => GetApi<OcsApiCore>();
|
||||
public OcsApiCloud Cloud => GetApi<OcsApiCloud>();
|
||||
|
||||
public TApi GetApi<TApi>() where TApi : OcsApiBase
|
||||
{
|
||||
var instance = TryGetApi<TApi>();
|
||||
return instance is null ? throw new NullReferenceException() : instance;
|
||||
}
|
||||
|
||||
public TApi? TryGetApi<TApi>() where TApi : OcsApiBase
|
||||
{
|
||||
TApi? instance = (TApi?)apis.FirstOrDefault(n => n is TApi);
|
||||
|
||||
instance ??= (TApi?)Activator.CreateInstance(typeof(TApi), new object[] { this });
|
||||
|
||||
if (instance is not null)
|
||||
apis.Add(instance);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public string BuildFullUrl(OcsApiUrlPath path)
|
||||
{
|
||||
return BaseUrl + path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments and deserialize it to the given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse"></typeparam>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns the given OcsResponse type from the deserialized OcsApiResponse content.</returns>
|
||||
public TResponse? MakeRequestOcs<TResponse>(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null) where TResponse : IOcsResponse
|
||||
{
|
||||
return MakeRequestOcs<TResponse>(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments and deserialize it to the given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse"></typeparam>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns the given OcsResponse type from the deserialized OcsApiResponse content.</returns>
|
||||
public TResponse? MakeRequestOcs<TResponse>(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null) where TResponse : IOcsResponse
|
||||
{
|
||||
var response = MakeRequest<OcsApiResponse<TResponse>?>(httpMethod, url, useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
|
||||
if (response != null)
|
||||
return response.Ocs;
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments and deserialize it to the given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse"></typeparam>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns the deserialized content of type given type.</returns>
|
||||
public TResponse? MakeRequest<TResponse>(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
|
||||
{
|
||||
return MakeRequest<TResponse>(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments and deserialize it to the given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse"></typeparam>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns the deserialized content of type given type.</returns>
|
||||
public TResponse? MakeRequest<TResponse>(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
|
||||
{
|
||||
using var responseInit = MakeRequest(httpMethod, url, useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
|
||||
if (responseInit != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bodyInit = responseInit.Content.ReadAsStringAsync().Result;
|
||||
return JsonConvert.DeserializeObject<TResponse>(bodyInit);
|
||||
}
|
||||
catch (FormatException) { }
|
||||
catch (JsonSerializationException) { }
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments.
|
||||
/// </summary>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns a HttpResponseMessage as result.</returns>
|
||||
public HttpResponseMessage MakeRequest(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
|
||||
{
|
||||
return MakeRequest(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes an request with the given arguments.
|
||||
/// </summary>
|
||||
/// <param name="httpMethod"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="useAuthentication"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns>Returns a HttpResponseMessage as result.</returns>
|
||||
public HttpResponseMessage MakeRequest(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
|
||||
{
|
||||
OcsApiAuthCredentials? authentication;
|
||||
string @params;
|
||||
HttpContent? httpContent;
|
||||
|
||||
// Get authentication
|
||||
if (useAuthentication)
|
||||
{
|
||||
var args = new GetOcsApiAuthCredentailsEventArgs();
|
||||
GetOcsApiAuthCredentails?.Invoke(this, args);
|
||||
authentication = args.Credentials;
|
||||
}
|
||||
else
|
||||
authentication = null;
|
||||
|
||||
// Parse params
|
||||
if (parameters != null)
|
||||
@params = "?" + string.Join("&", parameters.Select(p => $"{p.Key}={p.Value}"));
|
||||
else
|
||||
@params = string.Empty;
|
||||
|
||||
// Create content
|
||||
if (content is HttpContent contentHttp)
|
||||
httpContent = contentHttp;
|
||||
else if (content is OcsData || content is not null)
|
||||
{
|
||||
var stringContent = JsonConvert.SerializeObject(content);
|
||||
httpContent = new StringContent(stringContent, null, CONTENT_TYPE_JSON);
|
||||
}
|
||||
else
|
||||
httpContent = null;
|
||||
|
||||
// Send request
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
Method = httpMethod ?? HttpMethod.Post,
|
||||
RequestUri = new Uri(url + @params),
|
||||
Headers =
|
||||
{
|
||||
{ "Accept", CONTENT_TYPE_JSON },
|
||||
{ "OCS-APIREQUEST", "true" },
|
||||
//{ "Authorization", authentication.ToBasicAuth() }
|
||||
},
|
||||
Content = httpContent
|
||||
};
|
||||
|
||||
// Add authorization
|
||||
if (authentication != null)
|
||||
request.Headers.Add("Authorization", authentication.ToBasicAuth());
|
||||
|
||||
return client.Send(request);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
client.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public struct OcsApiAuthCredentials
|
||||
{
|
||||
public string LoginName { get; set; }
|
||||
public string AppPassword { get; set; }
|
||||
|
||||
public OcsApiAuthCredentials(string loginName, string appPassword)
|
||||
{
|
||||
LoginName = loginName;
|
||||
AppPassword = appPassword;
|
||||
}
|
||||
}
|
||||
11
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiBase.cs
Normal file
11
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiBase.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public abstract class OcsApiBase
|
||||
{
|
||||
protected OcsApi Manager { get; init; }
|
||||
|
||||
protected OcsApiBase(OcsApi manager)
|
||||
{
|
||||
Manager = manager;
|
||||
}
|
||||
}
|
||||
10
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiResponse.cs
Normal file
10
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public class OcsApiResponse<TOcsResponse> where TOcsResponse : IOcsResponse
|
||||
{
|
||||
[JsonProperty("ocs")]
|
||||
public TOcsResponse? Ocs { get; set; }
|
||||
}
|
||||
24
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiUrlPath.cs
Normal file
24
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsApiUrlPath.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public readonly struct OcsApiUrlPath
|
||||
{
|
||||
private readonly string path;
|
||||
|
||||
public OcsApiUrlPath()
|
||||
{
|
||||
path = string.Empty;
|
||||
}
|
||||
|
||||
public OcsApiUrlPath(string path)
|
||||
{
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public static implicit operator string(OcsApiUrlPath o) => o.path;
|
||||
public static implicit operator OcsApiUrlPath(string o) => new(o);
|
||||
|
||||
public override readonly string ToString()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
5
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsData.cs
Normal file
5
Pilz.Net.CloudProviders.Nextcloud/OCS/OcsData.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS;
|
||||
|
||||
public class OcsData
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public interface IOcsResponse
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public interface IOcsResponseData
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public interface IOcsResponseMeta
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public class OcsResponse<TMeta, TData> : IOcsResponse where TMeta : IOcsResponseMeta where TData : IOcsResponseData
|
||||
{
|
||||
[JsonProperty("meta")]
|
||||
public TMeta? Meta { get; set; }
|
||||
|
||||
[JsonProperty("data")]
|
||||
public TData? Data { get; set; }
|
||||
}
|
||||
|
||||
public class OcsResponse<TData> : OcsResponse<OcsResponseMeta, TData> where TData : IOcsResponseData
|
||||
{
|
||||
}
|
||||
|
||||
public class OcsResponse : OcsResponse<OcsResponseMeta, OcsResponseData>
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public class OcsResponseData : IOcsResponseData
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public class OcsResponseDataArray<TEntry> : List<TEntry>, IOcsResponseData where TEntry : OcsResponseDataEntry
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public class OcsResponseDataEntry
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Pilz.Net.CloudProviders.Nextcloud.OCS.Responses;
|
||||
|
||||
public class OcsResponseMeta : IOcsResponseMeta
|
||||
{
|
||||
[JsonProperty("status")]
|
||||
public string? Status { get; set; }
|
||||
|
||||
[JsonProperty("statuscode")]
|
||||
public int? StatusCode { get; set; }
|
||||
|
||||
[JsonProperty("message")]
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user