fixes for Nextcloud

This commit is contained in:
2023-10-02 15:25:16 +02:00
parent c7f5de4974
commit b2ef1e5cce
21 changed files with 233 additions and 101 deletions

View File

@@ -19,21 +19,21 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs.Apps
{
}
public async Task<bool> CreateRetentionRule(OcsDataRetentionRule rule)
public bool CreateRetentionRule(OcsDataRetentionRule rule)
{
var response = await Manager.MakeRequest(HttpMethod.Post, OCS_FILE_RETENTION_RULES, content: rule);
var response = Manager.MakeRequest(HttpMethod.Post, OCS_FILE_RETENTION_RULES, content: rule);
return response.IsSuccessStatusCode;
}
public async Task<bool> DeleteRetentionRule(int ruleID)
public bool DeleteRetentionRule(int ruleID)
{
var response = await Manager.MakeRequest(HttpMethod.Delete, OCS_FILE_RETENTION_RULE.FillParameters(ruleID));
var response = Manager.MakeRequest(HttpMethod.Delete, OCS_FILE_RETENTION_RULE.FillParameters(ruleID));
return response.IsSuccessStatusCode;
}
public Task<OcsResponseRetention?> GetRetentionRules()
public OcsResponseRetention? GetRetentionRules()
{
return Manager.MakeRequest<OcsResponseRetention>(HttpMethod.Get, OCS_FILE_RETENTION_RULES);
return Manager.MakeRequestOcs<OcsResponseRetention>(HttpMethod.Get, OCS_FILE_RETENTION_RULES);
}
}
}

View File

@@ -8,7 +8,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
{
public abstract class OcsApiBase
{
public OcsApi Manager { get; init; }
protected OcsApi Manager { get; init; }
protected OcsApiBase(OcsApi manager)
{

View File

@@ -15,9 +15,9 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
{
}
public Task<OcsResponseUser?> GetUserMeta(string username)
public OcsResponseUser? GetUserMeta(string username)
{
return Manager.MakeRequest<OcsResponseUser>(HttpMethod.Get, OCS_CLOUD_USER_METADATA.FillParameters(username));
return Manager.MakeRequestOcs<OcsResponseUser>(HttpMethod.Get, OCS_CLOUD_USER_METADATA.FillParameters(username));
}
}
}

View File

@@ -14,9 +14,9 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
{
}
public async Task<bool> DeleteAppPassword()
public bool DeleteAppPassword()
{
using var msg = await Manager.MakeRequest(HttpMethod.Delete, OCS_CORE_APPPASSWORD);
using var msg = Manager.MakeRequest(HttpMethod.Delete, OCS_CORE_APPPASSWORD);
return msg != null && msg.IsSuccessStatusCode;
}
}

View File

@@ -15,12 +15,12 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
{
}
public Task<OcsResponseLoginFlowV2?> Init(string url)
public OcsResponseLoginFlowV2? Init(string url)
{
return Manager.MakeRequest<OcsResponseLoginFlowV2>(HttpMethod.Get, url + OCS_LOGIN_INIT);
}
public Task<OcsResponseLoginFlowV2Credentials?> Poll(OcsResponseLoginFlowV2.PollData poll)
public OcsResponseLoginFlowV2Credentials? Poll(OcsResponseLoginFlowV2.PollData poll)
{
ArgumentNullException.ThrowIfNull(poll?.Endpoint);
ArgumentNullException.ThrowIfNull(poll?.Token);

View File

@@ -10,6 +10,8 @@ using System.Net;
using Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs;
using System.Net.Sockets;
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Data;
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses;
using System.Xml.Linq;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
{
@@ -51,30 +53,103 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
return BaseUrl + path;
}
public Task<TResponse?> MakeRequest<TResponse>(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
/// <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);
}
public Task<HttpResponseMessage> MakeRequest(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
/// <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)
{
return MakeRequest(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters, content: content);
}
public async Task<TResponse?> MakeRequest<TResponse>(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
{
using var responseInit = await MakeRequest(httpMethod, url, useAuthentication: useAuthentication, parameters: parameters, content: content);
using var responseInit = MakeRequest(httpMethod, url, useAuthentication: useAuthentication, parameters: parameters, content: content);
if (responseInit != null)
{
var bodyInit = await responseInit.Content.ReadAsStringAsync();
var bodyInit = responseInit.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<TResponse>(bodyInit);
}
return default;
}
public async Task<HttpResponseMessage> MakeRequest(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
/// <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;
@@ -118,7 +193,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
Content = httpContent
};
return await client.SendAsync(request);
return client.Send(request);
}
public void Dispose()

View File

@@ -0,0 +1,17 @@
using Newtonsoft.Json;
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
{
public class OcsApiResponse<TOcsResponse> where TOcsResponse : IOcsResponse
{
[JsonProperty("ocs")]
public TOcsResponse? Ocs { get; set; }
}
}

View File

@@ -12,32 +12,32 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.Cloud
public class ResponseQuota
{
[JsonProperty("free")]
public ulong? Free { get; set; }
public long? Free { get; set; }
[JsonProperty("used")]
public ulong? Used { get; set; }
public long? Used { get; set; }
[JsonProperty("total")]
public ulong? Total { get; set; }
public long? Total { get; set; }
[JsonProperty("relative")]
public float? Relative { get; set; }
[JsonProperty("quota")]
public ulong? Quota { get; set; }
public long? Quota { get; set; }
}
public class ResponseBackendCapabilities
{
[JsonProperty("setDisplayName")]
public int? SetDisplayName { get; set; }
public bool? SetDisplayName { get; set; }
[JsonProperty("setPassword")]
public int? SetPassword { get; set; }
public bool? SetPassword { get; set; }
}
[JsonProperty("enabled")]
public int? Enabled { get; set; }
public bool? Enabled { get; set; }
[JsonProperty("storageLocation")]
public string? StorageLocation { get; set; }
@@ -46,7 +46,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.Cloud
public string? ID { get; set; }
[JsonProperty("lastLogin")]
public ulong? LastLogin { get; set; }
public long? LastLogin { get; set; }
[JsonProperty("backend")]
public string? Backend { get; set; }

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
{
public interface IOcsResponse
{
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
{
public interface IOcsResponseMeta
{
}
}

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
{
public class OcsResponse<TMeta, TData> where TMeta : OcsResponseMeta where TData : IOcsResponseData
public class OcsResponse<TMeta, TData> : IOcsResponse where TMeta : IOcsResponseMeta where TData : IOcsResponseData
{
[JsonProperty("meta")]
public TMeta? Meta { get; set; }
@@ -20,7 +20,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
{
}
public class OcsResponse : OcsResponse<OcsResponseMeta, IOcsResponseData>
public class OcsResponse : OcsResponse<OcsResponseMeta, OcsResponseData>
{
}
}

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
{
public class OcsResponseMeta
public class OcsResponseMeta : IOcsResponseMeta
{
[JsonProperty("status")]
public string? Status { get; set; }