106 lines
4.1 KiB
C#
106 lines
4.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace SpectacleTransformer {
|
|
|
|
public abstract class HttpTranslationRequest : TranslationRequest {
|
|
/// <summary>
|
|
/// The URL to use when performing the request.
|
|
/// </summary>
|
|
public abstract Uri Url { get; }
|
|
|
|
public HttpTranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) : base(InstanceUri, Source, Target, Text) { }
|
|
|
|
public override string Run() {
|
|
HttpWebResponse response = null;
|
|
try {
|
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
|
|
request.UserAgent = String.Format("{0}/{1}", Application.ProductName, Application.ProductVersion);
|
|
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
|
|
string responseBody;
|
|
using (Stream stream = response.GetResponseStream()) {
|
|
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
|
|
responseBody = reader.ReadToEnd();
|
|
}
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) {
|
|
string translation = DecodeResponse(responseBody);
|
|
|
|
if (translation == null) {
|
|
MessageBox.Show(
|
|
String.Format(Properties.Resources.InvalidPayloadError, responseBody),
|
|
Properties.Resources.InvalidPayloadErrorTitle,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error
|
|
);
|
|
}
|
|
|
|
return translation;
|
|
}
|
|
|
|
string errorMessage = DecodeErrorResponse(responseBody);
|
|
string errorTitle;
|
|
|
|
if (String.IsNullOrEmpty(errorMessage)) {
|
|
errorMessage = String.Format(
|
|
Properties.Resources.UnknownTranslationError,
|
|
response.StatusCode,
|
|
response.StatusDescription
|
|
);
|
|
errorTitle = Properties.Resources.UnknownTranslationErrorTitle;
|
|
} else {
|
|
errorMessage = String.Format(
|
|
Properties.Resources.TranslationError,
|
|
response.StatusCode,
|
|
response.StatusDescription,
|
|
errorMessage
|
|
);
|
|
errorTitle = Properties.Resources.TranslationErrorTitle;
|
|
}
|
|
|
|
MessageBox.Show(
|
|
errorMessage,
|
|
errorTitle,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error
|
|
);
|
|
return null;
|
|
} catch (WebException ex) {
|
|
MessageBox.Show(
|
|
String.Format(Properties.Resources.HttpRequestError, ex.ToString()),
|
|
Properties.Resources.HttpRequestErrorTitle,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error
|
|
);
|
|
return null;
|
|
} finally {
|
|
if (response != null) response.Close();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode a successful response body into a translated text.
|
|
/// </summary>
|
|
/// <param name="responseBody">The retrieved response body.</param>
|
|
/// <returns>The translated text, or null if the translation failed.</returns>
|
|
public virtual string DecodeResponse(string responseBody) {
|
|
return responseBody;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode an error response body into an error message.
|
|
/// </summary>
|
|
/// <param name="responseBody">The retrieved response body.</param>
|
|
/// <returns>An error message, or null if there is no particular error message.</returns>
|
|
public virtual string DecodeErrorResponse(string responseBody) {
|
|
return responseBody;
|
|
}
|
|
}
|
|
|
|
}
|