Skip to content

Commit

Permalink
feat: add crud methods for models api (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
d1mak3 authored Aug 31, 2023
1 parent 7d257fb commit c866949
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Casdoor.Client/CasdoorClient.ModelApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 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.Json;

namespace Casdoor.Client;

public partial class CasdoorClient
{
public virtual async Task<CasdoorResponse?> AddModelAsync(CasdoorModel model, CancellationToken cancellationToken = default) =>
await PostAsJsonAsync(_options.GetActionUrl("add-model"), model, cancellationToken);

public virtual async Task<CasdoorResponse?> UpdateModelAsync(CasdoorModel model, string modelId,
CancellationToken cancellationToken = default)
{
string url = _options.GetActionUrl("update-model", new QueryMapBuilder().Add("id", modelId).QueryMap);
return await PostAsJsonAsync(url, model, cancellationToken);
}

public virtual async Task<CasdoorResponse?> DeleteModelAsync(CasdoorModel model, CancellationToken cancellationToken = default)
{
string url = _options.GetActionUrl("delete-model");
return await PostAsJsonAsync(url, model, cancellationToken);
}

public virtual async Task<CasdoorModel?> GetModelAsync(string name, string? owner = null, CancellationToken cancellationToken = default)
{
var queryMap = new QueryMapBuilder().Add("id", $"{owner ?? _options.OrganizationName}/{name}").QueryMap;
string url = _options.GetActionUrl("get-model", queryMap);
var result = await _httpClient.GetFromJsonAsync<CasdoorResponse?>(url, cancellationToken: cancellationToken);
return result.DeserializeData<CasdoorModel?>();
}

public virtual async Task<IEnumerable<CasdoorModel>?> GetModelsAsync(string? owner = null, CancellationToken cancellationToken = default)
{
var queryMap = new QueryMapBuilder().Add("owner", owner ?? _options.OrganizationName).QueryMap;
string url = _options.GetActionUrl("get-models", queryMap);
var result = await _httpClient.GetFromJsonAsync<CasdoorResponse?>(url, cancellationToken: cancellationToken);
return result.DeserializeData<IEnumerable<CasdoorModel>?>();
}
}
38 changes: 38 additions & 0 deletions src/Casdoor.Client/Models/CasdoorModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 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 CasdoorModel
{
[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("modelText")]
public string? ModelText { get; set; }
}

0 comments on commit c866949

Please sign in to comment.