Skip to content

Commit

Permalink
Merge pull request #358 from arangodb/feature-3.8/DE-194-support-for-…
Browse files Browse the repository at this point in the history
…views-api

Support for Views API
  • Loading branch information
Zyqsempai authored Apr 29, 2022
2 parents 5f08f11 + 109b6a5 commit 3c5c5b3
Show file tree
Hide file tree
Showing 21 changed files with 1,044 additions and 2 deletions.
141 changes: 141 additions & 0 deletions arangodb-net-standard.Test/ViewApi/ViewApiClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.ViewApi;
using ArangoDBNetStandard.ViewApi.Models;
using ArangoDBNetStandard.Transport;
using Moq;
using Xunit;

namespace ArangoDBNetStandardTest.ViewApi
{
public class ViewApiClientTest : IClassFixture<ViewApiClientTestFixture>, IAsyncLifetime
{
private ViewApiClient _viewApi;
private ArangoDBClient _adb;

public ViewApiClientTest(ViewApiClientTestFixture fixture)
{
_adb = fixture.ArangoDBClient;
_viewApi = _adb.View;
}

public Task InitializeAsync()
{
return Task.CompletedTask;
}

public Task DisposeAsync()
{
return Task.CompletedTask;
}

[Fact]
public async Task GetAllViewsAsync_ShouldSucceed()
{
var testName = "GetAllViewsAsyncShouldSucceedView";
var createResponse = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
var res = await _viewApi.GetAllViewsAsync();

Assert.Equal(HttpStatusCode.OK, res.Code);
Assert.False(res.Error);
Assert.NotNull(res.Result);
Assert.NotEmpty(res.Result);
}

[Fact]
public async Task PostCreateViewAsync_ShouldSucceed()
{
var testName = "PostCreateViewAsyncShouldSucceedView";
var res = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
Assert.NotNull(res);
Assert.NotNull(res.GloballyUniqueId);
}

[Fact]
public async Task DeleteViewAsync_ShouldSucceed()
{
var testName = "DeleteViewAsyncShouldSucceedView";
var createResponse = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
var res = await _viewApi.DeleteViewAsync(testName);

Assert.Equal(HttpStatusCode.OK, res.Code);
Assert.False(res.Error);
Assert.True(res.Result);
}

[Fact]
public async Task GetViewAsync_ShouldSucceed()
{
var testName = "GetViewAsyncShouldSucceedView";
var createResponse = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
var res = await _viewApi.GetViewAsync(testName);

Assert.Equal(HttpStatusCode.OK, res.Code);
Assert.False(res.Error);
}

[Fact]
public async Task GetViewPropertiesAsync_ShouldSucceed()
{
var testName = "GetViewPropertiesAsyncShouldSucceedView";
var createResponse = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
var res = await _viewApi.GetViewPropertiesAsync(testName);

Assert.Equal(HttpStatusCode.OK, res.Code);
Assert.False(res.Error);
Assert.NotNull(res.GloballyUniqueId);
}

[Fact]
public async Task PutRenameViewAsync_ShouldSucceed()
{
var testName = "PutRenameViewAsyncShouldSucceedView";
var newName = "PutRenameViewAsyncShouldSucceedViewRenamed";
var createResponse = await _viewApi.PostCreateViewAsync(
new ViewDetails()
{
Name = testName,
Type = "arangosearch"
});
var res = await _viewApi.PutRenameViewAsync(
testName,
new PutRenameViewBody()
{
Name = newName
});

Assert.Equal(HttpStatusCode.OK, res.Code);
Assert.False(res.Error);
Assert.NotNull(res.GloballyUniqueId);
Assert.Equal(newName, res.Name);
}
}
}
46 changes: 46 additions & 0 deletions arangodb-net-standard.Test/ViewApi/ViewApiClientTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ArangoDBNetStandard;
using ArangoDBNetStandard.CollectionApi.Models;
using ArangoDBNetStandard.ViewApi.Models;
using System;
using System.Threading.Tasks;

