Skip to content

Commit

Permalink
Fix incorrect 404 on pretranslations endpoints
Browse files Browse the repository at this point in the history
- remove IsBuilt check
- fixes #96
  • Loading branch information
ddaspit committed Sep 6, 2023
1 parent 2fe198a commit 0e2202c
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 639 deletions.
159 changes: 13 additions & 146 deletions src/Serval.Client/Client.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,26 +970,15 @@ public partial interface ITranslationEnginesClient
/// <br/> * The references defined in the SourceFile per line, if any.
/// <br/> * An auto-generated reference of `[TextId]:[lineNumber]`, 1 indexed.
/// <br/>* **Translation**: the text of the pretranslation
/// <br/>
/// <br/>Pretranslations can be filtered by text id if provided.
/// </remarks>
/// <param name="id">The translation engine id</param>
/// <param name="corpusId">The corpus id</param>
/// <param name="textId">The text id (optional)</param>
/// <returns>The pretranslations</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslationsAsync(string id, string corpusId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Gets all pretranslations from a TextId
/// </summary>
/// <remarks>
/// Similar to "get all pretranslations of a corpus," except that the results are filtered by TextId.
/// </remarks>
/// <param name="id">The translation engine id</param>
/// <param name="corpusId">The corpus id</param>
/// <param name="textId">The text id</param>
/// <returns>The pretranslations</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslations2Async(string id, string corpusId, string textId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslationsAsync(string id, string corpusId, string? textId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
Expand Down Expand Up @@ -2588,12 +2577,15 @@ public string BaseUrl
/// <br/> * The references defined in the SourceFile per line, if any.
/// <br/> * An auto-generated reference of `[TextId]:[lineNumber]`, 1 indexed.
/// <br/>* **Translation**: the text of the pretranslation
/// <br/>
/// <br/>Pretranslations can be filtered by text id if provided.
/// </remarks>
/// <param name="id">The translation engine id</param>
/// <param name="corpusId">The corpus id</param>
/// <param name="textId">The text id (optional)</param>
/// <returns>The pretranslations</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslationsAsync(string id, string corpusId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslationsAsync(string id, string corpusId, string? textId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
if (id == null)
throw new System.ArgumentNullException("id");
Expand All @@ -2602,133 +2594,14 @@ public string BaseUrl
throw new System.ArgumentNullException("corpusId");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/translation/engines/{id}/corpora/{corpusId}/pretranslations");
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/translation/engines/{id}/corpora/{corpusId}/pretranslations?");
urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{corpusId}", System.Uri.EscapeDataString(ConvertToString(corpusId, System.Globalization.CultureInfo.InvariantCulture)));

var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
request_.Method = new System.Net.Http.HttpMethod("GET");
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

PrepareRequest(client_, request_, urlBuilder_);

var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

