-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor TwitterClient; Make Tweet Delay configurable; Change Tweet template on Dev * Fix integration tests. * Use FluentAssertions and use AutoFixture for data generation * Create repository instance only once and not in each test. * Use AutoFixture for all test data generation. * Create a separate AppConfig file for the integration tests.
- Loading branch information
Showing
24 changed files
with
524 additions
and
707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"Twitter": { | ||
"TweetTemplate": "{name}: \"{meaning}\" {link}", | ||
"TweetIntervalSeconds": 0.1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...tilities/JsonSerializerOptionsProvider.cs → ...nverters/JsonSerializerOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Infrastructure.Twitter | ||
{ | ||
public interface ITwitterClientV2 | ||
{ | ||
Task<TweetV2PostResponse> PostTweet(string text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Newtonsoft.Json; | ||
namespace Infrastructure.Twitter | ||
{ | ||
public record TweetV2PostData | ||
{ | ||
[JsonProperty("id")] | ||
public string Id { get; init; } | ||
[JsonProperty("text")] | ||
public string Text { get; init; } | ||
|
||
public TweetV2PostData(string id, string text) | ||
{ | ||
Id = id; | ||
Text = text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Newtonsoft.Json; | ||
namespace Infrastructure.Twitter | ||
{ | ||
public record TweetV2PostResponse | ||
{ | ||
[JsonProperty("data")] | ||
public TweetV2PostData Data { get; init; } | ||
|
||
public string? Id => Data?.Id; | ||
public string? Text => Data?.Text; | ||
|
||
public TweetV2PostResponse(TweetV2PostData data) | ||
{ | ||
Data = data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Text; | ||
using Tweetinvi; | ||
using Newtonsoft.Json; | ||
namespace Infrastructure.Twitter | ||
{ | ||
public class TwitterClientV2 : ITwitterClientV2 | ||
{ | ||
private readonly ITwitterClient _twitterV1Client; | ||
|
||
public TwitterClientV2(ITwitterClient twitterClient) | ||
{ | ||
_twitterV1Client = twitterClient; | ||
} | ||
|
||
public async Task<TweetV2PostResponse> PostTweet(string text) | ||
{ | ||
var result = await _twitterV1Client.Execute.AdvanceRequestAsync( | ||
(request) => | ||
{ | ||
var jsonBody = _twitterV1Client.Json.Serialize(new TweetV2PostRequest | ||
{ | ||
Text = text | ||
}); | ||
|
||
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); | ||
|
||
request.Query.Url = "https://api.twitter.com/2/tweets"; | ||
request.Query.HttpMethod = Tweetinvi.Models.HttpMethod.POST; | ||
request.Query.HttpContent = content; | ||
} | ||
); | ||
|
||
if (!result.Response.IsSuccessStatusCode) | ||
{ | ||
throw new Exception($"Error when posting tweet:{Environment.NewLine}{result.Content}"); | ||
} | ||
|
||
return _twitterV1Client.Json.Deserialize<TweetV2PostResponse>(result.Content); | ||
} | ||
|
||
/// <summary> | ||
/// There are a lot more fields according to: | ||
/// https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets | ||
/// but these are the ones we care about for our use case. | ||
/// </summary> | ||
private class TweetV2PostRequest | ||
{ | ||
[JsonProperty("text")] | ||
public string Text { get; set; } = string.Empty; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.