Skip to content

Commit

Permalink
Fix #14 and add MisskeyService Dispose().
Browse files Browse the repository at this point in the history
  • Loading branch information
Asteriskx committed Jul 9, 2023
1 parent 5f4f1e4 commit cd2aa55
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
7 changes: 6 additions & 1 deletion Sagiri/Services/Misskey/Interfaces/IMisskeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Sagiri.Services.Misskey.Interfaces
public interface IMisskeyService
{
/// <summary>
///
/// 初期化
/// </summary>
/// <returns></returns>
ValueTask<bool> InitializeAsync();
Expand All @@ -27,5 +27,10 @@ public interface IMisskeyService
/// <param name="ps">パラメーター</param>
/// <returns>レスポンス</returns>
ValueTask<dynamic> RequestWithBinaryAsync(string endpoint, MultipartFormDataContent ps);

/// <summary>
/// 後始末
/// </summary>
void Dispose();
}
}
27 changes: 20 additions & 7 deletions Sagiri/Services/Misskey/MisskeyService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -10,8 +9,6 @@
using Sagiri.Util.Common;
using Sagiri.Util.Configuration;

using static Sagiri.Util.Common.Constants;

namespace Sagiri.Services.Misskey
{
/// <summary>
Expand Down Expand Up @@ -43,6 +40,10 @@ public MisskeyService(string host, string accessToken)

#region Public Methods

/// <summary>
/// 初期化
/// </summary>
/// <returns></returns>
async ValueTask<bool> IMisskeyService.InitializeAsync()
{
try
Expand Down Expand Up @@ -75,7 +76,8 @@ async ValueTask<bool> IMisskeyService.InitializeAsync()
async ValueTask<dynamic> IMisskeyService.RequestAsync(string endpoint, Dictionary<string, object?> ps)
{
ps.Add("i", _AccessToken);
return await _RequestAsync(_Host, endpoint, ps);
var host = new Uri(_Host).ToString();
return await _RequestAsync(host, endpoint, ps);
}

/// <summary>
Expand All @@ -87,7 +89,18 @@ async ValueTask<dynamic> IMisskeyService.RequestAsync(string endpoint, Dictionar
async ValueTask<dynamic> IMisskeyService.RequestWithBinaryAsync(string endpoint, MultipartFormDataContent ps)
{
ps.Add(new StringContent(_AccessToken), "i");
return await _RequestWithBinaryAsync(_Host, endpoint, ps);
var host = new Uri(_Host).ToString();
return await _RequestWithBinaryAsync(host, endpoint, ps);
}

/// <summary>
/// 後始末
/// </summary>
void IMisskeyService.Dispose()
{
_Client.Value?.Dispose();
_Logger = null;
_MisskeyCredentialConfig = null;
}

#endregion Public Methods
Expand All @@ -105,7 +118,7 @@ private async ValueTask<dynamic> _RequestAsync(string? host, string endpoint, Di
{
var client = _Client.Value;

var ep = $"{host}/api/{endpoint}";
var ep = $"{host}api/{endpoint}";

var content = new StringContent(
JsonConvert.SerializeObject(ps),
Expand All @@ -132,7 +145,7 @@ private async ValueTask<dynamic> _RequestWithBinaryAsync(string? host, string en
{
var client = _Client.Value;

var ep = $"{host}/api/{endpoint}";
var ep = $"{host}api/{endpoint}";

var res = await client.PostAsync(ep, ps);

Expand Down
17 changes: 7 additions & 10 deletions SagiriUI/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using Sagiri.Services.Misskey;
Expand All @@ -12,14 +15,10 @@
using Sagiri.Services.Spotify.Interfaces;
using Sagiri.Services.Spotify.Track;
using Sagiri.Util.Common;
using Sagiri.Util.Configuration;
using SagiriUI.Controls;
using SagiriUI.Properties;

using SagiriUI.Controls;
using Microsoft.Toolkit.Uwp.Notifications;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace SagiriUI
{
Expand All @@ -31,8 +30,6 @@ public partial class Form1 : Form
private IMisskeyService? _IMisskeyService { get; set; }
private CurrentTrackInfo _CurrentTrackInfo { get; set; }

private SpotifyCredentialConfig _SpotifyCredentialConfig { get; set; }

private Logger _Logger { get; set; }
private Point _MousePoint { get; set; }

Expand Down Expand Up @@ -61,9 +58,8 @@ private async void Form1_Load(object sender, EventArgs e)
if (!File.Exists("spotify.json") || !File.Exists("misskey.json"))
_CheckClosingApp(errorType: "file", title: "File not found!", message: "");

_CurrentTrackInfo = new();
_ISpotifyService = new SpotifyService();
_SpotifyCredentialConfig = SpotifyCredentialConfig.Instance;
_CurrentTrackInfo = new();

var spCanInitialized = await _ISpotifyService.InitializeAsync();
if (!spCanInitialized)
Expand Down Expand Up @@ -94,6 +90,7 @@ private async void Form1_Load(object sender, EventArgs e)
private void Form1_Closing(object sender, EventArgs e)
{
_ISpotifyService?.Dispose();
_IMisskeyService?.Dispose();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -237,7 +234,7 @@ private async void MisskeyPostPanel_Click(object sender, EventArgs e)
ps.Add("mediaIds", new string[] { file.id });
_ = await _IMisskeyService.RequestAsync("notes/create", ps);

new MessageBoxEx(note.ToString(), "投稿完了!", 3000).Show();
new MessageBoxEx(note.ToString(), "投稿完了!", 2000).Show();

#region Logging

Expand Down

0 comments on commit cd2aa55

Please sign in to comment.