Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 31, 2025

This PR adds an optional account_id parameter to the get_balances() method, allowing users to retrieve balance information for a specific account instead of all accounts.

Changes

  • Enhanced get_balances() method: Added optional account_id parameter that filters the API response to return only the specified account
  • Backward compatibility: Existing calls without account_id continue to work exactly as before
  • Type flexibility: Supports both string and integer account IDs with automatic type conversion
  • Error handling: Returns None when the specified account ID is not found

Usage

from luno_python.client import Client

c = Client(api_key_id='key_id', api_key_secret='key_secret')

# Original behavior - returns all accounts
all_balances = c.get_balances()
# Returns: {'balance': [{'account_id': '123', 'asset': 'XBT', ...}, ...]}

# New functionality - returns single account
single_balance = c.get_balances(account_id='12345678910')
# Returns: {'account_id': '12345678910', 'asset': 'XBT', 'balance': '0.00', 'reserved': '0.00', 'unconfirmed': '0.00'}

# Works with existing assets parameter
filtered_balance = c.get_balances(assets=['XBT'], account_id='12345678910')

Implementation Details

The implementation uses client-side filtering of the API response rather than modifying the API call itself. This ensures:

  • No changes to the underlying API interaction
  • Full backward compatibility
  • Consistent behavior with existing error handling
  • Minimal performance impact

Testing

Added comprehensive test coverage including:

  • Backward compatibility verification
  • Valid and invalid account ID handling
  • Type conversion between string and integer IDs
  • Edge cases (empty responses, malformed data)
  • Combined parameter usage

Fixes #25.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Summary by CodeRabbit

  • New Features

    • Enhanced the balance retrieval feature to allow filtering by account ID.
  • Tests

    • Added comprehensive tests to verify balance retrieval with and without account ID filters, including edge cases and error handling.

Co-authored-by: dasco144 <10575019+dasco144@users.noreply.github.com>
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
17.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Copilot AI changed the title [WIP] Adding functionality to get_balances to return for a specified account id only Add account_id parameter to get_balances method for single account filtering Jul 31, 2025
Copilot AI requested a review from dasco144 July 31, 2025 14:09
@dasco144 dasco144 requested a review from echarrod July 31, 2025 14:23
@dasco144 dasco144 marked this pull request as ready for review July 31, 2025 14:46
@coderabbitai
Copy link

coderabbitai bot commented Jul 31, 2025

Walkthrough

The get_balances method in luno_python/client.py has been updated to accept an optional account_id parameter. When provided, the method filters the API response to return only the balance entry matching the specified account_id. If no such entry exists, it returns None. If account_id is not specified, the method behaves as previously, returning the full list of balances. A comprehensive set of tests has been added in tests/test_client.py to cover various scenarios, including valid and invalid account_id values, type handling, and edge cases with malformed or empty API responses.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Update get_balances to support returning a single account's balance by account_id (#25)
Ensure backward compatibility when account_id is not specified (#25)
Return None if the specified account_id does not exist (#25)
Add tests covering valid, invalid, and edge-case account_id scenarios (#25)

Poem

In the garden of code where balances grow,
A rabbit hopped in, account in tow.
Now with an ID, it finds just the one—
No more searching, the job is done!
Tests abound, the logic’s tight,
This patch brings clarity and light.
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch copilot/fix-25

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dasco144
Copy link
Contributor

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jul 31, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
luno_python/client.py (1)

171-173: Update type annotation to reflect actual implementation.

The type annotation specifies str but the implementation handles both string and integer values through str() conversion on line 184. Consider updating to indicate this flexibility.

-        :param account_id: Only return balance for the account with this ID. If provided,
-                          returns a single account object instead of the full response.
-        :type account_id: str
+        :param account_id: Only return balance for the account with this ID. If provided,
+                          returns a single account object instead of the full response.
+                          Accepts both string and integer values.
+        :type account_id: Union[str, int]
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d604d4 and 941a275.

📒 Files selected for processing (2)
  • luno_python/client.py (2 hunks)
  • tests/test_client.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
tests/test_client.py (2)
luno_python/client.py (1)
  • get_balances (159-190)
luno_python/base_client.py (1)
  • set_base_url (45-52)
luno_python/client.py (1)
luno_python/base_client.py (1)
  • do (63-89)
🔇 Additional comments (9)
luno_python/client.py (2)

159-159: LGTM! Method signature enhancement maintains backward compatibility.

The addition of the optional account_id parameter is well-designed and preserves existing functionality.


180-190: Excellent client-side filtering implementation.

The filtering logic is robust:

  • Handles missing 'balance' key gracefully
  • Uses string conversion for type-agnostic comparison
  • Returns None for non-existent account IDs
  • Maintains backward compatibility when account_id is not specified
tests/test_client.py (7)

53-77: Excellent backward compatibility test coverage.

This test properly verifies that existing calls without account_id continue to work as before, returning the full API response.


79-107: Comprehensive testing of the core filtering functionality.

Well-structured test that validates both positive cases with different account IDs. The assertions properly verify the filtered response matches expected single account objects.


109-130: Good coverage of error condition handling.

This test correctly verifies that non-existent account IDs return None, which matches the expected behaviour described in the PR objectives.


132-154: Validates combined parameter usage effectively.

This test ensures that the account_id filtering works correctly when combined with the existing assets parameter, demonstrating proper integration.


156-178: Excellent type conversion testing.

This test validates the robust string conversion logic in the implementation, ensuring compatibility when the API returns integer account IDs but string values are provided as input.


180-200: Thorough edge case coverage for empty responses.

Good validation that empty balance arrays are handled correctly - returning None when filtering by account ID, but preserving the original response structure for backward compatibility.


202-222: Robust malformed response handling.

This test ensures graceful degradation when the API response lacks the expected balance key, maintaining backward compatibility while properly handling the filtering case.

Copy link
Contributor

@echarrod echarrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dasco144 dasco144 merged commit 1155bac into main Aug 13, 2025
4 of 5 checks passed
@dasco144 dasco144 deleted the copilot/fix-25 branch August 13, 2025 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding functionality to get_balances to return for a specified account id only

3 participants