Skip to content

Commit

Permalink
Merge pull request #662 from cultpodcasts/feature/improve-publish-api
Browse files Browse the repository at this point in the history
Add more detail to api-response
  • Loading branch information
cultpodcasts authored Jan 10, 2025
2 parents 2f54e69 + 317a3b4 commit 389339d
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Class-Libraries/RedditPodcastPoster.Twitter/ITweetPoster.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using RedditPodcastPoster.Models;
using RedditPodcastPoster.Twitter.Models;

namespace RedditPodcastPoster.Twitter;

public interface ITweetPoster
{
Task<TweetSendStatus> PostTweet(PodcastEpisode podcastEpisode, Uri? shortUrl);
Task<PostTweetResponse> PostTweet(PodcastEpisode podcastEpisode, Uri? shortUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace RedditPodcastPoster.Twitter;

public interface ITwitterClient
{
Task<TweetSendStatus> Send(string tweet);
Task<PostTweetResponse> Send(string tweet);
Task<GetTweetsResponseWrapper> GetTweets();
Task<bool> DeleteTweet(Tweet tweet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace RedditPodcastPoster.Twitter.Models;

public record PostTweetResponse(TweetSendStatus TweetSendStatus, string? candidateTweet=null);
7 changes: 4 additions & 3 deletions Class-Libraries/RedditPodcastPoster.Twitter/TweetPoster.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using RedditPodcastPoster.Models;
using RedditPodcastPoster.Persistence.Abstractions;
using RedditPodcastPoster.Twitter.Models;

namespace RedditPodcastPoster.Twitter;

Expand All @@ -11,10 +12,10 @@ public class TweetPoster(
ILogger<TweetPoster> logger)
: ITweetPoster
{
public async Task<TweetSendStatus> PostTweet(PodcastEpisode podcastEpisode, Uri? shortUrl)
public async Task<PostTweetResponse> PostTweet(PodcastEpisode podcastEpisode, Uri? shortUrl)
{
var tweet = await tweetBuilder.BuildTweet(podcastEpisode, shortUrl);
TweetSendStatus tweetStatus;
PostTweetResponse tweetStatus;
try
{
tweetStatus = await twitterClient.Send(tweet);
Expand All @@ -26,7 +27,7 @@ public async Task<TweetSendStatus> PostTweet(PodcastEpisode podcastEpisode, Uri?
throw;
}

if (tweetStatus is TweetSendStatus.Sent or TweetSendStatus.DuplicateForbidden)
if (tweetStatus.TweetSendStatus is TweetSendStatus.Sent or TweetSendStatus.DuplicateForbidden)
{
podcastEpisode.Episode.Tweeted = true;
try
Expand Down
4 changes: 2 additions & 2 deletions Class-Libraries/RedditPodcastPoster.Twitter/Tweeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public async Task Tweet(
}

var tweetStatus = await tweetPoster.PostTweet(podcastEpisode, shortnerResult.Url);
tweeted = tweetStatus == TweetSendStatus.Sent;
tooManyRequests = tweetStatus == TweetSendStatus.TooManyRequests;
tweeted = tweetStatus.TweetSendStatus == TweetSendStatus.Sent;
tooManyRequests = tweetStatus.TweetSendStatus == TweetSendStatus.TooManyRequests;
}
catch (Exception ex)
{
Expand Down
12 changes: 6 additions & 6 deletions Class-Libraries/RedditPodcastPoster.Twitter/TwitterClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ILogger<TwitterClient> logger
{
private readonly TwitterOptions _options = options.Value;

public async Task<TweetSendStatus> Send(string tweet)
public async Task<PostTweetResponse> Send(string tweet)
{
var oauth = new OAuthMessageHandler(_options.ConsumerKey, _options.ConsumerSecret, _options.AccessToken,
_options.AccessTokenSecret);
Expand All @@ -36,7 +36,7 @@ public async Task<TweetSendStatus> Send(string tweet)
if (response.IsSuccessStatusCode)
{
logger.LogInformation($"Tweet sent successfully! Tweet: '{tweet}'.");
return TweetSendStatus.Sent;
return new PostTweetResponse(TweetSendStatus.Sent);
}

if (response.StatusCode == HttpStatusCode.Forbidden)
Expand All @@ -47,14 +47,14 @@ public async Task<TweetSendStatus> Send(string tweet)
{
logger.LogError(
$"Failed to send tweet. Duplicate-tweet. Reason-Phrase: '{response.ReasonPhrase}'. Status-code: '{response.StatusCode}'. Body: '{await response.Content.ReadAsStringAsync()}', Tweet: '{tweet}'.");
return TweetSendStatus.DuplicateForbidden;
return new PostTweetResponse(TweetSendStatus.DuplicateForbidden, tweetData.text);
}

if (responseBody.Contains("too many requests"))
{
logger.LogError(
$"Failed to send tweet. Too-many-requests. Reason-Phrase: '{response.ReasonPhrase}'. Status-code: '{response.StatusCode}'. Body: '{await response.Content.ReadAsStringAsync()}', Tweet: '{tweet}'.");
return TweetSendStatus.TooManyRequests;
return new PostTweetResponse(TweetSendStatus.TooManyRequests, tweetData.text);
}
}
else if (response.StatusCode == HttpStatusCode.TooManyRequests)
Expand All @@ -73,13 +73,13 @@ public async Task<TweetSendStatus> Send(string tweet)

logger.LogError(
$"Failed to send tweet. Too-many-requests. {resetDetails}Reason-Phrase: '{response.ReasonPhrase}'. Status-code: '{response.StatusCode}'. Headers: {JsonSerializer.Serialize(retryAfterHeaders)} Body: '{await response.Content.ReadAsStringAsync()}', Tweet: '{tweet}'.");
return TweetSendStatus.TooManyRequests;
return new PostTweetResponse(TweetSendStatus.TooManyRequests, tweetData.text);
}

logger.LogError(
$"Failed to send tweet. Reason-Phrase: '{response.ReasonPhrase}'. Status-code: '{response.StatusCode}'. Body: '{await response.Content.ReadAsStringAsync()}', Tweet: '{tweet}'.");

return TweetSendStatus.Failed;
return new PostTweetResponse(TweetSendStatus.Failed, tweetData.text);
}

public async Task<GetTweetsResponseWrapper> GetTweets()
Expand Down
3 changes: 0 additions & 3 deletions Cloud/Api/Dtos/EpisodePostResponse.cs

This file was deleted.

2 changes: 2 additions & 0 deletions Cloud/Api/Dtos/EpisodePublishResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class EpisodePublishResponse
[JsonPropertyName("blueskyPosted")]
public bool? BlueskyPosted { get; set; }

[JsonPropertyName("failedTweetContent")]
public string? FailedTweetContent { get; set; }

public bool Updated()
{
Expand Down
12 changes: 12 additions & 0 deletions Cloud/Api/Dtos/EpisodeUpdateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace Api.Dtos;

public class EpisodeUpdateResponse
{
[JsonPropertyName("tweetDeleted")]
public bool? TweetDeleted { get; set; }

[JsonPropertyName("blueskyPostDeleted")]
public bool? BlueskyPostDeleted { get; set; }
}
20 changes: 11 additions & 9 deletions Cloud/Api/EpisodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ private async Task<HttpResponseData> Publish(HttpRequestData req, EpisodePublish
try
{
var result = await tweetPoster.PostTweet(podcastEpisode, shortnerResult.Url);
if (result != TweetSendStatus.Sent)
if (result.TweetSendStatus != TweetSendStatus.Sent)
{
logger.LogError($"Tweet result: '{result}'.");
response.FailedTweetContent = result.candidateTweet;
}
else
{
Expand Down Expand Up @@ -471,16 +472,17 @@ await imageUpdater.UpdateImages(
}
}

var response = req.CreateResponse(HttpStatusCode.Accepted);
if (changeState.UnTweet || changeState.UnBlueskyPost)
var respModel = new EpisodeUpdateResponse();
if (changeState.UnTweet)
{
var respModel = new EpisodePostResponse(
removeTweetResult == RemoveTweetState.Deleted,
removeBlueskyPostResult == RemovePostState.Deleted
);
response = await response.WithJsonBody(respModel, c);
respModel.TweetDeleted = removeTweetResult == RemoveTweetState.Deleted;

}

if (changeState.UnBlueskyPost)
{
respModel.BlueskyPostDeleted = removeBlueskyPostResult == RemovePostState.Deleted;
}
var response = await req.CreateResponse(HttpStatusCode.Accepted).WithJsonBody(respModel, c);
return response;
}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions Console-Apps/Poster/PostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ private async Task PostBluesky(PodcastEpisode podcastEpisode, Uri? shortUrl)
private async Task TweetEpisode(PodcastEpisode podcastEpisode, Uri? shortUrl)
{
var result = await tweetPoster.PostTweet(podcastEpisode, shortUrl);
if (result != TweetSendStatus.Sent)
if (result.TweetSendStatus != TweetSendStatus.Sent)
{
switch (result)
switch (result.TweetSendStatus)
{
case TweetSendStatus.DuplicateForbidden:
logger.LogError("Forbidden to send duplicate-tweet");
Expand Down
2 changes: 1 addition & 1 deletion Console-Apps/Tweet/TweetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Run(TweetRequest request)

var tweet = await tweetBuilder.BuildTweet(podcastEpisode, shortnerResult.Url);
var tweetStatus = await twitterClient.Send(tweet);
var tweeted = tweetStatus == TweetSendStatus.Sent;
var tweeted = tweetStatus.TweetSendStatus == TweetSendStatus.Sent;

if (tweeted)
{
Expand Down

0 comments on commit 389339d

Please sign in to comment.