PrepareRequest(client_, request_, url_);

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}

ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.IList<Pretranslation>>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ServalApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
return objectResponse_.Object;
}
else
if (status_ == 401)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The client is not authenticated", status_, responseText_, headers_, null);
}
else
if (status_ == 403)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The authenticated client cannot perform the operation or does not own the translation engine", status_, responseText_, headers_, null);
}
else
if (status_ == 404)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The engine or corpus does not exist", status_, responseText_, headers_, null);
}
else
if (status_ == 405)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The method is not supported", status_, responseText_, headers_, null);
}
else
if (status_ == 409)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The engine needs to be built first", status_, responseText_, headers_, null);
}
else
if (status_ == 503)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("A necessary service is currently unavailable. Check `/health` for more details. ", status_, responseText_, headers_, null);
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
}
}
finally
{
if (disposeResponse_)
response_.Dispose();
}
}
}
finally
if (textId != null)
{
if (disposeClient_)
client_.Dispose();
urlBuilder_.Append(System.Uri.EscapeDataString("textId") + "=").Append(System.Uri.EscapeDataString(ConvertToString(textId, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Gets all pretranslations from a TextId
/// </summary>
/// <remarks>
/// Similar to "get all pretranslations of a corpus," except that the results are filtered by TextId.
/// </remarks>
/// <param name="id">The translation engine id</param>
/// <param name="corpusId">The corpus id</param>
/// <param name="textId">The text id</param>
/// <returns>The pretranslations</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<System.Collections.Generic.IList<Pretranslation>> GetAllPretranslations2Async(string id, string corpusId, string textId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
if (id == null)
throw new System.ArgumentNullException("id");

if (corpusId == null)
throw new System.ArgumentNullException("corpusId");

if (textId == null)
throw new System.ArgumentNullException("textId");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/translation/engines/{id}/corpora/{corpusId}/pretranslations/{textId}");
urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{corpusId}", System.Uri.EscapeDataString(ConvertToString(corpusId, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Replace("{textId}", System.Uri.EscapeDataString(ConvertToString(textId, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Length--;

var client_ = _httpClient;
var disposeClient_ = false;
Expand Down Expand Up @@ -2785,13 +2658,7 @@ public string BaseUrl
if (status_ == 404)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The engine or corpus or text does not exist", status_, responseText_, headers_, null);
}
else
if (status_ == 405)
{
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ServalApiException("The method is not supported", status_, responseText_, headers_, null);
throw new ServalApiException("The engine or corpus does not exist", status_, responseText_, headers_, null);
}
else
if (status_ == 409)
Expand Down
13 changes: 13 additions & 0 deletions src/Serval.Shared/Controllers/AbortedRpcExceptionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Serval.Shared.Controllers;

public class AbortedRpcExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception is RpcException rpcException && rpcException.StatusCode == StatusCode.Aborted)
{
context.Result = new ConflictObjectResult(rpcException.Message);
context.ExceptionHandled = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Serval.Shared.Controllers
{
public class HttpResultFilter : ResultFilterAttribute
public class ErrorResultFilter : ResultFilterAttribute
{
private readonly ILogger _logger;

public HttpResultFilter(ILoggerFactory loggerFactory)
public ErrorResultFilter(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpResultFilter>();
_logger = loggerFactory.CreateLogger<ErrorResultFilter>();
}

public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
Expand Down
5 changes: 3 additions & 2 deletions src/Serval.Shared/Controllers/ServalControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
[Produces("application/json")]
[TypeFilter(typeof(OperationCancelledExceptionFilter))]
[TypeFilter(typeof(NotSupportedExceptionFilter))]
[TypeFilter(typeof(ServiceUnavailableException))]
[TypeFilter(typeof(HttpResultFilter))]
[TypeFilter(typeof(ServiceUnavailableExceptionFilter))]
[TypeFilter(typeof(ErrorResultFilter))]
[TypeFilter(typeof(AbortedRpcExceptionFilter))]
public abstract class ServalControllerBase : Controller
{
private readonly IAuthorizationService _authService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using Newtonsoft.Json.Linq;

namespace Serval.Shared.Controllers;

public class ServiceUnavailableException : ExceptionFilterAttribute
public class ServiceUnavailableExceptionFilter : ExceptionFilterAttribute
{
private readonly ILogger<ServiceUnavailableException> _logger;
private readonly ILogger<ServiceUnavailableExceptionFilter> _logger;

public ServiceUnavailableException(ILoggerFactory loggerFactory)
public ServiceUnavailableExceptionFilter(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<ServiceUnavailableException>();
_logger = loggerFactory.CreateLogger<ServiceUnavailableExceptionFilter>();
}

public override void OnException(ExceptionContext context)
{
if (
(context.Exception is System.TimeoutException)
|| (context.Exception is Grpc.Core.RpcException rpcEx && rpcEx.StatusCode == StatusCode.Unavailable)
(context.Exception is TimeoutException)
|| (context.Exception is RpcException rpcEx && rpcEx.StatusCode == StatusCode.Unavailable)
)
{
_logger.Log(
Expand Down
Loading

0 comments on commit 0e2202c

Please sign in to comment.