Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add function to retrieve API keys for a specific user #521

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Add `RetrieveApiKeysForUser` to `ApiKey` service

## v5.7.0 (2023-09-05)

- Fix FedEx SmartPost carrier account creation not going to the correct endpoint
Expand Down
30 changes: 30 additions & 0 deletions EasyPost.Tests/ServicesTests/ApiKeyServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost.Exceptions.General;
using EasyPost.Models.API;
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
Expand Down Expand Up @@ -39,6 +42,33 @@ public async Task TestAll()
}
}

[Fact]
[CrudOperations.Read]
public async Task TestRetrieveApiKeys()
{
UseVCR("retrieve_api_keys");

User user = await Client.User.RetrieveMe();

List<ApiKey> apiKeys = await Client.ApiKey.RetrieveApiKeysForUser(user.Id);

Assert.IsType<List<ApiKey>>(apiKeys);
}

[Fact]
[CrudOperations.Read]
public async Task TestRetrieveApiKeysChild()
{
UseVCR("retrieve_api_keys_child");

const string fakeChildId = "user_123456789";

// Test suite user has no child users, so this should throw a FilteringError
Exception? possibleException = await Record.ExceptionAsync(async () => await Client.ApiKey.RetrieveApiKeysForUser(fakeChildId));

Assert.IsType<FilteringError>(possibleException);
}

#endregion

#endregion
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions EasyPost/Services/ApiKeyService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Exceptions.General;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal.Attributes;
Expand Down Expand Up @@ -33,6 +37,35 @@ internal ApiKeyService(EasyPostClient client)
[CrudOperations.Read]
public async Task<ApiKeyCollection> All(CancellationToken cancellationToken = default) => await RequestAsync<ApiKeyCollection>(Method.Get, "api_keys", cancellationToken);

/// <summary>
/// Retrieve the <see cref="ApiKey"/>s for a specific <see cref="User"/>.
/// </summary>
/// <param name="id">The ID of the <see cref="User"/> to retrieve keys for.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for the HTTP request.</param>
/// <returns>A list of <see cref="ApiKey"/>s for the specified <see cref="User"/>.</returns>
/// <exception cref="FilteringError">Thrown if the specified <see cref="User"/> does not exist.</exception>
public async Task<List<ApiKey>?> RetrieveApiKeysForUser(string id, CancellationToken cancellationToken = default)
{
ApiKeyCollection apiKeyCollection = await All(cancellationToken);

if (apiKeyCollection.Id == id)
{
return apiKeyCollection.Keys;
}

if (apiKeyCollection.Children == null)
{
throw new FilteringError(string.Format(CultureInfo.InvariantCulture, Constants.ErrorMessages.NoObjectFound, "child"));
}
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved

foreach (ApiKeyCollection child in apiKeyCollection.Children.Where(child => child.Id == id))
{
return child.Keys;
}

throw new FilteringError(string.Format(CultureInfo.InvariantCulture, Constants.ErrorMessages.NoObjectFound, "child"));
}

#endregion
}
}
Loading