diff --git a/src/Casdoor.Client/Abstractions/ICasdoorAccountClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorAccountClient.cs index 6b8012d..94dd605 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorAccountClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorAccountClient.cs @@ -29,4 +29,12 @@ public interface ICasdoorAccountClient public Task GetLdapUsersAsync(string owner, string id, CancellationToken cancellationToken = default); public Task UpdateLdapAsync(string id, CasdoorLdap ldap, CancellationToken cancellationToken = default); + + public Task GetAccountAsync(CancellationToken cancellationToken = default); + + public Task ResetEmailOrPhoneAsync(CasdoorResetEmailOrPhoneForm casdoorResetEmailOrPhoneForm, CancellationToken cancellationToken = default); + + public Task User(CancellationToken cancellationToken = default); + + public Task UserInfo(CancellationToken cancellationToken = default); } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorAdpaterClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorAdpaterClient.cs new file mode 100644 index 0000000..7443941 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorAdpaterClient.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client; + +namespace Casdoor.Client; + +public interface ICasdoorAdapterClient +{ + public Task AddAdapterAsync(CasdoorAdapter adapter, CancellationToken cancellationToken = default); + public Task UpdateAdapterAsync(CasdoorAdapter adapter, IEnumerable propertyNames, CancellationToken cancellationToken = default); + public Task UpdateAdapterColumnsAsync(CasdoorAdapter adapter, IEnumerable? columns, CancellationToken cancellationToken = default); + public Task DeleteAdapterAsync(string owner, string name, CancellationToken cancellationToken = default); + public Task GetAdapterAsync(string owner, string name, CancellationToken cancellationToken = default); + public Task?> GetAdaptersAsync(string owner, CancellationToken cancellationToken = default); + public Task?> GetPaginationAdaptersAsync(string owner, int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorCertClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorCertClient.cs new file mode 100644 index 0000000..994449a --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorCertClient.cs @@ -0,0 +1,25 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorCertClient +{ + public Task AddCertAsync(CasdoorCert cert, CancellationToken cancellationToken = default); + public Task UpdateCertAsync(CasdoorCert cert, CancellationToken cancellationToken = default); + public Task DeleteCertAsync(string name, CancellationToken cancellationToken = default); + public Task GetCertAsync(string name, CancellationToken cancellationToken = default); + public Task?> GetCertsAsync(CancellationToken cancellationToken = default); + public Task?> GetGlobalCertsAsync(CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorClient.cs index 455a3f1..c560963 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorClient.cs @@ -15,9 +15,11 @@ namespace Casdoor.Client; public interface ICasdoorClient : - ICasdoorUserClient, ICasdoorTokenClient, ICasdoorResourceClient, ICasdoorServiceClient, + ICasdoorUserClient, ICasdoorTokenClient, ICasdoorResourceClient, ICasdoorServiceClient, ICasdoorWebhookClient, ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient, ICasdoorModelClient, - ICasdoorEnforcerClient, ICasdoorGroupClient, ICasdoorPlanClient + ICasdoorEnforcerClient, ICasdoorGroupClient, ICasdoorPlanClient, ICasdoorSyncerClient, ICasdoorSubscriptionClient, ICasdoorPermissionClient, + ICasdoorAdapterClient, ICasdoorCertClient, ICasdoorPaymentClient, ICasdoorPricingClient, ICasdoorProductClient, ICasdoorSessionClient, + ICasdoorRoleClient, ICasdoorRecordClient { } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorOrganizationClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorOrganizationClient.cs index cfc213d..954abb5 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorOrganizationClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorOrganizationClient.cs @@ -21,4 +21,5 @@ public interface ICasdoorOrganizationClient public Task UpdateOrganizationAsync(string id, CasdoorOrganization newOrganization, CancellationToken cancellationToken = default); public Task GetOrganizationAsync(string id, CancellationToken cancellationToken = default); public Task?> GetOrganizationsAsync(string owner, CancellationToken cancellationToken = default); + public Task?> GetOrganizationNamesAsync(string owner, CancellationToken cancellationToken = default); } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorPaymentClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorPaymentClient.cs new file mode 100644 index 0000000..5760973 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorPaymentClient.cs @@ -0,0 +1,29 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorPaymentClient +{ + public Task AddPaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default); + public Task UpdatePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default); + public Task NotifyPaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default); + public Task InvoicePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default); + public Task DeletePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default); + public Task GetPaymentAsync(string name, CancellationToken cancellationToken = default); + public Task?> GetUserPaymentsAsync(string userName,CancellationToken cancellationToken = default); + public Task?> GetPaymentsAsync(CancellationToken cancellationToken = default); + public Task?> GetPaginationPaymentsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorPermissionClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorPermissionClient.cs new file mode 100644 index 0000000..1812691 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorPermissionClient.cs @@ -0,0 +1,28 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorPermissionClient +{ + public Task AddPermissionAsync(CasdoorPermission permission, CancellationToken cancellationToken = default); + public Task DeletePermissionAsync(CasdoorPermission permission, CancellationToken cancellationToken = default); + public Task UpdatePermissionAsync(CasdoorPermission permission, string permissionId, + CancellationToken cancellationToken = default); + public Task UpdatePermissionAsyncForCoulums(CasdoorPermission permission, IEnumerable? columns, + CancellationToken cancellationToken = default); + public Task GetPermissionAsync(string id, CancellationToken cancellationToken = default); + public Task?> GetPermissionsAsync(string owner, CancellationToken cancellationToken = default); + public Task?> GetPermissionsByRoleAsync(string name, string? owner = null, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorPricingClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorPricingClient.cs new file mode 100644 index 0000000..5eb6557 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorPricingClient.cs @@ -0,0 +1,26 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorPricingClient +{ + public Task AddPricingAsync(CasdoorPricing pricing, CancellationToken cancellationToken = default); + public Task UpdatePricingAsync(CasdoorPricing pricing, CancellationToken cancellationToken = default); + public Task DeletePricingAsync(CasdoorPricing pricing, CancellationToken cancellationToken = default); + public Task GetPricingAsync(string name, CancellationToken cancellationToken = default); + public Task?> GetPricingsAsync(CancellationToken cancellationToken = default); + public Task?> GetPaginationPricingsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorProductClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorProductClient.cs new file mode 100644 index 0000000..2dc074c --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorProductClient.cs @@ -0,0 +1,27 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; +public interface ICasdoorProductClient +{ + public Task AddProductAsync(CasdoorProduct product, CancellationToken cancellationToken = default); + public Task UpdateProductAsync(CasdoorProduct product, CancellationToken cancellationToken = default); + public Task DeleteProductAsync(CasdoorProduct product, CancellationToken cancellationToken = default); + public Task GetProductAsync(string name, CancellationToken cancellationToken = default); + public Task?> GetProductsAsync(CancellationToken cancellationToken = default); + public Task?> GetPaginationProductsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); + + public Task BuyProductAsync(string name, string providerName, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorRecordClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorRecordClient.cs new file mode 100644 index 0000000..f027524 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorRecordClient.cs @@ -0,0 +1,26 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorRecordClient +{ + public Task AddRecordAsync(CasdoorRecord record, CancellationToken cancellationToken = default); + public Task UpdateRecordAsync(CasdoorRecord record, CancellationToken cancellationToken = default); + public Task DeleteRecordAsync(CasdoorRecord record, CancellationToken cancellationToken = default); + public Task GetRecordAsync(string name, CancellationToken cancellationToken = default); + public Task?> GetRecordsAsync(CancellationToken cancellationToken = default); + public Task?> GetPaginationRecordsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorResourceClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorResourceClient.cs index 19a1324..19ef3d8 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorResourceClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorResourceClient.cs @@ -20,4 +20,13 @@ public interface ICasdoorResourceClient Stream fileStream, string createdTime = "", string description = "", CancellationToken cancellationToken = default); public Task DeleteResourceAsync(string name, CancellationToken cancellationToken = default); + + public Task AddResourceAsync(CasdoorUserResource casdoorUserResource, CancellationToken cancellationToken = default); + + public Task GetResourceAsync(string name, CancellationToken cancellationToken = default); + + public Task?> GetResourcesAsync(string owner, string user, + string field, string value, string sortField, string sortOrder, CancellationToken cancellationToken = default); + + public Task?> GetPaginationResourcesAsync(string owner, string user, int pageSize, int p, string field, string value, string sortField, string sortOrder, CancellationToken cancellationToken = default); } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorRoleClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorRoleClient.cs new file mode 100644 index 0000000..8d94033 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorRoleClient.cs @@ -0,0 +1,26 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorRoleClient +{ + public Task AddRoleAsync(CasdoorRole role, CancellationToken cancellationToken = default); + public Task UpdateRoleAsync(CasdoorRole role, string name, string? owner = null, + CancellationToken cancellationToken = default); + public Task DeleteRoleAsync(CasdoorRole role, CancellationToken cancellationToken = default); + public Task GetRoleAsync(string name, string? owner = null, + CancellationToken cancellationToken = default); + public Task?> GetRolesAsync(string? owner = null, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorServiceClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorServiceClient.cs index 675d392..82b17ed 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorServiceClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorServiceClient.cs @@ -20,4 +20,6 @@ public interface ICasdoorServiceClient public Task SendEmailAsync(string title, string content, string sender, IEnumerable receivers, CancellationToken cancellationToken = default); + + public Task SendNotification(string content, CancellationToken cancellationToken = default); } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorSessionClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorSessionClient.cs new file mode 100644 index 0000000..1075e6d --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorSessionClient.cs @@ -0,0 +1,27 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorSessionClient +{ + public Task AddSessionAsync(CasdoorSession session, CancellationToken cancellationToken = default); + public Task UpdateSessionAsync(CasdoorSession session, CancellationToken cancellationToken = default); + public Task UpdateSessionForColumnsAsync(CasdoorSession session,IEnumerable columns, CancellationToken cancellationToken = default); + public Task DeleteSessionAsync(CasdoorSession session, CancellationToken cancellationToken = default); + public Task GetSessionAsync(string name, string application, CancellationToken cancellationToken = default); + public Task?> GetSessionsAsync(CancellationToken cancellationToken = default); + public Task?> GetPaginationSessionsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorSubscriptionClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorSubscriptionClient.cs new file mode 100644 index 0000000..c308631 --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorSubscriptionClient.cs @@ -0,0 +1,32 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorSubscriptionClient +{ + public Task AddSubscriptionAsync(CasdoorSubscription casdoorSubscription, CancellationToken cancellationToken = default); + + public Task DeleteSubscriptionAsync(CasdoorSubscription casdoorSubscription, CancellationToken cancellationToken = default); + + public Task GetSubscriptionAsync(string owner, string name, CancellationToken cancellationToken = default); + public Task?> GetPaginationSubscriptions(string owner, int p, int pageSize, + List> queryMap, CancellationToken cancellationToken = default); + public Task?> GetSubscriptionsAsync(string owner, CancellationToken cancellationToken = default); + + public Task UpdateSubscriptionAsync(CasdoorSubscription casdoorSubscription, CancellationToken cancellationToken = default); +} + + + diff --git a/src/Casdoor.Client/Abstractions/ICasdoorSyncerClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorSyncerClient.cs new file mode 100644 index 0000000..35d242f --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorSyncerClient.cs @@ -0,0 +1,30 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorSyncerClient +{ + public Task AddSyncerAsync(CasdoorSyncer casdoorSyncer, CancellationToken cancellationToken = default); + + public Task DeleteSyncerAsync(CasdoorSyncer casdoorSyncer, CancellationToken cancellationToken = default); + + public Task GetSyncerAsync(string owner, string name, CancellationToken cancellationToken = default); + + public Task?> GetSyncersAsync(string owner, CancellationToken cancellationToken = default); + + public Task RunSyncerAsync(CasdoorSyncer casdoorSyncer, CancellationToken cancellationToken = default); + + public Task UpdateSyncerAsync(CasdoorSyncer casdoorSyncer, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Abstractions/ICasdoorTokenClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorTokenClient.cs index 169771d..2350439 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorTokenClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorTokenClient.cs @@ -22,4 +22,14 @@ public interface ICasdoorTokenClient public Task RequestPasswordTokenAsync(string username, string password, CancellationToken cancellationToken = default); public Task RequestAuthorizationCodeTokenAsync(string code, string redirectUri, string codeVerifier = "", CancellationToken cancellationToken = default); public Task RequestRefreshTokenAsync(string refreshToken, CancellationToken cancellationToken = default); + public Task AddTokenAsync(CasdoorToken casdoorToken, CancellationToken cancellationToken = default); + public Task DeleteTokenAsync(CasdoorToken casdoorToken, CancellationToken cancellationToken = default); + public Task GetCaptchaStatusAsync(string id, CancellationToken cancellationToken = default); + public Task GetTokenAsync(string owner, string name, CancellationToken cancellationToken = default); + public Task?> GetTokensAsync(string owner,CancellationToken cancellationToken = default); + public Task?> GetPaginationTokensAsync(string owner, int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default); + public Task UpdateTokenAsync(CasdoorToken casdoorToken, IEnumerable propertyNames, CancellationToken cancellationToken = default); + public Task UpdateTokenColumnsAsync(CasdoorToken token, IEnumerable? columns, CancellationToken cancellationToken = default); + } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorUserClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorUserClient.cs index 9852fcd..fa8d005 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorUserClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorUserClient.cs @@ -20,10 +20,14 @@ public interface ICasdoorUserClient public Task?> GetSortedUsersAsync(string sorter, int limit, string? owner = null, CancellationToken cancellationToken = default); public Task GetUserAsync(string name, string? owner = null, CancellationToken cancellationToken = default); public Task GetUserByEmailAsync(string email, string? owner = null, CancellationToken cancellationToken = default); + public Task GetUserByPhoneAsync(string phone, string? owner = null, CancellationToken cancellationToken = default); public Task AddUserAsync(CasdoorUser user, CancellationToken cancellationToken = default); public Task UpdateUserAsync(CasdoorUser user, IEnumerable propertyNames, CancellationToken cancellationToken = default ); + public Task UpdateUserByIdAsync(string userId, CasdoorUser user, CancellationToken cancellationToken); + public Task UpdateUserForColumns(CasdoorUser user, IEnumerable columns, CancellationToken cancellationToken); public Task UpdateUserForbiddenFlagAsync(CasdoorUser user, CancellationToken cancellationToken = default); public Task UpdateUserDeletedFlagAsync(CasdoorUser user, CancellationToken cancellationToken = default); public Task DeleteUserAsync(string name, CancellationToken cancellationToken = default); public Task CheckUserPasswordAsync(string name, CancellationToken cancellationToken = default); + public Task GetUserCount(string owner, int isOnline, CancellationToken cancellationToken = default); } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorWebhookClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorWebhookClient.cs new file mode 100644 index 0000000..db3a92b --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorWebhookClient.cs @@ -0,0 +1,24 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Casdoor.Client; + +public interface ICasdoorWebhookClient +{ + public Task AddWebhookAsync(CasdoorWebhook webhook, CancellationToken cancellationToken = default); + public Task DeleteWebhookAsync(CasdoorWebhook webhook, CancellationToken cancellationToken = default); + public Task GetWebhookAsync(string owner, string name, CancellationToken cancellationToken = default); + public Task?> GetWebhooksAsync(string owner, CancellationToken cancellationToken = default); + public Task UpdateWebhookAsync(CasdoorWebhook webhook, CancellationToken cancellationToken = default); +} diff --git a/src/Casdoor.Client/Casdoor.Client.csproj b/src/Casdoor.Client/Casdoor.Client.csproj index a9b567d..6846a94 100644 --- a/src/Casdoor.Client/Casdoor.Client.csproj +++ b/src/Casdoor.Client/Casdoor.Client.csproj @@ -50,7 +50,7 @@ - + diff --git a/src/Casdoor.Client/CasdoorClient.AccountApi.cs b/src/Casdoor.Client/CasdoorClient.AccountApi.cs index 9f09db2..477d048 100644 --- a/src/Casdoor.Client/CasdoorClient.AccountApi.cs +++ b/src/Casdoor.Client/CasdoorClient.AccountApi.cs @@ -84,15 +84,45 @@ public partial class CasdoorClient { var queryMap = new QueryMapBuilder().Add("id", $"{owner}/{id}").QueryMap; var url = _options.GetActionUrl("get-ldap-users", queryMap); - var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); return result.DeserializeData(); } public virtual Task SyncLdapUsersAsync(string owner, string id, IEnumerable users, CancellationToken cancellationToken = default) { - var queryMap = new QueryMapBuilder().Add("id", $"{owner}/{id}").QueryMap; + var queryMap = new QueryMapBuilder().Add("id", $"{owner}/{id}").QueryMap; var url = _options.GetActionUrl("sync-ldap-users", queryMap); return PostAsJsonAsync(url, users, cancellationToken); } + + public virtual async Task GetAccountAsync(CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("get-account"); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + + var casdoorUser = result.DeserializeData(); + var casdoorOrganization = result.DeserializeData2(); + + var casdoorAccount = new CasdoorAccount(casdoorUser, casdoorOrganization); + return casdoorAccount; + } + + public virtual Task ResetEmailOrPhoneAsync (CasdoorResetEmailOrPhoneForm casdoorResetEmailOrPhoneForm, CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("reset-email-or-phone"); + return PostAsJsonAsync(url, casdoorResetEmailOrPhoneForm, cancellationToken); + } + + public virtual async Task User(CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("user"); + return await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + } + + public virtual async Task UserInfo(CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("userinfo"); + return await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + } } diff --git a/src/Casdoor.Client/CasdoorClient.AdapterApi.cs b/src/Casdoor.Client/CasdoorClient.AdapterApi.cs new file mode 100644 index 0000000..cfdc244 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.AdapterApi.cs @@ -0,0 +1,88 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddAdapterAsync(CasdoorAdapter casdoorUserAdapter, CancellationToken cancellationToken = default) + { + string url = _options.GetActionUrl("add-adapter"); + return PostAsJsonAsync(url, casdoorUserAdapter, cancellationToken); + } + + public virtual Task DeleteAdapterAsync(string owner, string name, CancellationToken cancellationToken = default) + { + CasdoorAdapter adapter = new() { Owner = owner, Name = name }; + var url = _options.GetActionUrl("delete-adapter"); + return PostAsJsonAsync(url, adapter, cancellationToken); + } + + public virtual async Task GetAdapterAsync(string owner,string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{owner}/{name}").QueryMap; + string url = _options.GetActionUrl("get-adapter", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetAdaptersAsync(string owner, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner).QueryMap; + string url = _options.GetActionUrl("get-adapters", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationAdaptersAsync(string owner, int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", owner)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-adapters", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual Task UpdateAdapterAsync(CasdoorAdapter adapter, IEnumerable propertyNames, CancellationToken cancellationToken = default) + => ModifyAdapterAsync("update-adapter", adapter, propertyNames, cancellationToken: cancellationToken); + + public virtual Task UpdateAdapterColumnsAsync(CasdoorAdapter adapter, IEnumerable? columns, CancellationToken cancellationToken = default) + => ModifyAdapterAsync("update-adapter", adapter, columns, cancellationToken: cancellationToken); + + private Task ModifyAdapterAsync(string action, CasdoorAdapter adapter, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{adapter.Owner}/{adapter.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, adapter, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.CasbinApi.cs b/src/Casdoor.Client/CasdoorClient.CasbinApi.cs index 6bbd88b..6a63707 100644 --- a/src/Casdoor.Client/CasdoorClient.CasbinApi.cs +++ b/src/Casdoor.Client/CasdoorClient.CasbinApi.cs @@ -16,6 +16,7 @@ using System.Net.Http.Json; using System.Text; using System.Text.Json; +using System.Net.Http; using IdentityModel.Client; namespace Casdoor.Client; diff --git a/src/Casdoor.Client/CasdoorClient.CertApi.cs b/src/Casdoor.Client/CasdoorClient.CertApi.cs new file mode 100644 index 0000000..0df6a07 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.CertApi.cs @@ -0,0 +1,86 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddCertAsync(CasdoorCert cert, + CancellationToken cancellationToken = default) + { + string url = _options.GetActionUrl("add-cert"); + cert.Owner = _options.OrganizationName; + return PostAsJsonAsync(url, cert, cancellationToken); + } + + public virtual Task DeleteCertAsync(string name, CancellationToken cancellationToken = default) + { + CasdoorCert cert = new() { Owner = _options.OrganizationName, Name = name }; + var url = _options.GetActionUrl("delete-cert"); + return PostAsJsonAsync(url, cert, cancellationToken); + } + + public virtual async Task GetCertAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-cert", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetCertsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-certs", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetGlobalCertsAsync(CancellationToken cancellationToken = default) + { + string url = _options.GetActionUrl("get-global-certs"); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual Task UpdateCertAsync(CasdoorCert cert, CancellationToken cancellationToken = default) + => ModifyCertAsync("update-cert", cert, null, cancellationToken: cancellationToken); + + + private Task ModifyCertAsync(string action, CasdoorCert cert, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{cert.Owner}/{cert.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + cert.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, cert, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.Internal.cs b/src/Casdoor.Client/CasdoorClient.Internal.cs index b17913a..c2f0aa2 100644 --- a/src/Casdoor.Client/CasdoorClient.Internal.cs +++ b/src/Casdoor.Client/CasdoorClient.Internal.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; @@ -43,5 +44,9 @@ public partial class CasdoorClient } internal Task PostFileAsync(string? requestUri, StreamContent postStream, CancellationToken cancellationToken = default) - => _httpClient.PostFileAsync(requestUri, postStream, cancellationToken); + { + _httpClient.SetCasdoorAuthentication(_options); + return _httpClient.PostFileAsync(requestUri, postStream, cancellationToken); + } + } diff --git a/src/Casdoor.Client/CasdoorClient.OrganizationApi.cs b/src/Casdoor.Client/CasdoorClient.OrganizationApi.cs index 3c24c66..239a4e9 100644 --- a/src/Casdoor.Client/CasdoorClient.OrganizationApi.cs +++ b/src/Casdoor.Client/CasdoorClient.OrganizationApi.cs @@ -30,7 +30,7 @@ public partial class CasdoorClient public virtual async Task DeleteOrganizationAsync(string name, CancellationToken cancellationToken = default) { - var organization = new CasdoorOrganization {Owner = CasdoorConstants.DefaultCasdoorOwner, Name = name}; + var organization = new CasdoorOrganization { Owner = CasdoorConstants.DefaultCasdoorOwner, Name = name }; var url = _options.GetActionUrl("delete-organization"); return await PostAsJsonAsync(url, organization, cancellationToken); } @@ -61,4 +61,13 @@ public partial class CasdoorClient var response = await GetFromJsonAsync(url, cancellationToken: cancellationToken); return response.DeserializeData>(); } + + public virtual async Task?> GetOrganizationNamesAsync(string owner, CancellationToken cancellationToken) + + { + var queryMap = new QueryMapBuilder().Add("owner", owner).QueryMap; + var url = _options.GetActionUrl("get-organization-names", queryMap); + var response = await GetFromJsonAsync(url, cancellationToken: cancellationToken); + return response.DeserializeData>(); + } } diff --git a/src/Casdoor.Client/CasdoorClient.PaymentApi.cs b/src/Casdoor.Client/CasdoorClient.PaymentApi.cs new file mode 100644 index 0000000..979b645 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.PaymentApi.cs @@ -0,0 +1,98 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddPaymentAsync(CasdoorPayment payment, + CancellationToken cancellationToken = default) + => ModifyPaymentAsync("add-payment", payment, null, cancellationToken: cancellationToken); + + public virtual Task UpdatePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default) + => ModifyPaymentAsync("update-payment", payment, null, cancellationToken: cancellationToken); + + public virtual Task NotifyPaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default) + => ModifyPaymentAsync("notify-payment", payment, null, cancellationToken: cancellationToken); + + public virtual Task InvoicePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default) + => ModifyPaymentAsync("invoice-payment", payment, null, cancellationToken: cancellationToken); + + public virtual Task DeletePaymentAsync(CasdoorPayment payment, CancellationToken cancellationToken = default) + => ModifyPaymentAsync("delete-payment", payment, null, cancellationToken: cancellationToken); + + public virtual async Task GetPaymentAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-payment", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetUserPaymentsAsync(string userName, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner",_options.OrganizationName) + .Add("organization", _options.OrganizationName) + .Add("user", userName).QueryMap; + string url = _options.GetActionUrl("get-user-payment", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaymentsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-payments", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationPaymentsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", _options.OrganizationName)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-payments", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + private Task ModifyPaymentAsync(string action, CasdoorPayment payment, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{payment.Owner}/{payment.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + payment.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, payment, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.PermissionApi.cs b/src/Casdoor.Client/CasdoorClient.PermissionApi.cs index d0a27db..615ba3f 100644 --- a/src/Casdoor.Client/CasdoorClient.PermissionApi.cs +++ b/src/Casdoor.Client/CasdoorClient.PermissionApi.cs @@ -25,11 +25,28 @@ public partial class CasdoorClient return await PostAsJsonAsync(url, permission, cancellationToken); } - public virtual async Task UpdatePermissionAsync(CasdoorPermission permission, string permissionId, + public virtual Task UpdatePermissionAsync(CasdoorPermission permission, string permissionId, CancellationToken cancellationToken = default) + => ModifyPermissionAsync("update-permission", permission, null); + + + public virtual Task UpdatePermissionAsyncForCoulums(CasdoorPermission permission, IEnumerable? columns, + CancellationToken cancellationToken = default) + => ModifyPermissionAsync("update-permission", permission, columns); + + private Task ModifyPermissionAsync(string action, CasdoorPermission permission, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) { - string url = _options.GetActionUrl("update-permission", new QueryMapBuilder().Add("id", permissionId).QueryMap); - return await PostAsJsonAsync(url, permission, cancellationToken); + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{permission.Owner}/{permission.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, permission, cancellationToken); } public virtual async Task DeletePermissionAsync(CasdoorPermission permission, CancellationToken cancellationToken = default) @@ -38,7 +55,15 @@ public partial class CasdoorClient return await PostAsJsonAsync(url, permission, cancellationToken); } - public virtual async Task?> GetPermissionsAsync(string? owner = null, CancellationToken cancellationToken = default) + public virtual async Task GetPermissionAsync(string id, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder().Add("id", id).QueryMap; + string url = _options.GetActionUrl("get-permission", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetPermissionsAsync(string? owner = null, CancellationToken cancellationToken = default) { var queryMap = new QueryMapBuilder().Add("owner", owner ?? _options.OrganizationName).QueryMap; string url = _options.GetActionUrl("get-permissions", queryMap); @@ -46,6 +71,20 @@ public partial class CasdoorClient return result.DeserializeData?>(); } + public virtual async Task?> GetPaginatePermissionsAsync(string owner, int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", owner)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + var url = _options.GetActionUrl("get-permissions", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + public virtual async Task?> GetPermissionsByRoleAsync(string name, string? owner = null, CancellationToken cancellationToken = default) { var queryMap = new QueryMapBuilder().Add("id", $"{owner ?? _options.OrganizationName}/{name}").QueryMap; diff --git a/src/Casdoor.Client/CasdoorClient.PricingApi.cs b/src/Casdoor.Client/CasdoorClient.PricingApi.cs new file mode 100644 index 0000000..e697b41 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.PricingApi.cs @@ -0,0 +1,81 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddPricingAsync(CasdoorPricing pricing, + CancellationToken cancellationToken = default) + => ModifyPricingAsync("add-pricing", pricing, null, cancellationToken: cancellationToken); + + public virtual Task UpdatePricingAsync(CasdoorPricing pricing, CancellationToken cancellationToken = default) + => ModifyPricingAsync("update-pricing", pricing, null, cancellationToken: cancellationToken); + + public virtual Task DeletePricingAsync(CasdoorPricing pricing, CancellationToken cancellationToken = default) + => ModifyPricingAsync("delete-pricing", pricing, null, cancellationToken: cancellationToken); + + public virtual async Task GetPricingAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-pricing", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetPricingsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-pricings", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationPricingsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", _options.OrganizationName)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-pricings", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + private Task ModifyPricingAsync(string action, CasdoorPricing pricing, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{pricing.Owner}/{pricing.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + pricing.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, pricing, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.ProductApi.cs b/src/Casdoor.Client/CasdoorClient.ProductApi.cs new file mode 100644 index 0000000..3259e40 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.ProductApi.cs @@ -0,0 +1,91 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddProductAsync(CasdoorProduct product, + CancellationToken cancellationToken = default) + => ModifyProductAsync("add-product", product, null, cancellationToken: cancellationToken); + + public virtual Task UpdateProductAsync(CasdoorProduct product, CancellationToken cancellationToken = default) + => ModifyProductAsync("update-product", product, null, cancellationToken: cancellationToken); + + public virtual Task DeleteProductAsync(CasdoorProduct product, CancellationToken cancellationToken = default) + => ModifyProductAsync("delete-product", product, null, cancellationToken: cancellationToken); + + public virtual async Task GetProductAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-product", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetProductsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-products", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationProductsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", _options.OrganizationName)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-products", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + private Task ModifyProductAsync(string action, CasdoorProduct product, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{product.Owner}/{product.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + product.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, product, cancellationToken); + } + + public virtual async Task BuyProductAsync(string name, string providerName, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}") + .Add("providerName", providerName).QueryMap; + string url = _options.GetActionUrl("buy-product", queryMap); + return await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + } +} + diff --git a/src/Casdoor.Client/CasdoorClient.RecordApi.cs b/src/Casdoor.Client/CasdoorClient.RecordApi.cs new file mode 100644 index 0000000..8ccd084 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.RecordApi.cs @@ -0,0 +1,81 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddRecordAsync(CasdoorRecord record, + CancellationToken cancellationToken = default) + => ModifyRecordAsync("add-record", record, null, cancellationToken: cancellationToken); + + public virtual Task UpdateRecordAsync(CasdoorRecord record, CancellationToken cancellationToken = default) + => ModifyRecordAsync("update-record", record, null, cancellationToken: cancellationToken); + + public virtual Task DeleteRecordAsync(CasdoorRecord record, CancellationToken cancellationToken = default) + => ModifyRecordAsync("delete-record", record, null, cancellationToken: cancellationToken); + + public virtual async Task GetRecordAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-record", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetRecordsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-records", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationRecordsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", _options.OrganizationName)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-records", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + private Task ModifyRecordAsync(string action, CasdoorRecord record, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{record.Owner}/{record.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + record.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, record, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.ResourceApi.cs b/src/Casdoor.Client/CasdoorClient.ResourceApi.cs index 459b899..bc76c02 100644 --- a/src/Casdoor.Client/CasdoorClient.ResourceApi.cs +++ b/src/Casdoor.Client/CasdoorClient.ResourceApi.cs @@ -12,11 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + namespace Casdoor.Client; public partial class CasdoorClient { - // TODO: what are `createdTime` and `description` for? public virtual Task UploadResourceAsync( string user, string tag, string parent, string fullFilePath, Stream fileStream, string createdTime = "", string description = "", CancellationToken cancellationToken = default) @@ -29,12 +32,63 @@ public partial class CasdoorClient .Add("parent", parent) .Add("fullFilePath", fullFilePath).QueryMap; string url = _options.GetActionUrl("upload-resource", queryMap); - return _httpClient.PostFileAsync(url, new StreamContent(fileStream), cancellationToken: cancellationToken); + return PostFileAsync(url, new StreamContent(fileStream), cancellationToken: cancellationToken); } public virtual Task DeleteResourceAsync(string name, CancellationToken cancellationToken = default) { CasdoorUserResource resource = new() {Owner = _options.OrganizationName, Name = name}; - return PostAsJsonAsync("delete-resource", resource, cancellationToken); + var url = _options.GetActionUrl("delete-resource"); + return PostAsJsonAsync(url, resource, cancellationToken); + } + + public virtual Task AddResourceAsync(CasdoorUserResource casdoorUserResource, + CancellationToken cancellationToken = default) + { + string url = _options.GetActionUrl("add-resource"); + return PostAsJsonAsync(url, casdoorUserResource, cancellationToken); + } + + public virtual async Task GetResourceAsync(string name, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{_options.OrganizationName}/{name}").QueryMap; + string url = _options.GetActionUrl("get-resource", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetResourcesAsync(string owner, string user, + string field, string value, string sortField, string sortOrder, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner) + .Add("user", user) + .Add("field", field) + .Add("value", value) + .Add("sortField", sortField) + .Add("sortOrder", sortOrder) + .QueryMap; + string url = _options.GetActionUrl("get-resources", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationResourcesAsync(string owner, string user, int pageSize, int p, + string field, string value, string sortField, string sortOrder, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner) + .Add("user", user) + .Add("pageSize", pageSize.ToString()) + .Add("p", p.ToString()) + .Add("field", field) + .Add("value", value) + .Add("sortField", sortField) + .Add("sortOrder", sortOrder) + .QueryMap; + string url = _options.GetActionUrl("get-resources", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); } } diff --git a/src/Casdoor.Client/CasdoorClient.RoleApi.cs b/src/Casdoor.Client/CasdoorClient.RoleApi.cs index 29e6319..9bb65ee 100644 --- a/src/Casdoor.Client/CasdoorClient.RoleApi.cs +++ b/src/Casdoor.Client/CasdoorClient.RoleApi.cs @@ -37,10 +37,7 @@ public partial class CasdoorClient public virtual async Task AddRoleAsync(CasdoorRole role, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(role.Owner)) - { - role.Owner = CasdoorConstants.DefaultCasdoorOwner; - } + role.Owner = _options.OrganizationName; string url = _options.GetActionUrl("add-role"); return await PostAsJsonAsync(url, role, cancellationToken); diff --git a/src/Casdoor.Client/CasdoorClient.ServiceApi.cs b/src/Casdoor.Client/CasdoorClient.ServiceApi.cs index 9051b68..2695186 100644 --- a/src/Casdoor.Client/CasdoorClient.ServiceApi.cs +++ b/src/Casdoor.Client/CasdoorClient.ServiceApi.cs @@ -41,4 +41,14 @@ public partial class CasdoorClient string url = _options.GetActionUrl("send-email"); return PostAsJsonAsync(url, form, cancellationToken); } + + public virtual Task SendNotification(string content, CancellationToken cancellationToken = default) + { + string url = _options.GetActionUrl("send-notification"); + CasdoorNotificationForm form = new() + { + Content = content, + }; + return PostAsJsonAsync(url, form, cancellationToken); + } } diff --git a/src/Casdoor.Client/CasdoorClient.SessionApi.cs b/src/Casdoor.Client/CasdoorClient.SessionApi.cs new file mode 100644 index 0000000..3fd82f0 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.SessionApi.cs @@ -0,0 +1,84 @@ +// Copyright 2024 The Casdoor Authors.All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Net.Http; +using System.Net.Http.Json; +using System.Runtime.CompilerServices; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + + public virtual Task AddSessionAsync(CasdoorSession session, + CancellationToken cancellationToken = default) + => ModifySessionAsync("add-session", session, null, cancellationToken: cancellationToken); + + public virtual Task UpdateSessionAsync(CasdoorSession session, CancellationToken cancellationToken = default) + => ModifySessionAsync("update-session", session, null, cancellationToken: cancellationToken); + + public virtual Task UpdateSessionForColumnsAsync(CasdoorSession session, IEnumerable columns, CancellationToken cancellationToken = default) + => ModifySessionAsync("update-session", session, null, cancellationToken: cancellationToken); + + public virtual Task DeleteSessionAsync(CasdoorSession session, CancellationToken cancellationToken = default) + => ModifySessionAsync("delete-session", session, null, cancellationToken: cancellationToken); + + public virtual async Task GetSessionAsync(string name, string application, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("sessionPkId", $"{_options.OrganizationName}/{name}/{application}").QueryMap; + string url = _options.GetActionUrl("get-session", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetSessionsAsync(CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName).QueryMap; + string url = _options.GetActionUrl("get-sessions", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationSessionsAsync(int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", _options.OrganizationName)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + string url = _options.GetActionUrl("get-sessions", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData?>(); + } + + private Task ModifySessionAsync(string action, CasdoorSession session, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{session.Owner}/{session.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + session.Owner = _options.OrganizationName; + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, session, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.SubscriptionApi.cs b/src/Casdoor.Client/CasdoorClient.SubscriptionApi.cs new file mode 100644 index 0000000..f4305dc --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.SubscriptionApi.cs @@ -0,0 +1,76 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace Casdoor.Client; +public partial class CasdoorClient +{ + public virtual async Task AddSubscriptionAsync(CasdoorSubscription casdoorSubscription, + CancellationToken cancellationToken = default) + => await PostAsJsonAsync(_options.GetActionUrl("add-subscription"), casdoorSubscription, cancellationToken); + + public virtual async Task DeleteSubscriptionAsync(CasdoorSubscription casdoorSubscription, + CancellationToken cancellationToken = default) + => await PostAsJsonAsync(_options.GetActionUrl("delete-subscription"), casdoorSubscription, cancellationToken); + + public virtual async Task GetSubscriptionAsync(string owner, string name, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{owner}/{name}").QueryMap; + + string url = _options.GetActionUrl("get-subscription", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetSubscriptionsAsync(string owner, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner).QueryMap; + + string url = _options.GetActionUrl("get-subscriptions", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationSubscriptions(string owner, int p, int pageSize, + List> queryMap, CancellationToken cancellationToken = default) + { + + queryMap.Add(new KeyValuePair("owner", owner)); + queryMap.Add(new KeyValuePair("p", p.ToString())); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + + string url = _options.GetActionUrl("get-subscriptions", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual Task UpdateSubscriptionAsync(CasdoorSubscription casdoorSubscription, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{casdoorSubscription.Owner}/{casdoorSubscription.Name}").QueryMap; + string url = _options.GetActionUrl("update-subscription", queryMap); + return PostAsJsonAsync(url, casdoorSubscription, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.SyncerApi.cs b/src/Casdoor.Client/CasdoorClient.SyncerApi.cs new file mode 100644 index 0000000..b465918 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.SyncerApi.cs @@ -0,0 +1,68 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + public virtual async Task AddSyncerAsync(CasdoorSyncer casdoorSyncer, + CancellationToken cancellationToken = default) + => await PostAsJsonAsync(_options.GetActionUrl("add-syncer"), casdoorSyncer, cancellationToken); + + public virtual async Task DeleteSyncerAsync(CasdoorSyncer casdoorSyncer, + CancellationToken cancellationToken = default) + => await PostAsJsonAsync(_options.GetActionUrl("delete-syncer"), casdoorSyncer, cancellationToken); + + public virtual async Task GetSyncerAsync(string owner, string name, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{owner}/{name}").QueryMap; + + string url = _options.GetActionUrl("get-syncer", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetSyncersAsync(string owner, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner).QueryMap; + + string url = _options.GetActionUrl("get-syncers", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task RunSyncerAsync(CasdoorSyncer casdoorSyncer, + CancellationToken cancellationToken = default) + => await PostAsJsonAsync(_options.GetActionUrl("run-syncer"), casdoorSyncer, cancellationToken); + + public virtual Task UpdateSyncerAsync(CasdoorSyncer casdoorSyncer, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{casdoorSyncer.Owner}/{casdoorSyncer.Name}").QueryMap; + string url = _options.GetActionUrl("update-syncer", queryMap); + return PostAsJsonAsync(url, casdoorSyncer, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.TokenApi.cs b/src/Casdoor.Client/CasdoorClient.TokenApi.cs index 50a571e..a238e98 100644 --- a/src/Casdoor.Client/CasdoorClient.TokenApi.cs +++ b/src/Casdoor.Client/CasdoorClient.TokenApi.cs @@ -13,6 +13,7 @@ // limitations under the License. using System.IdentityModel.Tokens.Jwt; +using System.Net.Http.Json; using System.Text.Json; using IdentityModel.Client; @@ -85,4 +86,82 @@ public virtual async Task RequestRefreshTokenAsync(string refresh var result = JsonSerializer.Deserialize(jwtToken.Payload.SerializeToJson()); return result; } + + public virtual Task AddTokenAsync(CasdoorToken casdoorToken, + CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("add-token"); + return PostAsJsonAsync(url, casdoorToken, cancellationToken); + } + + public virtual Task DeleteTokenAsync(CasdoorToken casdoorToken, + CancellationToken cancellationToken = default) + { + var url = _options.GetActionUrl("delete-token"); + return PostAsJsonAsync(url, casdoorToken, cancellationToken); + } + + public virtual async Task GetCaptchaStatusAsync(string id, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder().Add("id", id).QueryMap; + var url = _options.GetActionUrl("get-captcha-status", queryMap); + return await _httpClient.GetFromJsonAsync(url, cancellationToken); + } + + public virtual async Task GetTokenAsync(string owner, string name, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{owner}/{name}").QueryMap; + + var url = _options.GetActionUrl("get-token", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetTokensAsync(string owner, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner).QueryMap; + var url = _options.GetActionUrl("get-tokens", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual async Task?> GetPaginationTokensAsync(string owner, int pageSize, int p, + List>? queryMap, CancellationToken cancellationToken = default) + { + + queryMap ??= new List>(); + queryMap.Add(new KeyValuePair("owner", owner)); + queryMap.Add(new KeyValuePair("pageSize", pageSize.ToString())); + queryMap.Add(new KeyValuePair("p", p.ToString())); + + var url = _options.GetActionUrl("get-tokens", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual Task UpdateTokenAsync(CasdoorToken token, IEnumerable propertyNames, CancellationToken cancellationToken = default) + => ModifyTokenAsync("update-token", token, propertyNames, cancellationToken: cancellationToken); + + public virtual Task UpdateTokenColumnsAsync(CasdoorToken token, IEnumerable? columns, CancellationToken cancellationToken = default) + => ModifyTokenAsync("update-token", token, columns, cancellationToken: cancellationToken); + + private Task ModifyTokenAsync(string action, CasdoorToken token, IEnumerable? columns, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMapBuilder = new QueryMapBuilder().Add("id", $"{token.Owner}/{token.Name}"); + + string columnsValue = string.Join(",", columns ?? Array.Empty()); + + if (!string.IsNullOrEmpty(columnsValue)) + { + queryMapBuilder.Add("columns", columnsValue); + } + + string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); + return PostAsJsonAsync(url, token, cancellationToken); + } } diff --git a/src/Casdoor.Client/CasdoorClient.UserApi.cs b/src/Casdoor.Client/CasdoorClient.UserApi.cs index 93d09af..f18129a 100644 --- a/src/Casdoor.Client/CasdoorClient.UserApi.cs +++ b/src/Casdoor.Client/CasdoorClient.UserApi.cs @@ -12,7 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; +using System.Collections.Generic; +using System.Globalization; using System.Net.Http.Json; +using System.Threading; namespace Casdoor.Client; @@ -65,11 +69,29 @@ public partial class CasdoorClient var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); return result.DeserializeData(); } + public virtual async Task GetUserByPhoneAsync(string phone, string? owner = null, CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner", _options.OrganizationName) + .Add("phone", phone).QueryMap; + string url = _options.GetActionUrl("get-user", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } public virtual Task AddUserAsync(CasdoorUser user, CancellationToken cancellationToken = default) => ModifyUserAsync("add-user", user, null, cancellationToken: cancellationToken); - public virtual Task UpdateUserAsync(CasdoorUser user, IEnumerable propertyNames, CancellationToken cancellationToken = default) + public virtual Task UpdateUserByIdAsync(string userId, CasdoorUser user, CancellationToken cancellationToken) + { + user.Id = userId; + return ModifyUserAsync("update-user", user, null, cancellationToken: cancellationToken); + } + + public virtual Task UpdateUserForColumns(CasdoorUser user,IEnumerable columns, CancellationToken cancellationToken) + => ModifyUserAsync("update-user", user, columns, cancellationToken: cancellationToken); + + public virtual Task UpdateUserAsync(CasdoorUser user,IEnumerable propertyNames, CancellationToken cancellationToken = default) => ModifyUserAsync("update-user", user, propertyNames, cancellationToken: cancellationToken); public Task UpdateUserForbiddenFlagAsync(CasdoorUser user, CancellationToken cancellationToken = default) @@ -84,6 +106,20 @@ public partial class CasdoorClient return UpdateUserAsync(user, new List { "is_deleted" }, cancellationToken); } + public Task SetPasswordAsync(string owner, string name, string oldPassword, string newPassword, CancellationToken cancellationToken) + { + var param = new Dictionary + { + { "owner", owner }, + { "name", name }, + { "oldPassword", oldPassword }, + { "newPassword", newPassword } + }; + + string url = _options.GetActionUrl("set-password"); + return PostAsJsonAsync(url, param, cancellationToken); + } + public virtual async Task DeleteUserAsync(string name, CancellationToken cancellationToken = default) { @@ -120,4 +156,14 @@ public partial class CasdoorClient string url = _options.GetActionUrl(action, queryMapBuilder.QueryMap); return PostAsJsonAsync(url, user, cancellationToken); } + + public virtual async Task GetUserCount(string owner,int isOnline, CancellationToken cancellationToken) + { + var queryMap = new QueryMapBuilder() + .Add("owner", owner) + .Add("isOnline", isOnline.ToString()).QueryMap; + string url = _options.GetActionUrl("get-user-count", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken); + return result.DeserializeData(); + } } diff --git a/src/Casdoor.Client/CasdoorClient.WebhookApi.cs b/src/Casdoor.Client/CasdoorClient.WebhookApi.cs new file mode 100644 index 0000000..e922c16 --- /dev/null +++ b/src/Casdoor.Client/CasdoorClient.WebhookApi.cs @@ -0,0 +1,65 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace Casdoor.Client; + +public partial class CasdoorClient +{ + public virtual async Task AddWebhookAsync(CasdoorWebhook webhook, + CancellationToken cancellationToken = default) => + await PostAsJsonAsync(_options.GetActionUrl("add-webhook"), webhook, cancellationToken); + + public virtual async Task DeleteWebhookAsync(CasdoorWebhook webhook, + CancellationToken cancellationToken = default) => + await PostAsJsonAsync(_options.GetActionUrl("delete-webhook"), webhook, cancellationToken); + + public virtual async Task GetWebhookAsync(string owner, string name, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{owner}/{name}").QueryMap; + + string url = _options.GetActionUrl("get-webhook", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData(); + } + + public virtual async Task?> GetWebhooksAsync(string owner, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("owner",owner).QueryMap; + + string url = _options.GetActionUrl("get-webhooks", queryMap); + var result = await _httpClient.GetFromJsonAsync(url, cancellationToken); + return result.DeserializeData?>(); + } + + public virtual Task UpdateWebhookAsync(CasdoorWebhook casdoorWebhook, + CancellationToken cancellationToken = default) + { + var queryMap = new QueryMapBuilder() + .Add("id", $"{casdoorWebhook.Owner}/{casdoorWebhook.Name}").QueryMap; + string url = _options.GetActionUrl("update-webhook", queryMap); + return PostAsJsonAsync(url, casdoorWebhook, cancellationToken); + } +} diff --git a/src/Casdoor.Client/CasdoorClient.cs b/src/Casdoor.Client/CasdoorClient.cs index 6123f30..a39e0b6 100644 --- a/src/Casdoor.Client/CasdoorClient.cs +++ b/src/Casdoor.Client/CasdoorClient.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Net.Http; using IdentityModel.Client; namespace Casdoor.Client; diff --git a/src/Casdoor.Client/Extensions/CasdoorResponseExtension.cs b/src/Casdoor.Client/Extensions/CasdoorResponseExtension.cs index db57f74..80a3fad 100644 --- a/src/Casdoor.Client/Extensions/CasdoorResponseExtension.cs +++ b/src/Casdoor.Client/Extensions/CasdoorResponseExtension.cs @@ -31,4 +31,18 @@ public static class CasdoorResponseExtension return default; } + + internal static T? DeserializeData2(this CasdoorResponse? response) + { + if (response?.Status != "ok") + { + throw new CasdoorApiException(response?.Msg); + } + if (response.Data2 is JsonElement element) + { + return element.Deserialize(); + } + + return default; + } } diff --git a/src/Casdoor.Client/Extensions/HttpClientExtension.cs b/src/Casdoor.Client/Extensions/HttpClientExtension.cs index 05c2eb7..d52f3a2 100644 --- a/src/Casdoor.Client/Extensions/HttpClientExtension.cs +++ b/src/Casdoor.Client/Extensions/HttpClientExtension.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Net.Http; using System.Net.Http.Json; using IdentityModel.Client; diff --git a/src/Casdoor.Client/Models/CadoorPricing.cs b/src/Casdoor.Client/Models/CadoorPricing.cs new file mode 100644 index 0000000..33048a8 --- /dev/null +++ b/src/Casdoor.Client/Models/CadoorPricing.cs @@ -0,0 +1,64 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.Json.Serialization; + +namespace Casdoor.Client; + +public class CasdoorPricing +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; set; } + + [JsonPropertyName("description")] + public string? Description { get; set; } + + [JsonPropertyName("plans")] + public string[]? Plans { get; set; } + + [JsonPropertyName("isEnabled")] + public bool IsEnabled { get; set; } + + [JsonPropertyName("trialDuration")] + public int TrialDuration { get; set; } + + [JsonPropertyName("application")] + public string? Application { get; set; } + + [JsonPropertyName("submitter")] + public string? Submitter { get; set; } + + [JsonPropertyName("approver")] + public string? Approver { get; set; } + + [JsonPropertyName("approveTime")] + public string? ApproveTime { get; set; } + + [JsonPropertyName("state")] + public string? State { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorAccount.cs b/src/Casdoor.Client/Models/CasdoorAccount.cs new file mode 100644 index 0000000..4a4a886 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorAccount.cs @@ -0,0 +1,98 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Text.Json.Serialization; + +namespace Casdoor.Client; + +public class CasdoorAccount +{ + public CasdoorUser? cassdoorUser { get; set; } + public CasdoorOrganization? casdoorOrganization {get; set;} + + public CasdoorAccount(CasdoorUser? cassdoorUser, CasdoorOrganization? casdoorOrganization) + { + this.cassdoorUser = cassdoorUser; + this.casdoorOrganization = casdoorOrganization; + } +} + +public class CasdoorResetEmailOrPhoneForm +{ + [JsonPropertyName("dest")] + public string? Dest { get; set;} + + [JsonPropertyName("type")] + public string? Type { get; set;} + + [JsonPropertyName("code")] + public string? Code { get; set;} +} + +public class CasdoorLaravelResponse +{ + [JsonPropertyName("created_at")] + public string? CreatedAt { get; set; } + + [JsonPropertyName("email")] + public string? Email { get; set; } + + [JsonPropertyName("email_verified_at")] + public string? EmailVerifiedAt { get; set; } + + [JsonPropertyName("id")] + public string? Id { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("updated_at")] + public string? UpdatedAt { get; set; } +} + +public class CasdoorUserInfo +{ + [JsonPropertyName("address")] + public string? Address { get; set; } + + [JsonPropertyName("aud")] + public string? Aud { get; set; } + + [JsonPropertyName("email")] + public string? Email { get; set; } + + [JsonPropertyName("email_verified")] + public string? EmailVerified { get; set; } + + [JsonPropertyName("groups")] + public List? Groups { get; set; } + + [JsonPropertyName("iss")] + public string? Iss { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("phone")] + public string? Phone { get; set; } + + [JsonPropertyName("picture")] + public string? Picture { get; set; } + + [JsonPropertyName("preferred_username")] + public string? PreferredUsername { get; set; } + + [JsonPropertyName("sub")] + public string? Sub { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorAdapter.cs b/src/Casdoor.Client/Models/CasdoorAdapter.cs new file mode 100644 index 0000000..411109e --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorAdapter.cs @@ -0,0 +1,76 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.Json.Serialization; + +namespace Casdoor.Client; + + +public class CasdoorAdapter +{ + + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + + + [JsonPropertyName("name")] + public string? Name { get; set; } + + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + + [JsonPropertyName("type")] + public string? Type { get; set; } + + + [JsonPropertyName("databaseType")] + public string? DatabaseType { get; set; } + + + [JsonPropertyName("host")] + public string? Host { get; set; } + + [JsonPropertyName("port")] + public int Port { get; set; } + + + [JsonPropertyName("user")] + public string? User { get; set; } + + + [JsonPropertyName("password")] + public string? Password { get; set; } + + + [JsonPropertyName("database")] + public string? Database { get; set; } + + + [JsonPropertyName("table")] + public string? Table { get; set; } + + [JsonPropertyName("tableNamePrefix")] + public string? TableNamePrefix { get; set; } + + [JsonPropertyName("isEnabled")] + public bool IsEnabled { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorCert.cs b/src/Casdoor.Client/Models/CasdoorCert.cs new file mode 100644 index 0000000..5563493 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorCert.cs @@ -0,0 +1,64 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.Json.Serialization; + +namespace Casdoor.Client; + +public class CasdoorCert +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; set; } + + [JsonPropertyName("scope")] + public string? Scope { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + [JsonPropertyName("cryptoAlgorithm")] + public string? CryptoAlgorithm { get; set; } + + [JsonPropertyName("bitSize")] + public int BitSize { get; set; } + + [JsonPropertyName("expireInYears")] + public int ExpireInYears { get; set; } + + [JsonPropertyName("certificate")] + public string? Certificate { get; set; } + + [JsonPropertyName("privateKey")] + public string? PrivateKey { get; set; } + + [JsonPropertyName("authorityPublicKey")] + public string? AuthorityPublicKey { get; set; } + + [JsonPropertyName("authorityRootPublicKey")] + public string? AuthorityRootPublicKey { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorEmailForm.cs b/src/Casdoor.Client/Models/CasdoorEmailForm.cs index 7181cd7..6ea6286 100644 --- a/src/Casdoor.Client/Models/CasdoorEmailForm.cs +++ b/src/Casdoor.Client/Models/CasdoorEmailForm.cs @@ -31,5 +31,5 @@ public class CasdoorEmailForm public string[] Receivers { get; set; } = Array.Empty(); [JsonPropertyName("provider")] - public string Provider { get; set; } + public string? Provider { get; set; } } diff --git a/src/Casdoor.Client/Models/CasdoorNotificationForm.cs b/src/Casdoor.Client/Models/CasdoorNotificationForm.cs new file mode 100644 index 0000000..3d0bf24 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorNotificationForm.cs @@ -0,0 +1,23 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Text.Json.Serialization; + +namespace Casdoor.Client; + +public class CasdoorNotificationForm +{ + [JsonPropertyName("content")] + public string Content { get; set; } = string.Empty; +} diff --git a/src/Casdoor.Client/Models/CasdoorPayment.cs b/src/Casdoor.Client/Models/CasdoorPayment.cs new file mode 100644 index 0000000..f7ab079 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorPayment.cs @@ -0,0 +1,110 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +using System.Text.Json.Serialization; + +public class CasdoorPayment +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; set; } + + // Payment Provider Info + [JsonPropertyName("provider")] + public string? Provider { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + // Product Info + [JsonPropertyName("productName")] + public string? ProductName { get; set; } + + [JsonPropertyName("productDisplayName")] + public string? ProductDisplayName { get; set; } + + [JsonPropertyName("detail")] + public string? Detail { get; set; } + + [JsonPropertyName("tag")] + public string? Tag { get; set; } + + [JsonPropertyName("currency")] + public string? Currency { get; set; } + + [JsonPropertyName("price")] + public double Price { get; set; } + + [JsonPropertyName("returnUrl")] + public string? ReturnUrl { get; set; } + + // Payer Info + [JsonPropertyName("user")] + public string? User { get; set; } + + [JsonPropertyName("personName")] + public string? PersonName { get; set; } + + [JsonPropertyName("personIdCard")] + public string? PersonIdCard { get; set; } + + [JsonPropertyName("personEmail")] + public string? PersonEmail { get; set; } + + [JsonPropertyName("personPhone")] + public string? PersonPhone { get; set; } + + [JsonPropertyName("invoiceType")] + public string? InvoiceType { get; set; } + + [JsonPropertyName("invoiceTitle")] + public string? InvoiceTitle { get; set; } + + [JsonPropertyName("invoiceTaxId")] + public string? InvoiceTaxId { get; set; } + + [JsonPropertyName("invoiceRemark")] + public string? InvoiceRemark { get; set; } + + [JsonPropertyName("invoiceUrl")] + public string? InvoiceUrl { get; set; } + + [JsonPropertyName("outOrderId")] + public string? OutOrderId { get; set; } + + [JsonPropertyName("payUrl")] + public string? PayUrl { get; set; } + + [JsonPropertyName("state")] + public string? State { get; set; } + + [JsonPropertyName("message")] + public string? Message { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorPermission.cs b/src/Casdoor.Client/Models/CasdoorPermission.cs index a7d2968..e2eb9d8 100644 --- a/src/Casdoor.Client/Models/CasdoorPermission.cs +++ b/src/Casdoor.Client/Models/CasdoorPermission.cs @@ -78,4 +78,7 @@ public class CasdoorPermission [JsonPropertyName("isEnabled")] public bool IsEnabled { get; set; } + + [JsonPropertyName("groups")] + public IEnumerable? Groups { get; set; } } diff --git a/src/Casdoor.Client/Models/CasdoorPlan.cs b/src/Casdoor.Client/Models/CasdoorPlan.cs index ea6d9b1..01925ad 100644 --- a/src/Casdoor.Client/Models/CasdoorPlan.cs +++ b/src/Casdoor.Client/Models/CasdoorPlan.cs @@ -30,8 +30,8 @@ public class CasdoorPlan [JsonPropertyName("pricePerMonth")] public double PricePerMonth { get; set; } [JsonPropertyName("pricePerYear")] public double PricePerYear { get; set; } - [JsonPropertyName("currency")] public string Currency { get; set; } + [JsonPropertyName("currency")] public string? Currency { get; set; } [JsonPropertyName("isEnabled")] public bool IsEnabled { get; set; } - [JsonPropertyName("role")] public string Role { get; set; } - [JsonPropertyName("options")] public List Options { get; set; } + [JsonPropertyName("role")] public string? Role { get; set; } + [JsonPropertyName("options")] public List? Options { get; set; } } diff --git a/src/Casdoor.Client/Models/CasdoorProduce.cs b/src/Casdoor.Client/Models/CasdoorProduce.cs new file mode 100644 index 0000000..d4b6dca --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorProduce.cs @@ -0,0 +1,73 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public class CasdoorProduct +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; set; } + + [JsonPropertyName("image")] + public string? Image { get; set; } + + [JsonPropertyName("detail")] + public string? Detail { get; set; } + + [JsonPropertyName("description")] + public string? Description { get; set; } + + [JsonPropertyName("tag")] + public string? Tag { get; set; } + + [JsonPropertyName("currency")] + public string? Currency { get; set; } + + [JsonPropertyName("price")] + public double? Price { get; set; } + + [JsonPropertyName("quantity")] + public int? Quantity { get; set; } + + [JsonPropertyName("sold")] + public int? Sold { get; set; } + + [JsonPropertyName("providers")] + public IEnumerable? Providers { get; set; } + + [JsonPropertyName("returnUrl")] + public string? ReturnUrl { get; set; } + + [JsonPropertyName("state")] + public string? State { get; set; } + + [JsonPropertyName("providerObjs")] + public IEnumerable? ProviderObjs { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorRecord.cs b/src/Casdoor.Client/Models/CasdoorRecord.cs new file mode 100644 index 0000000..1886661 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorRecord.cs @@ -0,0 +1,63 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; +public class CasdoorRecord +{ + [JsonPropertyName("id")] + public int Id { get; set; } + + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("organization")] + public string? Organization { get; set; } + + [JsonPropertyName("clientIp")] + public string? ClientIp { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } + + [JsonPropertyName("method")] + public string? Method { get; set; } + + [JsonPropertyName("requestUri")] + public string? RequestUri { get; set; } + + [JsonPropertyName("action")] + public string? Action { get; set; } + + [JsonPropertyName("object")] + public string? Object { get; set; } + + [JsonPropertyName("extendedUser")] + public CasdoorUser? ExtendedUser { get; set; } + + [JsonPropertyName("isTriggered")] + public bool IsTriggered { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorSession.cs b/src/Casdoor.Client/Models/CasdoorSession.cs new file mode 100644 index 0000000..33eb31a --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorSession.cs @@ -0,0 +1,40 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public class CasdoorSession +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("application")] + public string? Application { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("sessionId")] + public IEnumerable? SessionId { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorSubscription.cs b/src/Casdoor.Client/Models/CasdoorSubscription.cs new file mode 100644 index 0000000..7a4db56 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorSubscription.cs @@ -0,0 +1,70 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public class CasdoorSubscription +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; set; } + + [JsonPropertyName("startDate")] + public DateTime? StartDate { get; set; } + + [JsonPropertyName("endDate")] + public DateTime? EndDate { get; set; } + + [JsonPropertyName("duration")] + public int? Duration { get; set; } + + [JsonPropertyName("description")] + public string? Description { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } + + [JsonPropertyName("plan")] + public string? Plan { get; set; } + + [JsonPropertyName("isEnabled")] + public bool? IsEnabled { get; set; } + + [JsonPropertyName("submitter")] + public string? Submitter { get; set; } + + [JsonPropertyName("approver")] + public string? Approver { get; set; } + + [JsonPropertyName("approveTime")] + public string? ApproveTime { get; set; } + + [JsonPropertyName("state")] + public string? State { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorSyncer.cs b/src/Casdoor.Client/Models/CasdoorSyncer.cs new file mode 100644 index 0000000..5f8428b --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorSyncer.cs @@ -0,0 +1,106 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public class TableColumnsItem +{ + [JsonPropertyName("casdoorName")] + public string? CasdoorName { get; set; } + + [JsonPropertyName("isHashed")] + public bool? IsHashed { get; set; } + + [JsonPropertyName("isKey")] + public bool? IsKey { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + [JsonPropertyName("values")] + public IEnumerable? Values { get; set; } +} + +public class CasdoorSyncer +{ + [JsonPropertyName("affiliationTable")] + public string? AffiliationTable { get; set; } + + [JsonPropertyName("avatarBaseUrl")] + public string? AvatarBaseUrl { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("database")] + public string? Database { get; set; } + + [JsonPropertyName("databaseType")] + public string? DatabaseType { get; set; } + + [JsonPropertyName("errorText")] + public string? ErrorText { get; set; } + + [JsonPropertyName("host")] + public string? Host { get; set; } + + [JsonPropertyName("isEnabled")] + public bool? IsEnabled { get; set; } + + [JsonPropertyName("isReadOnly")] + public bool? IsReadOnly { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("organization")] + public string? Organization { get; set; } + + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("password")] + public string? Password { get; set; } + + [JsonPropertyName("port")] + public int Port { get; set; } + + [JsonPropertyName("sslMode")] + public string? SslMode { get; set; } + + [JsonPropertyName("syncInterval")] + public int SyncInterval { get; set; } + + [JsonPropertyName("table")] + public string? Table { get; set; } + + [JsonPropertyName("tableColumns")] + public IEnumerable? TableColumns { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorThemeData.cs b/src/Casdoor.Client/Models/CasdoorThemeData.cs index fbaeb58..b8541c7 100644 --- a/src/Casdoor.Client/Models/CasdoorThemeData.cs +++ b/src/Casdoor.Client/Models/CasdoorThemeData.cs @@ -1,3 +1,17 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + using System.Text.Json.Serialization; namespace Casdoor.Client; diff --git a/src/Casdoor.Client/Models/CasdoorToken.cs b/src/Casdoor.Client/Models/CasdoorToken.cs new file mode 100644 index 0000000..27d0331 --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorToken.cs @@ -0,0 +1,70 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; + +public class CasdoorToken +{ + [JsonPropertyName("accessToken")] + public string? AccessToken { get; set; } + + [JsonPropertyName("application")] + public string? Application { get; set; } + + [JsonPropertyName("code")] + public string? Code { get; set; } + + [JsonPropertyName("codeChallenge")] + public string? CodeChallenge { get; set; } + + [JsonPropertyName("codeExpireIn")] + public long? CodeExpireIn { get; set; } + + [JsonPropertyName("codeIsUsed")] + public bool? CodeIsUsed { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("expiresIn")] + public long? ExpiresIn { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("organization")] + public string? Organization { get; set; } + + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("refreshToken")] + public string? RefreshToken { get; set; } + + [JsonPropertyName("scope")] + public string? Scope { get; set; } + + [JsonPropertyName("tokenType")] + public string? TokenType { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } +} diff --git a/src/Casdoor.Client/Models/CasdoorUserResource.cs b/src/Casdoor.Client/Models/CasdoorUserResource.cs index 143c36d..50aeda0 100644 --- a/src/Casdoor.Client/Models/CasdoorUserResource.cs +++ b/src/Casdoor.Client/Models/CasdoorUserResource.cs @@ -23,4 +23,40 @@ public class CasdoorUserResource [JsonPropertyName("name")] public string Name { get; set; } = string.Empty; + + [JsonPropertyName("application")] + public string? Application { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("description")] + public string? Description { get; set; } + + [JsonPropertyName("fileFormat")] + public string? FileFormat { get; set; } + + [JsonPropertyName("fileName")] + public string? FileName { get; set; } + + [JsonPropertyName("fileSize")] + public long? FileSize { get; set; } + + [JsonPropertyName("fileType")] + public string? FileType { get; set; } + + [JsonPropertyName("parent")] + public string? Parent { get; set; } + + [JsonPropertyName("provider")] + public string? Provider { get; set; } + + [JsonPropertyName("tag")] + public string? Tag { get; set; } + + [JsonPropertyName("url")] + public string? Url { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } } diff --git a/src/Casdoor.Client/Models/CasdoorWebhook.cs b/src/Casdoor.Client/Models/CasdoorWebhook.cs new file mode 100644 index 0000000..c2bdd1c --- /dev/null +++ b/src/Casdoor.Client/Models/CasdoorWebhook.cs @@ -0,0 +1,84 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Casdoor.Client; +public class CasdoorWebhook +{ + [JsonPropertyName("owner")] + public string? Owner { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdTime")] + public string? CreatedTime { get; set; } + + [JsonPropertyName("organization")] + public string? Organization { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + [JsonPropertyName("host")] + public string? Host { get; set; } + + [JsonPropertyName("port")] + public int Port { get; set; } + + [JsonPropertyName("user")] + public string? User { get; set; } + + [JsonPropertyName("password")] + public string? Password { get; set; } + + [JsonPropertyName("databaseType")] + public string? DatabaseType { get; set; } + + [JsonPropertyName("database")] + public string? Database { get; set; } + + [JsonPropertyName("table")] + public string? Table { get; set; } + + [JsonPropertyName("tablePrimaryKey")] + public string? TablePrimaryKey { get; set; } + + [JsonPropertyName("tableColumns")] + public List? TableColumns { get; set; } + + [JsonPropertyName("affiliationTable")] + public string? AffiliationTable { get; set; } + + [JsonPropertyName("avatarBaseUrl")] + public string? AvatarBaseUrl { get; set; } + + [JsonPropertyName("errorText")] + public string? ErrorText { get; set; } + + [JsonPropertyName("syncInterval")] + public int SyncInterval { get; set; } + + [JsonPropertyName("isReadOnly")] + public bool IsReadOnly { get; set; } + + [JsonPropertyName("isEnabled")] + public bool IsEnabled { get; set; } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/AdapterTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/AdapterTest.cs new file mode 100644 index 0000000..4940bab --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/AdapterTest.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class AdapterTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public AdapterTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestAdapter() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + + const string ownerName = "admin"; + string name = "Adapter_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var adapter = new CasdoorAdapter() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + User = name, + Host = "https://casdoor.org" + }; + + // Add a new object + + Task responseAsync = userClient.AddAdapterAsync(adapter); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> adaptersAsync = userClient.GetAdaptersAsync(ownerName); + IEnumerable? getAdapters = await adaptersAsync; + Assert.True(getAdapters.Any()); + _testOutputHelper.WriteLine($"{getAdapters.Count()}"); + bool found = false; + foreach (CasdoorAdapter casdoorAdapter in getAdapters) + { + _testOutputHelper.WriteLine(casdoorAdapter.Name); + if (casdoorAdapter.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task adapterAsync = userClient.GetAdapterAsync(ownerName, name); + CasdoorAdapter? getAdapter = await adapterAsync; + Assert.Equal(name, getAdapter.Name); + //Update the object + const string updatedUser = "Updated Casdoor Website"; + adapter.User = updatedUser; + // Update the object + responseAsync = + userClient.UpdateAdapterAsync(adapter, null); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the update + adapterAsync = userClient.GetAdapterAsync(ownerName, name); + getAdapter = await adapterAsync; + Assert.Equal(updatedUser, getAdapter.User); + + // Delete the object + responseAsync = userClient.DeleteAdapterAsync(ownerName, name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the deletion + adapterAsync = userClient.GetAdapterAsync(ownerName, name); + getAdapter = await adapterAsync; + Assert.Null(getAdapter); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/CertsTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/CertsTest.cs new file mode 100644 index 0000000..72253c8 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/CertsTest.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class CertTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public CertTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestCert() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + + const string ownerName = "admin"; + string name = "Cert_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var cert = new CasdoorCert() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + Scope = "JWT", + Type = "x509", + CryptoAlgorithm = "RS256", + BitSize = 4096, + ExpireInYears = 20, + }; + + // Add a new object + + Task responseAsync = userClient.AddCertAsync(cert); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> certsAsync = userClient.GetCertsAsync(); + IEnumerable? getCerts = await certsAsync; + Assert.True(getCerts.Any()); + _testOutputHelper.WriteLine($"{getCerts.Count()}"); + bool found = false; + foreach (CasdoorCert casdoorCert in getCerts) + { + _testOutputHelper.WriteLine(casdoorCert.Name); + if (casdoorCert.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task certAsync = userClient.GetCertAsync(name); + CasdoorCert? getCert = await certAsync; + Assert.Equal(name, getCert.Name); + //Update the object + const string updatedDisplayName = "Updated Casdoor Website"; + cert.DisplayName = updatedDisplayName; + // Update the object + responseAsync = + userClient.UpdateCertAsync(cert); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the update + certAsync = userClient.GetCertAsync(name); + getCert = await certAsync; + Assert.Equal(updatedDisplayName, getCert.DisplayName); + + // Delete the object + responseAsync = userClient.DeleteCertAsync(name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the deletion + certAsync = userClient.GetCertAsync(name); + getCert = await certAsync; + Assert.Null(getCert); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/OrganizationTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/OrganizationTest.cs new file mode 100644 index 0000000..4039f70 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/OrganizationTest.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class OrganizationTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public OrganizationTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestOrganization() + { + var organizationClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Organization_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + _testOutputHelper.WriteLine($"test with organization name {name}"); + string owner = "admin"; + + CasdoorOrganization organization = new CasdoorOrganization() + { + Owner = owner, + Name = name, + CreatedTime = DateTime.Now.ToString(), + DisplayName = name, + WebsiteUrl = "https://example.com", + PasswordType = "plain", + PasswordOptions = new string[]{ "AtLeast6"}, + CountryCodes = new string[]{ "US", "ES", "FR", "DE", "GB", "CN", "JP", "KR", "VN", "ID", "SG", "IN"}, + Tags = new string[] { }, + Languages = new string[] { "en", "zh", "es", "fr", "de", "id", "ja", "ko", "ru", "vi", "pt" }, + InitScore = 2000, + EnableSoftDeletion = false, + IsProfilePublic = false, + }; + + // Add the object + Task responseAsync = organizationClient.AddOrganizationAsync(organization); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get the object + Task organizationAsync = organizationClient.GetOrganizationAsync($"admin/{name}"); + CasdoorOrganization? getOrganization = await organizationAsync; + Assert.Equal(name, getOrganization.Name); + + // Update the object + string updatedDisplayName = "Updated Casdoor Website"; + getOrganization.DisplayName = updatedDisplayName; + responseAsync = organizationClient.UpdateOrganizationAsync($"admin/{name}",getOrganization); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + organizationAsync = organizationClient.GetOrganizationAsync($"admin/{name}"); + getOrganization = await organizationAsync; + Assert.Equal(updatedDisplayName, getOrganization.DisplayName); + + // Delete the object + responseAsync = organizationClient.DeleteOrganizationAsync(name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + organizationAsync = organizationClient.GetOrganizationAsync($"admin/{name}"); + getOrganization = await organizationAsync; + Assert.Null(getOrganization); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/PaymentTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/PaymentTest.cs new file mode 100644 index 0000000..29fff31 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/PaymentTest.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class PaymentTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public PaymentTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestPayment() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "admin"; + string name = "Payment_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var payment = new CasdoorPayment() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + ProductName = "casbin", + }; + + // Add a new object + Task responseAsync = userClient.AddPaymentAsync(payment); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> paymentsAsync = userClient.GetPaymentsAsync(); + IEnumerable? getPayments = await paymentsAsync; + Assert.True(getPayments.Any()); + _testOutputHelper.WriteLine($"{getPayments.Count()}"); + bool found = false; + foreach (CasdoorPayment casdoorPayment in getPayments) + { + _testOutputHelper.WriteLine(casdoorPayment.Name); + if (casdoorPayment.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task paymentAsync = userClient.GetPaymentAsync(name); + CasdoorPayment? getPayment = await paymentAsync; + Assert.Equal(name, getPayment.Name); + + // Update the object + const string updatedProductName = "Updated Casdoor Payment"; + payment.ProductName = updatedProductName; + + // Update the object + responseAsync = userClient.UpdatePaymentAsync(payment); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + paymentAsync = userClient.GetPaymentAsync(name); + getPayment = await paymentAsync; + Assert.Equal(updatedProductName, getPayment.ProductName); + + // Delete the object + responseAsync = userClient.DeletePaymentAsync(getPayment); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + paymentAsync = userClient.GetPaymentAsync(name); + getPayment = await paymentAsync; + Assert.Null(getPayment); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/PermissionTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/PermissionTest.cs new file mode 100644 index 0000000..9a3567b --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/PermissionTest.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class PermissionTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public PermissionTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestPermission() + { + var permissionClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Permission_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + _testOutputHelper.WriteLine($"test with Permission name {name}"); + string owner = "casbin"; + + CasdoorPermission permission = new CasdoorPermission() + { + Owner = owner, + Name = name, + CreatedTime = DateTime.Now.ToString(), + DisplayName = name, + Description = "Casdoor Website", + Users = new string[] { "casbin/*" }, + Groups = new string[] { }, + Roles = new string[] { }, + Domains = new string[] { }, + Model = "user-model-built-in", + ResourceType = "Application", + Resources = new string[] { "app-casbin" }, + Actions = new string[] { "Read", "Write" }, + Effect = "Allow", + IsEnabled = true, + }; + + // Add the object + Task responseAsync = permissionClient.AddPermissionAsync(permission); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + await Task.Delay(1000); + + // Get all objects, check if our added object is inside the list + Task?> permissionAsyncs = permissionClient.GetPermissionsAsync(owner); + IEnumerable? getPermissions = await permissionAsyncs; + Assert.True(getPermissions.Any()); + _testOutputHelper.WriteLine($"{getPermissions.Count()}"); + bool found = false; + foreach (CasdoorPermission casdoorPermission in getPermissions) + { + _testOutputHelper.WriteLine(casdoorPermission.Name); + if (casdoorPermission.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task PermissionAsync = permissionClient.GetPermissionAsync($"{owner}/{ name}"); + CasdoorPermission ? getPermission = await PermissionAsync; + Assert.Equal(name, getPermission.Name); + + // Update the object + string updatedDescription = "Updated Code"; + getPermission.Description = updatedDescription; + responseAsync = permissionClient.UpdatePermissionAsync(getPermission, ""); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + PermissionAsync = permissionClient.GetPermissionAsync($"{owner}/{name}"); + getPermission = await PermissionAsync; + Assert.Equal(updatedDescription, getPermission.Description); + + // Delete the object + responseAsync = permissionClient.DeletePermissionAsync(permission); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + PermissionAsync = permissionClient.GetPermissionAsync($"{owner}/{name}"); + getPermission = await PermissionAsync; + Assert.Null(getPermission); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/PricingTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/PricingTest.cs new file mode 100644 index 0000000..9a0c948 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/PricingTest.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class PricingTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public PricingTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestPricing() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "casbin"; + string name = "Pricing_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var pricing = new CasdoorPricing() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + Application = "app-admin", + Description = "Casdoor Website", + }; + + // Add a new object + Task responseAsync = userClient.AddPricingAsync(pricing); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> pricingsAsync = userClient.GetPricingsAsync(); + IEnumerable? getPricings = await pricingsAsync; + Assert.True(getPricings.Any()); + _testOutputHelper.WriteLine($"{getPricings.Count()}"); + bool found = false; + foreach (CasdoorPricing casdoorPricing in getPricings) + { + _testOutputHelper.WriteLine(casdoorPricing.Name); + if (casdoorPricing.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task pricingAsync = userClient.GetPricingAsync(name); + CasdoorPricing? getPricing = await pricingAsync; + Assert.Equal(name, getPricing.Name); + + // Update the object + const string updatedDescription = "Updated Casdoor Pricing"; + pricing.Description = updatedDescription; + + // Update the object + responseAsync = userClient.UpdatePricingAsync(pricing); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + pricingAsync = userClient.GetPricingAsync(name); + getPricing = await pricingAsync; + Assert.Equal(updatedDescription, getPricing.Description); + + // Delete the object + responseAsync = userClient.DeletePricingAsync(getPricing); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + pricingAsync = userClient.GetPricingAsync(name); + getPricing = await pricingAsync; + Assert.Null(getPricing); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/ProductTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/ProductTest.cs new file mode 100644 index 0000000..b796e6c --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/ProductTest.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class ProductTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public ProductTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestProduct() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "casbin"; + string name = "Product_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var product = new CasdoorProduct() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + Image = "https://cdn.casbin.org/img/casdoor-logo_1185x256.png", + Description = "Casdoor Website", + Tag = "auto_created_product_for_plan", + Quantity = 999, + Sold = 0, + State = "Published" + }; + + // Add a new object + Task responseAsync = userClient.AddProductAsync(product); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> productsAsync = userClient.GetProductsAsync(); + IEnumerable? getProducts = await productsAsync; + Assert.True(getProducts.Any()); + _testOutputHelper.WriteLine($"{getProducts.Count()}"); + bool found = false; + foreach (CasdoorProduct casdoorProduct in getProducts) + { + _testOutputHelper.WriteLine(casdoorProduct.Name); + if (casdoorProduct.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task productAsync = userClient.GetProductAsync(name); + CasdoorProduct? getProduct = await productAsync; + Assert.Equal(name, getProduct.Name); + + // Update the object + const string updatedDescription = "Updated Casdoor Product"; + product.Description = updatedDescription; + + // Update the object + responseAsync = userClient.UpdateProductAsync(product); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + productAsync = userClient.GetProductAsync(name); + getProduct = await productAsync; + Assert.Equal(updatedDescription, getProduct.Description); + + // Delete the object + responseAsync = userClient.DeleteProductAsync(getProduct); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + productAsync = userClient.GetProductAsync(name); + getProduct = await productAsync; + Assert.Null(getProduct); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/ProviderTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/ProviderTest.cs new file mode 100644 index 0000000..5ce8873 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/ProviderTest.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class ProviderTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public ProviderTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestProvider() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "admin"; + string name = "Provider_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var provider = new CasdoorProvider() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + Category = "Captcha", + Type = "Default" + }; + + // Add a new object + Task responseAsync = userClient.AddProviderAsync(provider); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> providersAsync = userClient.GetProvidersAsync(); + IEnumerable? getProviders = await providersAsync; + Assert.True(getProviders.Any()); + _testOutputHelper.WriteLine($"{getProviders.Count()}"); + bool found = false; + foreach (CasdoorProvider casdoorProvider in getProviders) + { + _testOutputHelper.WriteLine(casdoorProvider.Name); + if (casdoorProvider.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task providerAsync = userClient.GetProviderAsync(name); + CasdoorProvider? getProvider = await providerAsync; + Assert.Equal(name, getProvider.Name); + + // Update the object + const string updatedDisplayName = "Updated Casdoor Website"; + provider.DisplayName = updatedDisplayName; + + // Update the object + responseAsync = userClient.UpdateProviderAsync(provider.Name, provider); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + providerAsync = userClient.GetProviderAsync(name); + getProvider = await providerAsync; + Assert.Equal(updatedDisplayName, getProvider.DisplayName); + + // Delete the object + responseAsync = userClient.DeleteProviderAsync(name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + providerAsync = userClient.GetProviderAsync(name); + getProvider = await providerAsync; + Assert.Null(getProvider); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/ResourceTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/ResourceTest.cs new file mode 100644 index 0000000..69c4810 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/ResourceTest.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using NuGet.Frameworks; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class ResourceTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public ResourceTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestResource() + { + var resourceClient = _servicesFixture.ServiceProvider.GetService(); + //string name = "Resource_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + string owner = "casbin"; + string filename = "ResourceUploadTest.txt"; + + string path = $"Examples/{filename}"; + string name = $"/casdoor/{filename}"; + _testOutputHelper.WriteLine($"test with resource name {name}"); + Assert.True(File.Exists(path)); + + FileStream fs = File.OpenRead(path); + + + CasdoorUserResource resource = new CasdoorUserResource() + { + Owner = owner, + Name = name, + CreatedTime = DateTime.Now.ToString(), + Description = "Casdoor Website", + User = "casbin", + FileName = filename, + FileSize = fs.Length, + Tag = name + }; + + // Add the object + Task responseAsync = resourceClient.UploadResourceAsync(resource.User, resource.Tag, "", resource.FileName, fs); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + await Task.Delay(1000); + + // Get all objects, check if our added object is inside the list + Task?> resourceAsyncs = resourceClient.GetResourcesAsync(owner, "casbin", "", "", "", ""); + IEnumerable? getResources = await resourceAsyncs; + Assert.True(getResources.Any()); + _testOutputHelper.WriteLine($"{getResources.Count()}"); + bool found = false; + foreach (CasdoorUserResource casdoorResource in getResources) + { + _testOutputHelper.WriteLine(casdoorResource.Name); + if (casdoorResource.Tag == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task resourceAsync = resourceClient.GetResourceAsync(name); + CasdoorUserResource? getResource = await resourceAsync; + Assert.Equal(name, getResource.Tag); + + // Delete the object + responseAsync = resourceClient.DeleteResourceAsync(name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + resourceAsync = resourceClient.GetResourceAsync(name); + getResource = await resourceAsync; + Assert.Null(getResource); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/RoleTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/RoleTest.cs new file mode 100644 index 0000000..dcd1b03 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/RoleTest.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class RoleTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public RoleTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestRole() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "admin"; + string name = "Role_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + _testOutputHelper.WriteLine($"start with name: {name}"); + + var role = new CasdoorRole() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + DisplayName = name, + Description = "Casdoor Website" + }; + + // Add a new object + Task responseAsync = userClient.AddRoleAsync(role); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> rolesAsync = userClient.GetRolesAsync(); + IEnumerable? getRoles = await rolesAsync; + Assert.True(getRoles.Any()); + _testOutputHelper.WriteLine($"{getRoles.Count()}"); + bool found = false; + foreach (CasdoorRole casdoorRole in getRoles) + { + _testOutputHelper.WriteLine(casdoorRole.Name); + if (casdoorRole.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task roleAsync = userClient.GetRoleAsync(name); + CasdoorRole? getRole = await roleAsync; + Assert.Equal(name, getRole.Name); + + // Update the object + const string updatedDescription = "Updated Casdoor Website"; + role.Description = updatedDescription; + + // Update the object + responseAsync = userClient.UpdateRoleAsync(role, role.Name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + roleAsync = userClient.GetRoleAsync(name); + getRole = await roleAsync; + Assert.Equal(updatedDescription, getRole.Description); + + // Delete the object + responseAsync = userClient.DeleteRoleAsync(getRole); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + roleAsync = userClient.GetRoleAsync(name); + getRole = await roleAsync; + Assert.Null(getRole); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/SessionTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/SessionTest.cs new file mode 100644 index 0000000..c222e3f --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/SessionTest.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; + +namespace Casdoor.Client.UnitTests.ApiClientTests +{ + public class SessionTest : IClassFixture + { + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public SessionTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestSession() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string ownerName = "casbin"; + string name = "Session_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + + var session = new CasdoorSession() + { + Owner = ownerName, + Name = name, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + Application = "app-built-in", + SessionId = new string[]{} + }; + + // Add a new object + Task responseAsync = userClient.AddSessionAsync(session); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> sessionsAsync = userClient.GetSessionsAsync(); + IEnumerable? getSessions = await sessionsAsync; + Assert.True(getSessions.Any()); + _testOutputHelper.WriteLine($"{getSessions.Count()}"); + bool found = false; + foreach (CasdoorSession casdoorSession in getSessions) + { + _testOutputHelper.WriteLine(casdoorSession.Name); + if (casdoorSession.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task sessionAsync = userClient.GetSessionAsync(name, session.Application); + CasdoorSession? getSession = await sessionAsync; + Assert.Equal(name, getSession.Name); + + // Update the object + string updatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture); + session.CreatedTime = updatedTime; + + // Update the object + responseAsync = userClient.UpdateSessionAsync(session); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + sessionAsync = userClient.GetSessionAsync(name, session.Application); + getSession = await sessionAsync; + Assert.Equal(updatedTime, getSession.CreatedTime); + + // Delete the object + responseAsync = userClient.DeleteSessionAsync(getSession); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + sessionAsync = userClient.GetSessionAsync(name, session.Application); + getSession = await sessionAsync; + Assert.Null(getSession); + } + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/SubscriptionTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/SubscriptionTest.cs new file mode 100644 index 0000000..cdcc25d --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/SubscriptionTest.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class SubscriptionTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public SubscriptionTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestSubscription() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Subscription_" + DateTime.Now.ToLongTimeString(); + + CasdoorSubscription subscription = new CasdoorSubscription() + { + Owner = "admin", + Name = name, + CreatedTime = DateTime.Now.ToString(), + DisplayName = name, + Description = "Casdoor Website", + }; + + // Add the object + Task responseAsync = userClient.AddSubscriptionAsync(subscription); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> subscriptionAsyncs = userClient.GetSubscriptionsAsync("admin"); + IEnumerable? getSubscriptions = await subscriptionAsyncs; + Assert.True(getSubscriptions.Any()); + _testOutputHelper.WriteLine($"{getSubscriptions.Count()}"); + bool found = false; + foreach (CasdoorSubscription casdoorSubscription in getSubscriptions) + { + _testOutputHelper.WriteLine(casdoorSubscription.Name); + if (casdoorSubscription.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task subscriptionAsync = userClient.GetSubscriptionAsync("admin", name); + CasdoorSubscription? getSubscription = await subscriptionAsync; + Assert.Equal(name, getSubscription.Name); + + // Update the object + string updateDescription = "Updated Casdoor Website"; + subscription.Description = updateDescription; + responseAsync = userClient.UpdateSubscriptionAsync(subscription); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + subscriptionAsync = userClient.GetSubscriptionAsync("admin", name); + getSubscription = await subscriptionAsync; + Assert.Equal(updateDescription, getSubscription.Description); + + // Delete the object + responseAsync = userClient.DeleteSubscriptionAsync(subscription); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + subscriptionAsync = userClient.GetSubscriptionAsync("admin", name); + getSubscription = await subscriptionAsync; + Assert.Null(getSubscription); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/SyncerTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/SyncerTest.cs new file mode 100644 index 0000000..732c304 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/SyncerTest.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class SyncerTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public SyncerTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestSyncer() + { + + var userClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Syncer_" + DateTime.Now.ToLongTimeString(); + string appName = $"admin/{name}"; + + CasdoorSyncer syncer = new CasdoorSyncer() + { + Owner = "admin", + Name = name, + CreatedTime = DateTime.Now.ToString(), + Organization = "casbin", + Host = "localhost", + Port = 3306, + User = "root", + Password = "123", + DatabaseType = "mysql", + Database = "syncer_db", + Table = "user-table", + SyncInterval = 1, + }; + + // Add the object + Task responseAsync = userClient.AddSyncerAsync(syncer); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> syncerAsyncs = userClient.GetSyncersAsync("admin"); + IEnumerable? getSyncers = await syncerAsyncs; + Assert.True(getSyncers.Any()); + _testOutputHelper.WriteLine($"{getSyncers.Count()}"); + bool found = false; + foreach (CasdoorSyncer casdoorSyncer in getSyncers) + { + _testOutputHelper.WriteLine(casdoorSyncer.Name); + if (casdoorSyncer.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task syncerAsync = userClient.GetSyncerAsync("admin", name); + CasdoorSyncer? getSyncer = await syncerAsync; + Assert.Equal(name, getSyncer.Name); + + // Update the object + getSyncer.SyncInterval = 200; + responseAsync = userClient.UpdateSyncerAsync(getSyncer); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + //Validate the update + syncerAsync = userClient.GetSyncerAsync("admin", name); + getSyncer = await syncerAsync; + Assert.Equal(200, getSyncer.SyncInterval); + + // Delete the object + responseAsync = userClient.DeleteSyncerAsync(syncer); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the deletion + syncerAsync = userClient.GetSyncerAsync("admin", name); + getSyncer = await syncerAsync; + Assert.Null(getSyncer); + + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/TokenTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/TokenTest.cs new file mode 100644 index 0000000..4dbe646 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/TokenTest.cs @@ -0,0 +1,88 @@ +using System.Globalization; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class TokenTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public TokenTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestToken() + { + var tokenClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Token_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + _testOutputHelper.WriteLine($"test with token name {name}"); + string owner = "admin"; + + CasdoorToken token = new CasdoorToken() + { + Owner = owner, + Name = name, + CreatedTime = DateTime.Now.ToString(), + Code = "abc", + AccessToken = "123456" + }; + + // Add the object + Task responseAsync = tokenClient.AddTokenAsync(token); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + await Task.Delay(1000); + + // Get all objects, check if our added object is inside the list + Task?> tokenAsyncs = tokenClient.GetTokensAsync(owner); + IEnumerable? getTokens = await tokenAsyncs; + Assert.True(getTokens.Any()); + _testOutputHelper.WriteLine($"{getTokens.Count()}"); + bool found = false; + foreach (CasdoorToken casdoorToken in getTokens) + { + _testOutputHelper.WriteLine(casdoorToken.Name); + if (casdoorToken.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task tokenAsync = tokenClient.GetTokenAsync(owner, name); + CasdoorToken? getToken = await tokenAsync; + Assert.Equal(name, getToken.Name); + + // Update the object + string updatedCode = "Updated Code"; + getToken.Code = updatedCode; + responseAsync = tokenClient.UpdateTokenAsync(getToken, new List()); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + tokenAsync = tokenClient.GetTokenAsync(owner, name); + getToken = await tokenAsync; + Assert.Equal(updatedCode, getToken.Code); + + // Delete the object + responseAsync = tokenClient.DeleteTokenAsync(token); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + tokenAsync = tokenClient.GetTokenAsync(owner, name); + getToken = await tokenAsync; + Assert.Null(getToken); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/UserTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/UserTest.cs new file mode 100644 index 0000000..33cd208 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/UserTest.cs @@ -0,0 +1,85 @@ +using System.Globalization; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class UserTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public UserTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestUser() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + string name = "User_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); + _testOutputHelper.WriteLine($"test with username {name}"); + string appName = $"admin/{name}"; + string owner = "casbin"; + + CasdoorUser user = new CasdoorUser() + { + Owner = owner, + Name = name, + CreatedTime = DateTime.Now.ToString(), + DisplayName = name, + }; + + // Add the object + Task responseAsync = userClient.AddUserAsync(user); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> userAsyncs = userClient.GetUsersAsync(owner); + IEnumerable? getUsers = await userAsyncs; + Assert.True(getUsers.Any()); + _testOutputHelper.WriteLine($"{getUsers.Count()}"); + bool found = false; + foreach (CasdoorUser casdoorUser in getUsers) + { + _testOutputHelper.WriteLine(casdoorUser.Name); + if (casdoorUser.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task userAsync = userClient.GetUserAsync(name, owner); + CasdoorUser? getUser = await userAsync; + Assert.Equal(name, getUser.Name); + + // Update the object + getUser.DisplayName = "Updated Casdoor Website"; + responseAsync = userClient.UpdateUserAsync(getUser, null); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the update + userAsync = userClient.GetUserAsync(name, owner); + getUser = await userAsync; + Assert.Equal("Updated Casdoor Website", getUser.DisplayName); + + // Delete the object + responseAsync = userClient.DeleteUserAsync(user.Name); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Validate the deletion + userAsync = userClient.GetUserAsync(name, owner); + getUser = await userAsync; + Assert.Null(getUser); + } +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/WebhookTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/WebhookTest.cs new file mode 100644 index 0000000..8b43f82 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/WebhookTest.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class WebhookTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public WebhookTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestWebhook() + { + + var userClient = _servicesFixture.ServiceProvider.GetService(); + string name = "Webhook_" + DateTime.Now.ToLongTimeString(); + string appName = $"admin/{name}"; + + CasdoorWebhook webhook = new CasdoorWebhook() + { + Owner= "casbin", + Name = name, + CreatedTime = DateTime.Now.ToString(), + Organization = "casbin", + }; + + // Add the object + Task responseAsync = userClient.AddWebhookAsync(webhook); + CasdoorResponse? response = await responseAsync; + _testOutputHelper.WriteLine($"{response.Status} {response.Msg}"); + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + // Get all objects, check if our added object is inside the list + Task?> webhookAsyncs = userClient.GetWebhooksAsync("casbin"); + IEnumerable? getWebhooks = await webhookAsyncs; + Assert.True(getWebhooks.Any()); + _testOutputHelper.WriteLine($"{getWebhooks.Count()}"); + bool found = false; + foreach (CasdoorWebhook casdoorWebhook in getWebhooks) + { + _testOutputHelper.WriteLine(casdoorWebhook.Name); + if (casdoorWebhook.Name == name) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task webhookAsync = userClient.GetWebhookAsync("casbin", name); + CasdoorWebhook? getWebhook = await webhookAsync; + Assert.Equal(name, getWebhook.Name); + + // Update the object + webhook.Organization = "admin"; + responseAsync = userClient.UpdateWebhookAsync(webhook); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + + //Validate the update + webhookAsync = userClient.GetWebhookAsync("casbin", name); + getWebhook = await webhookAsync; + Assert.Equal("admin", getWebhook.Organization); + + // Delete the object + responseAsync = userClient.DeleteWebhookAsync(webhook); // 修改为 DeleteWebhookAsync + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the deletion + webhookAsync = userClient.GetWebhookAsync("casbin", name); // 修改为 GetWebhookAsync + getWebhook = await webhookAsync; + Assert.Null(getWebhook); + + } +} diff --git a/tests/Casdoor.Client.UnitTests/Casdoor.Client.UnitTests.csproj b/tests/Casdoor.Client.UnitTests/Casdoor.Client.UnitTests.csproj index 7575840..0c76f82 100644 --- a/tests/Casdoor.Client.UnitTests/Casdoor.Client.UnitTests.csproj +++ b/tests/Casdoor.Client.UnitTests/Casdoor.Client.UnitTests.csproj @@ -25,4 +25,10 @@ + + + Always + + + diff --git a/tests/Casdoor.Client.UnitTests/Examples/ResourceUploadTest.txt b/tests/Casdoor.Client.UnitTests/Examples/ResourceUploadTest.txt new file mode 100644 index 0000000..0fac4be --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/Examples/ResourceUploadTest.txt @@ -0,0 +1 @@ +ResourceUploadTest