namespace ArangoDBNetStandardTest.ViewApi
{
public class ViewApiClientTestFixture : ApiClientTestFixtureBase
{
public ArangoDBClient ArangoDBClient { get; internal set; }

public ViewApiClientTestFixture()
{
}

public override async Task InitializeAsync()
{
await base.InitializeAsync();

Console.WriteLine("ViewApiClientTestFixture.InitializeAsync() started.");

string dbName = nameof(ViewApiClientTestFixture);
await CreateDatabase(dbName);
Console.WriteLine("Database " + dbName + " created successfully");

ArangoDBClient = GetArangoDBClient(dbName);
try
{
var dbRes = await ArangoDBClient.Database.GetCurrentDatabaseInfoAsync();
if (dbRes.Error)
throw new Exception("GetCurrentDatabaseInfoAsync failed: " + dbRes.Code.ToString());
else
{
Console.WriteLine("In database " + dbRes.Result.Name);
}
}
catch (ApiErrorException ex)
{
Console.WriteLine(ex.Message);
throw ex;
}

}
}
}
9 changes: 8 additions & 1 deletion arangodb-net-standard/ArangoDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using ArangoDBNetStandard.Transport;
using ArangoDBNetStandard.Transport.Http;
using ArangoDBNetStandard.UserApi;
using ArangoDBNetStandard.ViewApi;

namespace ArangoDBNetStandard
{
Expand Down Expand Up @@ -77,7 +78,12 @@ public class ArangoDBClient : IArangoDBClient
public IndexApiClient Index { get; private set; }

/// <summary>
/// Index management API.
/// View management API.
/// </summary>
public ViewApiClient View { get; private set; }

/// <summary>
/// Analyzer management API.
/// </summary>
public AnalyzerApiClient Analyzer { get; private set; }

Expand Down Expand Up @@ -144,6 +150,7 @@ private void InitializeApis(
Graph = new GraphApiClient(transport, serialization);
User = new UserApiClient(transport, serialization);
Index = new IndexApiClient(transport, serialization);
View = new ViewApiClient(transport, serialization);
Analyzer = new AnalyzerApiClient(transport, serialization);
}
}
Expand Down
8 changes: 7 additions & 1 deletion arangodb-net-standard/IArangoDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ArangoDBNetStandard.IndexApi;
using ArangoDBNetStandard.TransactionApi;
using ArangoDBNetStandard.UserApi;
using ArangoDBNetStandard.ViewApi;

namespace ArangoDBNetStandard
{
Expand Down Expand Up @@ -66,8 +67,13 @@ public interface IArangoDBClient : IDisposable
IndexApiClient Index { get; }

/// <summary>
/// View management API.
/// </summary>
ViewApiClient View { get; }

/// <summary>
/// Analyzer API.
/// </summary>
AnalyzerApiClient Analyzer { get; }
}
}
}
79 changes: 79 additions & 0 deletions arangodb-net-standard/ViewApi/IViewsApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using ArangoDBNetStandard.ViewApi.Models;
using System.Threading.Tasks;

