Skip to content

Commit

Permalink
Started working on the datastores API.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmikecreations committed Dec 14, 2022
1 parent 588bebc commit ccc5ebc
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace GeoTools.GeoServer.Models
{
/// <summary>
/// Wrapper object in order to comply with current API encoding.
/// </summary>
public class DataStoreListWrapper
{
[JsonPropertyName("dataStore")]
public IList<NamedLink> DataStore { get; }

[JsonConstructor]
public DataStoreListWrapper(IList<NamedLink> dataStore)
{
DataStore = dataStore;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;

namespace GeoTools.GeoServer.Models
{
/// <summary>
/// Datastores.
/// </summary>
public class DataStoresListResponse
{
[JsonPropertyName("dataStores")]
public DataStoreListWrapper DataStores { get; }

[JsonConstructor]
public DataStoresListResponse(DataStoreListWrapper dataStores)
{
DataStores = dataStores;
}
}
}
26 changes: 26 additions & 0 deletions src/GeoTools.GeoServer.Abstractions/Services/IDatastoreService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using GeoTools.GeoServer.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace GeoTools.GeoServer.Services
{
public interface IDatastoreService
{

/// <summary>
/// Get a list of data stores.
/// </summary>
/// <param name="workspaceName">The name of the worskpace containing the data stores.</param>
/// <param name="token"></param>
/// <returns>List all data stores in workspace ws.</returns>
/// <remarks>
/// <para>200: OK. Returns the list of models.</para>
/// <para>401: Missing auth configuration. Returns null / <seealso cref="UnauthorizedAccessException"/>.</para>
/// <para>404: Workspace not found.</para>
/// <para>Def: Returns null / <seealso cref="ArgumentOutOfRangeException"/>.</para>
/// </remarks>
Task<GeoServerResponse<IList<NamedLink>>> GetDatastoresAsync(string workspaceName, CancellationToken token);
}
}
96 changes: 96 additions & 0 deletions src/GeoTools.GeoServer/Services/DatastoreService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using GeoTools.GeoServer.Models;
using System.Threading;
using GeoTools.GeoServer.Helpers;
using GeoTools.GeoServer.Resources;
using System.Net.Http.Json;
using System.Net;
using System.Xml.Linq;

namespace GeoTools.GeoServer.Services
{
public class DatastoreService : IDatastoreService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<DatastoreService> _logger;
private readonly GeoServerOptions _options;
private readonly JsonSerializerOptions _jsonOpt;

public DatastoreService(IHttpClientFactory httpClientFactory, IServiceProvider provider, IOptions<GeoServerOptions> options)
{
_httpClientFactory = httpClientFactory;
_logger = provider.GetService<ILogger<DatastoreService>>();
_options = options.Value;
_jsonOpt = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
UnknownTypeHandling = JsonUnknownTypeHandling.JsonElement,
};
}

public async Task<GeoServerResponse<IList<NamedLink>>> GetDatastoresAsync(string workspaceName, CancellationToken token)
{
using (HttpClient client = _httpClientFactory.CreateClient(GeoServerOptions.HttpClientName))
{
try
{
var request = new HttpRequestMessage(HttpMethod.Get, $"workspaces/{workspaceName}/datastores");

var response = await client.SendAsync(request, token);

switch (response.StatusCode)
{
case HttpStatusCode.OK:
var result = await response.Content.ReadFromJsonAsync<DataStoresListResponse>(_jsonOpt, token);
_logger.LogInformation(string.Format(
Messages.Request_200OK,
request.RequestUri));
return new GeoServerResponse<IList<NamedLink>>((int)response.StatusCode, result.DataStores.DataStore);
case HttpStatusCode.NotFound:
_logger.LogInformation(string.Format(
Messages.Request_404NotFound,
request.RequestUri));
return new GeoServerResponse<IList<NamedLink>>((int)response.StatusCode, null);
case HttpStatusCode.Unauthorized:
throw new GeoServerClientException((int)response.StatusCode, null,
new UnauthorizedAccessException(string.Format(
Messages.Request_401Unauthorized,
request.RequestUri)));
default:
throw new GeoServerClientException((int)response.StatusCode, null,
new ArgumentOutOfRangeException(
nameof(response.StatusCode),
response.StatusCode,
string.Format(
Messages.Value_OutOfRange,
nameof(response.StatusCode),
"{200,401,404}")));
}
}
catch (GeoServerClientException e)
{
if (_options.IgnoreServerErrors)
{
_logger.LogWarning(e, nameof(GetDatastoresAsync));
return new GeoServerResponse<IList<NamedLink>>(e.StatusCode, null);
}
else
{
_logger.LogError(e, nameof(GetDatastoresAsync));
throw e.InnerException;
}
}
}
}
}
}

0 comments on commit ccc5ebc

Please sign in to comment.