Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Net 6, simplified configuration, security++
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesbarbez committed Mar 23, 2022
1 parent 17d146d commit ad94324
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 121 deletions.
8 changes: 4 additions & 4 deletions BarbezDotEu.Twitter/BarbezDotEu.Twitter.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Authors>Hannes Barbez</Authors>
<Company>Hannes Barbez</Company>
<Title>BarbezDotEu.Twitter</Title>
<Product>BarbezDotEu.Twitter</Product>
<PackageId>BarbezDotEu.Twitter</PackageId>
<Copyright2021 Hannes Barbez</Copyright>
<Version>3.0.1</Version>
<Copyright2022 Hannes Barbez</Copyright>
<Version>4.0.0</Version>
<Description>An unofficial, modern, very much work-in-progress client for Twitter APIs.</Description>
<PackageTags>basic;polite;Twitter;integration;third-party integration;provider;barbez</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -22,6 +22,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BarbezDotEu.MicroBlog" Version="2.0.0" />
<PackageReference Include="BarbezDotEu.Provider" Version="3.0.4" />
<PackageReference Include="BarbezDotEu.Provider" Version="5.0.0" />
</ItemGroup>
</Project>
124 changes: 7 additions & 117 deletions BarbezDotEu.Twitter/TwitterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,10 @@ namespace BarbezDotEu.Twitter
/// </summary>
public class TwitterConfiguration
{
/// <summary>
/// A constant representing this package's default implementation for the fully-qualified URL to use to search for topics in recent tweets. However, omits the actual search query and query fields.
/// </summary>
public const string DEFAULTSEARCHRECENTTWEETSURL = "https://api.twitter.com/2/tweets/search/recent?query=";

/// <summary>
/// A constant representing this package's default implementation for the query fields to query the <see cref="SearchRecentTweetsUrl"/> with.
/// </summary>
public const string DEFAULTSEARCHRECENTTWEETSFIELDS = "&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,reply_settings,source,text,withheld&max_results=";

/// <summary>
/// A constant representing this package's default implementation of the OAuth 2 Token URL for authentication.
/// </summary>
public const string DEFAULTOAUTH2TOKENURL = "https://api.twitter.com/oauth2/token";

/// <summary>
/// A constant representing this package's default assumption of the maximum number of calls allowed per minute.
/// </summary>
public const long DEFAULTMAXCALLSPERMINUTE = 2;

/// <summary>
/// A constant representing this package's default assumption of the maximum number of results to return per request.
/// </summary>
public const long DEFAULTRESULTSPERREQUEST = 15;

/// <summary>
/// Gets the maximum number of calls allowed per minute (see the Twitter developer website for current rates).
/// </summary>
public string MaxCallsPerMinute { get; }
public long MaxCallsPerMinute { get; }

/// <summary>
/// Gets the maximum number of results to return per request.
Expand All @@ -61,114 +36,29 @@ public class TwitterConfiguration
/// <summary>
/// Gets the consumer key for authentication. (Get yours from the Twitter.com developer website)
/// </summary>
public string ConsumerKey { get; }
internal string ConsumerKey { get; }

/// <summary>
/// Gets the consumer secret for authentication. (Get yours from the Twitter.com developer website)
/// </summary>
public string ConsumerSecret { get; }
internal string ConsumerSecret { get; }

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters.
/// </summary>
/// <param name="maxCallsPerMinute">The maximum number of calls allowed per minute (see the Twitter.com developer website for current rates).</param>
/// <param name="resultsPerRequest">The maximum number of results to return per request.</param>
/// <param name="searchRecentTweetsUrl">The fully-qualified URL to use to search for topics in recent tweets. However, omits the actual search query and query fields.</param>
/// <param name="searchRecentTweetsFields">The query fields to query the <see cref="SearchRecentTweetsUrl"/> with.</param>
/// <param name="oAuth2TokenUrl">The OAuth 2 Token URL for authentication.</param>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(string maxCallsPerMinute, long resultsPerRequest, string searchRecentTweetsUrl, string searchRecentTweetsFields, string oAuth2TokenUrl, string consumerKey, string consumerSecret)
public TwitterConfiguration(long maxCallsPerMinute, long resultsPerRequest, string consumerKey, string consumerSecret)
{
this.MaxCallsPerMinute = maxCallsPerMinute;
this.ResultsPerRequest = resultsPerRequest;
this.SearchRecentTweetsUrl = searchRecentTweetsUrl;
this.SearchRecentTweetsFields = searchRecentTweetsFields;
this.OAuth2TokenUrl = oAuth2TokenUrl;
this.ConsumerKey = consumerKey;
this.ConsumerSecret = consumerSecret;
}

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters.
/// </summary>
/// <param name="maxCallsPerMinute">The maximum number of calls allowed per minute (see the Twitter.com developer website for current rates).</param>
/// <param name="resultsPerRequest">The maximum number of results to return per request.</param>
/// <param name="searchRecentTweetsUrl">The fully-qualified URL to use to search for topics in recent tweets. However, omits the actual search query and query fields.</param>
/// <param name="searchRecentTweetsFields">The query fields to query the <see cref="SearchRecentTweetsUrl"/> with.</param>
/// <param name="oAuth2TokenUrl">The OAuth 2 Token URL for authentication.</param>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(string maxCallsPerMinute, string resultsPerRequest, string searchRecentTweetsUrl, string searchRecentTweetsFields, string oAuth2TokenUrl, string consumerKey, string consumerSecret)
{
this.MaxCallsPerMinute = maxCallsPerMinute;
this.ResultsPerRequest = GetResultsPerRequest(resultsPerRequest);
this.SearchRecentTweetsUrl = searchRecentTweetsUrl;
this.SearchRecentTweetsFields = searchRecentTweetsFields;
this.OAuth2TokenUrl = oAuth2TokenUrl;
this.SearchRecentTweetsUrl = "https://api.twitter.com/2/tweets/search/recent?query=";
this.SearchRecentTweetsFields = "&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,reply_settings,source,text,withheld&max_results=";
this.OAuth2TokenUrl = "https://api.twitter.com/oauth2/token";
this.ConsumerKey = consumerKey;
this.ConsumerSecret = consumerSecret;
}

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters.
/// </summary>
/// <param name="maxCallsPerMinute">The maximum number of calls allowed per minute (see the Twitter.com developer website for current rates).</param>
/// <param name="resultsPerRequest">The maximum number of results to return per request.</param>
/// <param name="searchRecentTweetsUrl">The fully-qualified URL to use to search for topics in recent tweets. However, omits the actual search query and query fields.</param>
/// <param name="searchRecentTweetsFields">The query fields to query the <see cref="SearchRecentTweetsUrl"/> with.</param>
/// <param name="oAuth2TokenUrl">The OAuth 2 Token URL for authentication.</param>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(long maxCallsPerMinute, long resultsPerRequest, string searchRecentTweetsUrl, string searchRecentTweetsFields, string oAuth2TokenUrl, string consumerKey, string consumerSecret)
: this(maxCallsPerMinute.ToString(), resultsPerRequest, searchRecentTweetsUrl, searchRecentTweetsFields, oAuth2TokenUrl, consumerKey, consumerSecret)
{
}

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters and using some default settings.
/// </summary>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(string consumerKey, string consumerSecret)
: this(DEFAULTMAXCALLSPERMINUTE.ToString(), DEFAULTRESULTSPERREQUEST, DEFAULTSEARCHRECENTTWEETSURL, DEFAULTSEARCHRECENTTWEETSFIELDS, DEFAULTOAUTH2TOKENURL, consumerKey, consumerSecret)
{
}

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters and using some default settings.
/// </summary>
/// <param name="maxCallsPerMinute">The maximum number of calls allowed per minute (see the Twitter.com developer website for current rates).</param>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(long maxCallsPerMinute, string consumerKey, string consumerSecret)
: this(maxCallsPerMinute.ToString(), DEFAULTRESULTSPERREQUEST, DEFAULTSEARCHRECENTTWEETSURL, DEFAULTSEARCHRECENTTWEETSFIELDS, DEFAULTOAUTH2TOKENURL, consumerKey, consumerSecret)
{
}

/// <summary>
/// Constructs a new <see cref="TwitterConfiguration"/> using given parameters and using some default settings.
/// </summary>
/// <param name="maxCallsPerMinute">The maximum number of calls allowed per minute (see the Twitter.com developer website for current rates).</param>
/// <param name="consumerKey">The consumer key for authentication. (Get yours from the Twitter.com developer website)</param>
/// <param name="consumerSecret">The secret for authentication. (Get yours from the Twitter.com developer website)</param>
public TwitterConfiguration(string maxCallsPerMinute, string consumerKey, string consumerSecret)
: this(maxCallsPerMinute, DEFAULTRESULTSPERREQUEST, DEFAULTSEARCHRECENTTWEETSURL, DEFAULTSEARCHRECENTTWEETSFIELDS, DEFAULTOAUTH2TOKENURL, consumerKey, consumerSecret)
{
}

/// <summary>
/// From a given string, parses the max. number of results to return per request. If not possible to parse, returns <see cref="DEFAULTRESULTSPERREQUEST"/>.
/// </summary>
/// <param name="resultsPerRequest">The string representation of the value of the maximum number of results per request.</param>
/// <returns>The maximum number of results to return per request.</returns>
private static long GetResultsPerRequest(string resultsPerRequest)
{
var success = long.TryParse(resultsPerRequest, out long numberOfResultsPerRequest);
if (success)
return numberOfResultsPerRequest;

return DEFAULTRESULTSPERREQUEST;
}
}
}

0 comments on commit ad94324

Please sign in to comment.