namespace ArangoDBNetStandard.ViewApi
{
/// <summary>
/// Defines a client to access the ArangoDB API for Views.
/// </summary>
public interface IViewApiClient
{
/// <summary>
/// Gets a list of all views in a database,
/// regardless of their type.
/// GET /_api/view
/// </summary>
/// <returns></returns>
Task<GetAllViewsResponse> GetAllViewsAsync();

/// <summary>
/// Create a new View
/// POST /_api/view
/// </summary>
/// <param name="body">The body of the request containing required properties.</param>
/// <returns></returns>
Task<ViewResponse> PostCreateViewAsync(ViewDetails body);

/// <summary>
/// Delete / drop a view
/// DELETE /_api/view/{view-name}
/// </summary>
/// <param name="viewNameOrId">The name or identifier of the view to drop.</param>
/// <returns></returns>
Task<DeleteViewResponse> DeleteViewAsync(string viewNameOrId);

/// <summary>
/// Get information about a view
/// GET /_api/view/{view-name}
/// </summary>
/// <param name="viewNameOrId">The name or identifier of the view to drop.</param>
/// <returns></returns>
Task<GetViewResponse> GetViewAsync(string viewNameOrId);

/// <summary>
/// Get the properties of a view
/// GET /_api/view/{view-name}/properties
/// </summary>
/// <param name="viewNameOrId">The name or identifier of the view to drop.</param>
/// <returns></returns>
Task<GetViewPropertiesResponse> GetViewPropertiesAsync(string viewNameOrId);

/// <summary>
/// Partially changes properties of a view
/// PATCH /_api/view/{view-name}/properties
/// </summary>
/// <param name="viewNameOrId">The name or identifier of the view.</param>
/// <param name="body">The body of the request containing required properties.</param>
/// <returns></returns>
Task<ViewResponse> PatchViewPropertiesAsync(string viewNameOrId, ViewDetails body);

/// <summary>
/// Changes all properties of a view
/// PUT /_api/view/{view-name}/properties
/// </summary>
/// <param name="viewName">The name of the view.</param>
/// <param name="body">The body of the request containing required properties.</param>
/// <returns></returns>
Task<ViewResponse> PutViewPropertiesAsync(string viewName, ViewDetails body);

/// <summary>
/// Renames a view
/// PUT /_api/view/{view-name}/rename
/// </summary>
/// <param name="viewName">The name of the view.</param>
/// <param name="body">The body of the request containing required properties.</param>
/// <returns></returns>
/// <remarks>Note: This method is not available in a cluster.</remarks>
Task<PutRenameViewResponse> PutRenameViewAsync(string viewName, PutRenameViewBody body);
}
}
14 changes: 14 additions & 0 deletions arangodb-net-standard/ViewApi/Models/DeleteViewResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ArangoDBNetStandard.ViewApi.Models
{
/// <summary>
/// Response from <see cref="IViewApiClient.DeleteViewAsync(string)"/>
/// </summary>
public class DeleteViewResponse :ResponseBase
{
/// <summary>
/// Indicates whether the delete
/// operation was successful.
/// </summary>
public bool Result { get; set; }
}
}
15 changes: 15 additions & 0 deletions arangodb-net-standard/ViewApi/Models/GetAllViewsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace ArangoDBNetStandard.ViewApi.Models
{
/// <summary>
/// Response from <see cref="IViewApiClient.GetAllViewsAsync"/>
/// </summary>
public class GetAllViewsResponse : ResponseBase
{
/// <summary>
/// List of views
/// </summary>
public List<ViewSummary> Result { get; set; }
}
}
20 changes: 20 additions & 0 deletions arangodb-net-standard/ViewApi/Models/GetViewPropertiesResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Net;

namespace ArangoDBNetStandard.ViewApi.Models
{
/// <summary>
/// Response from <see cref="IViewApiClient.GetViewPropertiesAsync(string)"/>
/// </summary>
public class GetViewPropertiesResponse : ViewResponse
{
/// <summary>
/// Indicates whether an error occurred
/// </summary>
public bool Error { get; set; }

/// <summary>
/// The HTTP status code.
/// </summary>
public HttpStatusCode Code { get; set; }
}
}
20 changes: 20 additions & 0 deletions arangodb-net-standard/ViewApi/Models/GetViewResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Net;

namespace ArangoDBNetStandard.ViewApi.Models
{
/// <summary>
/// Response from <see cref="IViewApiClient.GetViewAsync(string)"/>
/// </summary>
public class GetViewResponse : ViewSummary
{
/// <summary>
/// Indicates whether an error occurred
/// </summary>
public bool Error { get; set; }

/// <summary>
/// The HTTP status code.
/// </summary>
public HttpStatusCode Code { get; set; }
}
}
Loading

0 comments on commit 3c5c5b3

Please sign in to comment.