Skip to content

Commit

Permalink
Exposed access token as set-only property (#8)
Browse files Browse the repository at this point in the history
* Exposed access token as set-only property

* Updated docs
  • Loading branch information
itsWindows11 authored Oct 13, 2024
1 parent 373e5dc commit bf8aa9d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
15 changes: 8 additions & 7 deletions docs/api-reference/ThreadSharp/ThreadsClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ Creates an instance of [ThreadsClient](./ThreadsClient) which doesn't automatica

## Properties

| Property | Type | Summary | Default Value |
|-------------------------|-----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|---------------|
| MaxRetriesOnServerError | `int` | The maximum amount of retries to do when a request fails on the Threads API's end. | 5 |
| BackingClient | `HttpClient` | The backing HttpClient for the client. **Use of the `SendRequestAsync(HttpMethod, string)` method is preferred over using this directly.** | -- |
| Me | [`ThreadsUserClient`](./Internal/ThreadsUserClient) | Client for all things user related for the current authenticated user, including posting threads. | -- |
| Threads | [`ThreadsThreadManagementClient`](./Internal/ThreadsThreadManagementClient) | Client for thread fetching & management. | -- |
| Insights | [`ThreadsInsightsClient`](./Internal/ThreadsInsightsClient) | Client for all things insight/data related for the current authenticated user. | -- |
| Property | Type | Summary | Default Value |
|-------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|---------------|
| MaxRetriesOnServerError | `int` | The maximum amount of retries to do when a request fails on the Threads API's end. | 5 |
| AccessToken | `string` | Sets the current user's access token, mostly for emergency purposes or when a new access token is generated. | 5 |
| BackingClient | `HttpClient` | The backing HttpClient for the client.<br>**Use of the `SendRequestAsync(HttpMethod, string)` method is preferred over using this directly.** | -- |
| Me | [`ThreadsUserClient`](./Internal/ThreadsUserClient) | Client for all things user related for the current authenticated user, including posting threads. | -- |
| Threads | [`ThreadsThreadManagementClient`](./Internal/ThreadsThreadManagementClient) | Client for thread fetching & management. | -- |
| Insights | [`ThreadsInsightsClient`](./Internal/ThreadsInsightsClient) | Client for all things insight/data related for the current authenticated user. | -- |

## Methods

Expand Down
12 changes: 6 additions & 6 deletions src/ThreadSharp/Internal/ThreadsInsightsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ namespace ThreadSharp.Internal;
/// </summary>
public sealed class ThreadsInsightsClient
{
private readonly string _accessToken;
private readonly Func<string> _getAccessToken;
private readonly IThreadSharpRefitClient _refitClient;
private readonly Func<int> _getMaxRetriesOnServerError;

internal ThreadsInsightsClient(
string accessToken,
Func<string> getAccessToken,
IThreadSharpRefitClient retrofitClient,
Func<int> getMaxRetriesOnServerError)
{
_accessToken = accessToken;
_getAccessToken = getAccessToken;
_refitClient = retrofitClient;
_getMaxRetriesOnServerError = getMaxRetriesOnServerError;
}
Expand Down Expand Up @@ -75,8 +75,8 @@ public Task<ThreadsResult<List<ThreadsUserInsightDataBase>>> GetForCurrentUserAs
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = breakdownStringList?.Count > 0
? await _refitClient.GetUserInsightsAsync(_accessToken, metricPagingParameters, string.Join(",", breakdownStringList), cancellationToken: cancellationToken)
: await _refitClient.GetUserInsightsAsync(_accessToken, metricPagingParameters, cancellationToken: cancellationToken);
? await _refitClient.GetUserInsightsAsync(_getAccessToken(), metricPagingParameters, string.Join(",", breakdownStringList), cancellationToken: cancellationToken)
: await _refitClient.GetUserInsightsAsync(_getAccessToken(), metricPagingParameters, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsUserInsightDataBase>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -126,7 +126,7 @@ public Task<ThreadsResult<List<ThreadsMediaInsightItem>>> GetForPostAsync(string
{
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.GetMediaInsightsAsync(_accessToken, string.Join(",", metrics), mediaContainerId, cancellationToken);
using var response = await _refitClient.GetMediaInsightsAsync(_getAccessToken(), string.Join(",", metrics), mediaContainerId, cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsMediaInsightItem>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down
22 changes: 11 additions & 11 deletions src/ThreadSharp/Internal/ThreadsThreadManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace ThreadSharp.Internal;
/// </summary>
public sealed class ThreadsThreadManagementClient
{
private readonly string _accessToken;
private readonly Func<string> _getAccessToken;
private readonly IThreadSharpRefitClient _refitClient;
private readonly Func<int> _getMaxRetriesOnServerError;

internal ThreadsThreadManagementClient(
string accessToken,
Func<string> getAccessToken,
IThreadSharpRefitClient retrofitClient,
Func<int> getMaxRetriesOnServerError)
{
_accessToken = accessToken;
_getAccessToken = getAccessToken;
_refitClient = retrofitClient;
_getMaxRetriesOnServerError = getMaxRetriesOnServerError;
}
Expand All @@ -40,7 +40,7 @@ public Task<ThreadsResult<ThreadsMediaContainerStatus>> GetContainerStatusAsync(
{
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.GetThreadAsync(_accessToken, mediaContainerId, "id,status,error_message", cancellationToken);
using var response = await _refitClient.GetThreadAsync(_getAccessToken(), mediaContainerId, "id,status,error_message", cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsMediaContainerStatus>(new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -94,8 +94,8 @@ public Task<ThreadsResult<ThreadsPost>> GetThreadAsync(string threadId, string[]
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = fields is not null
? await _refitClient.GetThreadAsync(_accessToken, threadId, string.Join(",", fields), cancellationToken)
: await _refitClient.GetThreadAsync(_accessToken, threadId, cancellationToken: cancellationToken);
? await _refitClient.GetThreadAsync(_getAccessToken(), threadId, string.Join(",", fields), cancellationToken)
: await _refitClient.GetThreadAsync(_getAccessToken(), threadId, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsPost>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -153,8 +153,8 @@ public Task<ThreadsResult<List<ThreadsPost>>> GetRepliesAsync(string threadId, P
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = fields is not null
? await _refitClient.GetRepliesAsync(_accessToken, threadId, reverse, pagingParameters, string.Join(",", fields), cancellationToken)
: await _refitClient.GetRepliesAsync(_accessToken, threadId, reverse, pagingParameters, cancellationToken: cancellationToken);
? await _refitClient.GetRepliesAsync(_getAccessToken(), threadId, reverse, pagingParameters, string.Join(",", fields), cancellationToken)
: await _refitClient.GetRepliesAsync(_getAccessToken(), threadId, reverse, pagingParameters, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsPost>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -218,8 +218,8 @@ public Task<ThreadsResult<List<ThreadsPost>>> GetConversationAsync(string thread
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = fields is not null
? await _refitClient.GetConversationAsync(_accessToken, threadId, reverse, pagingParameters, string.Join(",", fields), cancellationToken)
: await _refitClient.GetConversationAsync(_accessToken, threadId, reverse, pagingParameters, cancellationToken: cancellationToken);
? await _refitClient.GetConversationAsync(_getAccessToken(), threadId, reverse, pagingParameters, string.Join(",", fields), cancellationToken)
: await _refitClient.GetConversationAsync(_getAccessToken(), threadId, reverse, pagingParameters, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsPost>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -268,7 +268,7 @@ public Task<ThreadsResult<ThreadsManageReplyResult>> ManageReplyAsync(string thr
{
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.ManageReplyAsync(_accessToken, threadId, hide, cancellationToken);
using var response = await _refitClient.ManageReplyAsync(_getAccessToken(), threadId, hide, cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsManageReplyResult>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down
28 changes: 14 additions & 14 deletions src/ThreadSharp/Internal/ThreadsUserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public sealed class ThreadsUserClient
{
private readonly string? _threadsUserId;

Check warning on line 18 in src/ThreadSharp/Internal/ThreadsUserClient.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThreadsUserClient._threadsUserId' is never assigned to, and will always have its default value null

Check warning on line 18 in src/ThreadSharp/Internal/ThreadsUserClient.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThreadsUserClient._threadsUserId' is never assigned to, and will always have its default value null

Check warning on line 18 in src/ThreadSharp/Internal/ThreadsUserClient.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThreadsUserClient._threadsUserId' is never assigned to, and will always have its default value null

Check warning on line 18 in src/ThreadSharp/Internal/ThreadsUserClient.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThreadsUserClient._threadsUserId' is never assigned to, and will always have its default value null

private readonly string _accessToken;
private readonly Func<string> _getAccessToken;
private readonly IThreadSharpRefitClient _refitClient;
private readonly Func<int> _getMaxRetriesOnServerError;

internal ThreadsUserClient(
string accessToken,
Func<string> getAccessToken,
IThreadSharpRefitClient retrofitClient,
Func<int> getMaxRetriesOnServerError)
{
_accessToken = accessToken;
_getAccessToken = getAccessToken;
_refitClient = retrofitClient;
_getMaxRetriesOnServerError = getMaxRetriesOnServerError;
}
Expand All @@ -51,8 +51,8 @@ public Task<ThreadsResult<ThreadsProfile>> GetAsync(string[]? fields = null, Can
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = _threadsUserId is not null
? (fields is not null ? await _refitClient.GetProfileAsync(_accessToken, _threadsUserId, string.Join(",", fields), cancellationToken: cancellationToken) : await _refitClient.GetProfileAsync(_accessToken, _threadsUserId, cancellationToken: cancellationToken))
: (fields is not null ? await _refitClient.GetProfileAsync(_accessToken, string.Join(",", fields), cancellationToken: cancellationToken) : await _refitClient.GetProfileAsync(_accessToken, cancellationToken: cancellationToken));
? (fields is not null ? await _refitClient.GetProfileAsync(_getAccessToken(), _threadsUserId, string.Join(",", fields), cancellationToken: cancellationToken) : await _refitClient.GetProfileAsync(_getAccessToken(), _threadsUserId, cancellationToken: cancellationToken))
: (fields is not null ? await _refitClient.GetProfileAsync(_getAccessToken(), string.Join(",", fields), cancellationToken: cancellationToken) : await _refitClient.GetProfileAsync(_getAccessToken(), cancellationToken: cancellationToken));

if (response.Content == null)
return new ThreadsResult<ThreadsProfile>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -105,8 +105,8 @@ public Task<ThreadsResult<List<ThreadsPost>>> GetThreadsAsync(PostPagingParamete
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = _threadsUserId is not null
? await _refitClient.GetThreadsAsync(_accessToken, pagingParameters, _threadsUserId, limit: limit, cancellationToken: cancellationToken)
: await _refitClient.GetThreadsAsync(_accessToken, pagingParameters, limit: limit, cancellationToken: cancellationToken);
? await _refitClient.GetThreadsAsync(_getAccessToken(), pagingParameters, _threadsUserId, limit: limit, cancellationToken: cancellationToken)
: await _refitClient.GetThreadsAsync(_getAccessToken(), pagingParameters, limit: limit, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsPost>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -162,8 +162,8 @@ public Task<ThreadsResult<List<ThreadsPost>>> GetRepliesAsync(PostPagingParamete
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = _threadsUserId is not null
? await _refitClient.GetRepliesAsync(_accessToken, pagingParameters, _threadsUserId, limit: limit, cancellationToken: cancellationToken)
: await _refitClient.GetRepliesAsync(_accessToken, pagingParameters, limit: limit, cancellationToken: cancellationToken);
? await _refitClient.GetRepliesAsync(_getAccessToken(), pagingParameters, _threadsUserId, limit: limit, cancellationToken: cancellationToken)
: await _refitClient.GetRepliesAsync(_getAccessToken(), pagingParameters, limit: limit, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<List<ThreadsPost>>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -214,7 +214,7 @@ public Task<ThreadsResult<ThreadsPublishingLimitData>> GetPublishingLimitAsync(C

return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.GetPublishingLimitAsync(_accessToken, cancellationToken: cancellationToken);
using var response = await _refitClient.GetPublishingLimitAsync(_getAccessToken(), cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsPublishingLimitData>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -263,8 +263,8 @@ public Task<ThreadsResult<ThreadsIdContainer>> GetMediaContainerAsync(string med
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = fields is not null
? await _refitClient.GetMediaContainerAsync(_accessToken, mediaContainerId, string.Join(",", fields), cancellationToken: cancellationToken)
: await _refitClient.GetMediaContainerAsync(_accessToken, mediaContainerId, cancellationToken: cancellationToken);
? await _refitClient.GetMediaContainerAsync(_getAccessToken(), mediaContainerId, string.Join(",", fields), cancellationToken: cancellationToken)
: await _refitClient.GetMediaContainerAsync(_getAccessToken(), mediaContainerId, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsIdContainer>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down Expand Up @@ -342,7 +342,7 @@ public Task<ThreadsResult<ThreadsIdContainer>> CreateMediaContainerAsync(
return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.CreateMediaContainerAsync(
_accessToken,
_getAccessToken(),
mediaType,
postContent,
replyControl,
Expand Down Expand Up @@ -399,7 +399,7 @@ public Task<ThreadsResult<ThreadsIdContainer>> PublishMediaContainerAsync(string

return RetryHelpers.RetryOnServerErrorAsync(async () =>
{
using var response = await _refitClient.PublishMediaContainerAsync(_accessToken, mediaContainerId, cancellationToken: cancellationToken);
using var response = await _refitClient.PublishMediaContainerAsync(_getAccessToken(), mediaContainerId, cancellationToken: cancellationToken);

if (response.Content == null)
return new ThreadsResult<ThreadsIdContainer>(error: new ThreadsBlankResponseException(), response.StatusCode);
Expand Down
17 changes: 13 additions & 4 deletions src/ThreadSharp/ThreadsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ThreadSharp;
/// </summary>
public sealed class ThreadsClient : IDisposable
{
private readonly string _accessToken;
private string _accessToken;

private readonly HttpClient _httpClient;
private readonly IThreadSharpRefitClient _refitClient;
Expand All @@ -23,6 +23,15 @@ public sealed class ThreadsClient : IDisposable
/// </summary>
public int MaxRetriesOnServerError { get; set; } = 5;

/// <summary>
/// Sets the current user's access token, mostly for emergency
/// purposes or when a new access token is generated.
/// </summary>
public string AccessToken
{
set => _accessToken = value;
}

/// <summary>
/// The backing <see cref="HttpClient"/> for the client.
/// </summary>
Expand Down Expand Up @@ -80,9 +89,9 @@ public ThreadsClient(string accessToken)
})
});

Me = new(_accessToken, _refitClient, () => MaxRetriesOnServerError);
Threads = new(_accessToken, _refitClient, () => MaxRetriesOnServerError);
Insights = new(_accessToken, _refitClient, () => MaxRetriesOnServerError);
Me = new(() => _accessToken, _refitClient, () => MaxRetriesOnServerError);
Threads = new(() => _accessToken, _refitClient, () => MaxRetriesOnServerError);
Insights = new(() => _accessToken, _refitClient, () => MaxRetriesOnServerError);
}

/*/// <summary>
Expand Down

0 comments on commit bf8aa9d

Please sign in to comment.