Skip to content

Commit

Permalink
Merge pull request #41 from JasonElkin/perf/async
Browse files Browse the repository at this point in the history
Make Mastodon API calls async.
  • Loading branch information
OwainWilliams authored Oct 17, 2023
2 parents b0aeaec + b34c268 commit 372008e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 2 additions & 6 deletions Core/Services/IMastodonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace h5yr.Core.Services
{

public interface IMastodonService
{

IReadOnlyList<MastodonStatus> GetStatuses(int limit, string? maxId = null);

Task<IReadOnlyList<MastodonStatus>> GetStatuses(int limit, string? maxId = null);
}

}
}
6 changes: 3 additions & 3 deletions Core/Services/MastodonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MastodonService(ILogger<MastodonService> logger)
_logger = logger;
}

public IReadOnlyList<MastodonStatus> GetStatuses(int limit, string? maxId = null)
public async Task<IReadOnlyList<MastodonStatus>> GetStatuses(int limit, string? maxId = null)
{

// Initialize a new HTTP service (basically the API wrapper)
Expand All @@ -33,9 +33,9 @@ public IReadOnlyList<MastodonStatus> GetStatuses(int limit, string? maxId = null
{

// Make the request to the API
MastodonStatusListResponse response = mastodon
MastodonStatusListResponse response = await mastodon
.Timelines
.GetHashtagTimeline(options);
.GetHashtagTimelineAsync(options);

// Return the statuses
return response.Body;
Expand Down
8 changes: 4 additions & 4 deletions ViewComponents/MastodonViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public MastodonViewComponent(ILogger<MastodonViewComponent> logger, IOptions<API
_mastodonService = mastodonService;
}

public IViewComponentResult Invoke() {
public async Task<IViewComponentResult> InvokeAsync() {

// Get the statuses
IReadOnlyList<MastodonStatus> statuses = GetStatuses();
IReadOnlyList<MastodonStatus> statuses = await GetStatuses();

// Initialize a new model for the view component
MastodonModel model = new(statuses);
Expand All @@ -36,7 +36,7 @@ public IViewComponentResult Invoke() {

}

private IReadOnlyList<MastodonStatus> GetStatuses() {
private async Task<IReadOnlyList<MastodonStatus>> GetStatuses() {

string fileName = "TestStatuses.json";

Expand All @@ -49,7 +49,7 @@ private IReadOnlyList<MastodonStatus> GetStatuses() {
}
}

IReadOnlyList<MastodonStatus> statuses = _mastodonService.GetStatuses(12);
IReadOnlyList<MastodonStatus> statuses = await _mastodonService.GetStatuses(12);

if (CreateOfflineFile) {
try {
Expand Down

0 comments on commit 372008e

Please sign in to comment.