huge work for nextcloud api
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Data.Apps.FileRetention;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.Apps.FilesRetention;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs.Apps
|
||||
{
|
||||
public class OcsApiFilesRetention : OcsApiBase
|
||||
{
|
||||
public static readonly OcsApiUrlPath OCS_FILE_RETENTION_RULES = new("/ocs/v2.php/apps/files_retention/api/v1/retentions");
|
||||
public static readonly OcsApiUrlPath OCS_FILE_RETENTION_RULE = new("/ocs/v2.php/apps/files_retention/api/v1/retentions/{0}");
|
||||
|
||||
public OcsApiFilesRetention(OcsApi manager) : base(manager)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<bool> CreateRetentionRule(OcsDataRetentionRule rule)
|
||||
{
|
||||
var response = await Manager.MakeRequest(HttpMethod.Put, OCS_FILE_RETENTION_RULES, content: rule);
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteRetentionRule(int ruleID)
|
||||
{
|
||||
var response = await Manager.MakeRequest(HttpMethod.Delete, OCS_FILE_RETENTION_RULE.FillParameters(ruleID));
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public Task<OcsResponseRetention?> GetRetentionRules()
|
||||
{
|
||||
return Manager.MakeRequest<OcsResponseRetention>(HttpMethod.Get, OCS_FILE_RETENTION_RULES);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
|
||||
|
||||
public Task<OcsResponseUser?> GetUserMeta(string username)
|
||||
{
|
||||
return Manager.MakeRequestJson<OcsResponseUser>(HttpMethod.Get, OCS_CLOUD_USER_METADATA.FillParameters(username));
|
||||
return Manager.MakeRequest<OcsResponseUser>(HttpMethod.Get, OCS_CLOUD_USER_METADATA.FillParameters(username));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
|
||||
|
||||
public Task<OcsResponseLoginFlowV2?> Init(string url)
|
||||
{
|
||||
return Manager.MakeRequestJson<OcsResponseLoginFlowV2>(HttpMethod.Get, url + OCS_LOGIN_INIT);
|
||||
return Manager.MakeRequest<OcsResponseLoginFlowV2>(HttpMethod.Get, url + OCS_LOGIN_INIT);
|
||||
}
|
||||
|
||||
public Task<OcsResponseLoginFlowV2Credentials?> Poll(OcsResponseLoginFlowV2.PollData poll)
|
||||
@@ -25,7 +25,7 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs
|
||||
ArgumentNullException.ThrowIfNull(poll?.Endpoint);
|
||||
ArgumentNullException.ThrowIfNull(poll?.Token);
|
||||
|
||||
return Manager.MakeRequestJson<OcsResponseLoginFlowV2Credentials?>(HttpMethod.Get, poll.Endpoint,
|
||||
return Manager.MakeRequest<OcsResponseLoginFlowV2Credentials?>(HttpMethod.Get, poll.Endpoint,
|
||||
parameters: new Dictionary<string, string>
|
||||
{
|
||||
{ "token", poll.Token }
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Data.Apps.FileRetention
|
||||
{
|
||||
public class OcsDataRetentionRule : OcsData
|
||||
{
|
||||
|
||||
[JsonProperty("tagid")]
|
||||
public int? TagID { get; set; }
|
||||
|
||||
[JsonProperty("timeunit")]
|
||||
public int? TimeUnit { get; set; }
|
||||
|
||||
[JsonProperty("timeamount")]
|
||||
public int? TimeAmount { get; set; }
|
||||
|
||||
[JsonProperty("timeafter")]
|
||||
public int? TimeAfter { get; set; }
|
||||
}
|
||||
}
|
||||
12
Pilz.Networking.CloudProviders.Nextcloud/OCS/Data/OcsData.cs
Normal file
12
Pilz.Networking.CloudProviders.Nextcloud/OCS/Data/OcsData.cs
Normal 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.Data
|
||||
{
|
||||
public class OcsData
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -8,26 +8,42 @@ using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.APIs;
|
||||
using System.Net.Sockets;
|
||||
using Pilz.Networking.CloudProviders.Nextcloud.OCS.Data;
|
||||
|
||||
namespace Pilz.Networking.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 { get; init; }
|
||||
public OcsApiCore Core { get; init; }
|
||||
public OcsApiCloud Cloud { get; init; }
|
||||
public OcsApiLoginFlowV2 LoginFlowV2 => GetApi<OcsApiLoginFlowV2>();
|
||||
public OcsApiCore Core => GetApi<OcsApiCore>();
|
||||
public OcsApiCloud Cloud => GetApi<OcsApiCloud>();
|
||||
|
||||
public OcsApi()
|
||||
public TApi GetApi<TApi>() where TApi : OcsApiBase
|
||||
{
|
||||
LoginFlowV2 = new(this);
|
||||
Core = new(this);
|
||||
Cloud = new(this);
|
||||
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)
|
||||
@@ -35,19 +51,19 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
|
||||
return BaseUrl + path;
|
||||
}
|
||||
|
||||
public Task<TResponse?> MakeRequestJson<TResponse>(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null)
|
||||
public Task<TResponse?> MakeRequest<TResponse>(HttpMethod httpMethod, OcsApiUrlPath url, bool useAuthentication = true, Dictionary<string, string>? parameters = null, object? content = null)
|
||||
{
|
||||
return MakeRequestJson<TResponse>(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters);
|
||||
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)
|
||||
public Task<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);
|
||||
return MakeRequest(httpMethod, BuildFullUrl(url), useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
}
|
||||
|
||||
public async Task<TResponse?> MakeRequestJson<TResponse>(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null)
|
||||
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);
|
||||
using var responseInit = await MakeRequest(httpMethod, url, useAuthentication: useAuthentication, parameters: parameters, content: content);
|
||||
|
||||
if (responseInit != null)
|
||||
{
|
||||
@@ -58,10 +74,11 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> MakeRequest(HttpMethod httpMethod, string url, bool useAuthentication = true, Dictionary<string, string>? parameters = null)
|
||||
public async Task<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)
|
||||
@@ -79,6 +96,14 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
|
||||
else
|
||||
@params = string.Empty;
|
||||
|
||||
// Create content
|
||||
if (content is HttpContent contentHttp)
|
||||
httpContent = contentHttp;
|
||||
else if (content is OcsData)
|
||||
httpContent = new StringContent(JsonConvert.SerializeObject(content), null, CONTENT_TYPE_JSON);
|
||||
else
|
||||
httpContent = null;
|
||||
|
||||
// Send request
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
@@ -86,10 +111,11 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS
|
||||
RequestUri = new Uri(url + @params),
|
||||
Headers =
|
||||
{
|
||||
{ "Accept", "application/json" },
|
||||
{ "Accept", CONTENT_TYPE_JSON },
|
||||
{ "OCS-APIREQUEST", "true" },
|
||||
{ "Authorization", authentication.ToBasicAuth() }
|
||||
},
|
||||
Content = httpContent
|
||||
};
|
||||
|
||||
return await client.SendAsync(request);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.Apps.FilesRetention
|
||||
{
|
||||
public class OcsResponseDataEntryRetention : OcsResponseDataEntry
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int? ID { get; set; }
|
||||
|
||||
[JsonProperty("tagid")]
|
||||
public int? TagID { get; set; }
|
||||
|
||||
[JsonProperty("timeunit")]
|
||||
public int? TimeUnit { get; set; }
|
||||
|
||||
[JsonProperty("timeamount")]
|
||||
public int? TimeAmount { get; set; }
|
||||
|
||||
[JsonProperty("timeafter")]
|
||||
public int? TimeAfter { get; set; }
|
||||
|
||||
[JsonProperty("hasJob")]
|
||||
public bool? HasJob { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses.Apps.FilesRetention
|
||||
{
|
||||
public class OcsResponseRetention : OcsResponse<OcsResponseDataArray<OcsResponseDataEntryRetention>>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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 IOcsResponseData
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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 : OcsResponseData
|
||||
public class OcsResponse<TMeta, TData> where TMeta : OcsResponseMeta where TData : IOcsResponseData
|
||||
{
|
||||
[JsonProperty("meta")]
|
||||
public TMeta? Meta { get; set; }
|
||||
@@ -16,11 +16,11 @@ namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
|
||||
public TData? Data { get; set; }
|
||||
}
|
||||
|
||||
public class OcsResponse<TData> : OcsResponse<OcsResponseMeta, TData> where TData : OcsResponseData
|
||||
public class OcsResponse<TData> : OcsResponse<OcsResponseMeta, TData> where TData : IOcsResponseData
|
||||
{
|
||||
}
|
||||
|
||||
public class OcsResponse : OcsResponse<OcsResponseMeta, OcsResponseData>
|
||||
public class OcsResponse : OcsResponse<OcsResponseMeta, IOcsResponseData>
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Networking.CloudProviders.Nextcloud.OCS.Responses
|
||||
{
|
||||
public class OcsResponseData
|
||||
public class OcsResponseData : IOcsResponseData
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 class OcsResponseDataArray<TEntry> : List<TEntry>, IOcsResponseData where TEntry : OcsResponseDataEntry
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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 class OcsResponseDataEntry
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user