-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working on the datastores API.
- Loading branch information
1 parent
588bebc
commit ccc5ebc
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/GeoTools.GeoServer.Abstractions/Models/Datastores/DataStoreListWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/GeoTools.GeoServer.Abstractions/Models/Datastores/DataStoresListResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/GeoTools.GeoServer.Abstractions/Services/IDatastoreService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |