Skip to content

Commit

Permalink
Mono is shit and broke HttpClient so use WebClient
Browse files Browse the repository at this point in the history
  • Loading branch information
oonqt committed May 1, 2020
1 parent a526ed9 commit e0fc13f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 200 deletions.
6 changes: 2 additions & 4 deletions Emby.Notifications.Discord/Api/NotificationsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class TestNotification : IReturnVoid

class NotificationsService : IService
{
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
private readonly IServerConfigurationManager _serverConfiguration;
private readonly IJsonSerializer _jsonSerializer;

public NotificationsService(ILogManager logManager, IJsonSerializer jsonSerializer, IServerConfigurationManager serverConfiguration)
{
_logger = logManager.GetLogger(GetType().Namespace);
_httpClient = new HttpClient();
_serverConfiguration = serverConfiguration;
_jsonSerializer = jsonSerializer;
}
Expand Down Expand Up @@ -63,7 +61,7 @@ public async Task PostAsync(TestNotification request)
{
avatar_url = options.AvatarUrl,
username = options.Username,
embeds = new List<DiscordEmbed>()
embeds = new List<DiscordEmbed>()
{
new DiscordEmbed()
{
Expand Down Expand Up @@ -91,7 +89,7 @@ public async Task PostAsync(TestNotification request)
}

try {
await DiscordWebhookHelper.ExecuteWebhook(discordMessage, options.DiscordWebhookURI, _jsonSerializer, _httpClient);
await DiscordWebhookHelper.ExecuteWebhook(discordMessage, options.DiscordWebhookURI, _jsonSerializer);
}
catch (System.Exception e) {
_logger.ErrorException("Failed to execute webhook", e);
Expand Down
1 change: 0 additions & 1 deletion Emby.Notifications.Discord/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
class Constants
{
public static readonly string[] AllowedMediaTypes = new string[] { "Movie", "Episode", "Audio" };
public static readonly int RecheckIntervalMS = 10000;
public static readonly int MaxRetriesBeforeFallback = 10;
public static readonly int MessageQueueSendInterval = 1000;
Expand Down
34 changes: 19 additions & 15 deletions Emby.Notifications.Discord/DiscordWebhookHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using MediaBrowser.Model.Serialization;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace Emby.Notifications.Discord
{
Expand Down Expand Up @@ -53,27 +55,29 @@ public static int FormatColorCode(string hexCode)
return int.Parse(hexCode.Substring(1, 6), System.Globalization.NumberStyles.HexNumber);
}

public static async Task ExecuteWebhook(DiscordMessage message, string webhookUrl, IJsonSerializer _jsonSerializer, HttpClient _httpClient)
public static async Task ExecuteWebhook(DiscordMessage message, string webhookUrl, IJsonSerializer _jsonSerializer)
{
StringContent postData = new StringContent(_jsonSerializer.SerializeToString(message).ToString());
//StringContent postData = new StringContent(_jsonSerializer.SerializeToString(message).ToString());

try
{
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, webhookUrl);
req.Content = postData;
req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
byte[] bytes = Encoding.UTF8.GetBytes(_jsonSerializer.SerializeToString(message));

HttpResponseMessage response = await _httpClient.SendAsync(req).ConfigureAwait(false);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webhookUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = bytes.Length;
using (Stream requestData = request.GetRequestStream())
{
requestData.Write(bytes, 0, bytes.Count());
}

string content = await response.Content.ReadAsStringAsync();
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

if(response.StatusCode != HttpStatusCode.NoContent) {
throw new System.Exception($"Status: {response.StatusCode} content: {content}");
}
}
catch (HttpRequestException e)
response.Dispose();
} catch (Exception e)
{
throw new System.Exception(e.Message);
throw e;
}
}
}
Expand Down
34 changes: 10 additions & 24 deletions Emby.Notifications.Discord/MemesterServiceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Net.Http.Headers;
using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Model.Serialization;
using System.Net.Http;
using System.IO;
using System;
using System.Text;

namespace Emby.Notifications.Discord
{
Expand All @@ -16,29 +14,17 @@ public class ImageServiceResponse
public class MemesterServiceHelper {
private static string uploadEndpoint => "https://i.memester.xyz/upload?format=json";

public static async Task<string> UploadImage(Stream ImageData, IJsonSerializer jsonSerializer, HttpClient httpClient) {
StreamContent imageStream = new StreamContent(ImageData);
ByteArrayContent imageStreamContent = new ByteArrayContent(await imageStream.ReadAsByteArrayAsync());
MultipartFormDataContent formData = new MultipartFormDataContent();
public static ImageServiceResponse UploadImage(string path, IJsonSerializer jsonSerializer) {
try
{
WebClient client = new WebClient();

imageStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
byte[] response = client.UploadFile(uploadEndpoint, path);

formData.Add(imageStreamContent, "file", "poster.png");

try {
HttpResponseMessage res = await httpClient.PostAsync(uploadEndpoint, formData);

string responseContent = await res.Content.ReadAsStringAsync();

ImageServiceResponse memesterResponse = jsonSerializer.DeserializeFromString<ImageServiceResponse>(responseContent);

if(res.StatusCode == HttpStatusCode.Created) {
return memesterResponse.filePath;
} else {
throw new Exception($"Status: {res.StatusCode} Server Response: {responseContent}");
}
} catch (HttpRequestException e) {
throw new Exception(e.Message);
return jsonSerializer.DeserializeFromString<ImageServiceResponse>(Encoding.Default.GetString(response));
} catch (Exception e)
{
throw e;
}
}
}
Expand Down
Loading

0 comments on commit e0fc13f

Please sign in to comment.