Code Monkey home page Code Monkey logo

ynab-sdk-python's Introduction

ynab-sdk-python

PyPI version Maintainability codecov Code style: black

Python implementation of the YNAB API (https://api.youneedabudget.com/)

Installation

Use the package manager pip to install ynab-sdk-python

pip install ynab-sdk

Usage

Example of use with the default client:

from ynab_sdk import YNAB

ynab = YNAB('some-key')

print(ynab.budgets.get_budgets())

Example of use with the cached client:

from ynab_sdk import YNAB
from ynab_sdk.utils.clients.cached_client import CachedClient
from ynab_sdk.utils.configurations.cached import CachedConfig

ynab_config = CachedConfig(
    redis_host='redis-host',
    redis_port='redis-port',
    redis_db='redis-db',
    redis_pass='redis-password',
    api_key='some-key',
)
ynab_client = CachedClient(ynab_config)
ynab = YNAB(client=ynab_client)

# clear the cache
ynab_client.clear_cache()

# set the cached data expiration time in seconds
# if set to 0, negative or None, the cached data never expires
# default value is 3600 seconds (1 hour)
ynab_config.redis_ttl = 120

print(ynab.budgets.get_budgets())

Endpoints

See below whats implemented (Not fully updated yet)

Endpoint Verb Description Working Obs
/user GET Returns authenticated user information NO
/budgets GET Returns budgets list with summary information YES
/budgets/{budget_id} GET Returns a single budget with all related entities. This resource is effectively a full budget export. YES
/budgets/{budget_id}/settings GET Returns settings for a budget YES
/budgets/{budget_id}/accounts GET Returns all accounts YES
/budgets/{budget_id}/accounts/{account_id} GET Returns a single account YES
/budgets/{budget_id}/accounts POST Creates a new account YES
/budgets/{budget_id}/categories GET Returns all categories grouped by category group. Amounts (budgeted, activity, balance, etc.) are specific to the current budget month (UTC).
/budgets/{budget_id}/categories/{category_id} GET Returns a single category. Amounts (budgeted, activity, balance, etc.) are specific to the current budget month (UTC).
/budgets/{budget_id}/months/{month}/categories/{category_id} GET Returns a single category for a specific budget month. Amounts (budgeted, activity, balance, etc.) are specific to the current budget month (UTC).
/budgets/{budget_id}/months/{month}/categories/{category_id} PATCH Update a category for a specific month
/budgets/{budget_id}/payees GET Returns all payees YES
/budgets/{budget_id}/payees/{payee_id} GET Returns single payee YES
/budgets/{budget_id}/payee_locations GET Returns all payee locations NO
/budgets/{budget_id}/payee_locations/{payee_location_id} GET Returns a single payee location NO
/budgets/{budget_id}/payees/{payee_id}/payee_locations GET Returns all payee locations for the specified payee NO
/budgets/{budget_id}/months GET Returns all budget months NO
/budgets/{budget_id}/months/{month} GET Returns a single budget month NO
/budgets/{budget_id}/transactions GET Returns budget transactions YES
/budgets/{budget_id}/transactions POST Creates a single transaction or multiple transactions. If you provide a body containing a 'transaction' object, a single transaction will be created and if you provide a body containing a 'transactions' array, multiple transactions will be created. YES
/budgets/{budget_id}/transactions PATCH Updates multiple transactions, by 'id' or 'import_id'. NO
/budgets/{budget_id}/transactions/{transaction_id} GET Returns a single transaction YES
/budgets/{budget_id}/transactions/{transaction_id} PUT Updates a transaction YES
/budgets/{budget_id}/transactions/bulk POST Creates multiple transactions. Although this endpoint is still supported, it is recommended to use 'POST /budgets/{budget_id}/transactions' to create multiple transactions. NO
/budgets/{budget_id}/accounts/{account_id}/transactions GET Returns all transactions for a specified account YES
/budgets/{budget_id}/categories/{category_id}/transactions GET Returns all transactions for a specified category NO
/budgets/{budget_id}/payees/{payee_id}/transactions GET Returns all transactions for a specified payee NO
/budgets/{budget_id}/scheduled_transactions GET Returns all scheduled transactions NO
/budgets/{budget_id}/scheduled_transactions/{scheduled_transaction_id} GET Returns a single scheduled transaction NO

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

Apache License 2.0

ynab-sdk-python's People

Contributors

andreroggeri avatar birdwing avatar dependabot-preview[bot] avatar dependabot[bot] avatar mschfh avatar pyup-bot avatar quinnhosler avatar tommaso-n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ynab-sdk-python's Issues

Running Balance

It looks like Running Balance (available in the UI) isn't something that's in this API, likely because it seems not to be in the API spec.

¿Is that right?

Presuming so, then would the "right" way to do it be to grab the current account balance and then self-calculate it by going backwards in time using the transaction dates.

get_budget fails with some split transactions in the budget

Hi!
I have some issue with the sdk and my budget.

Calling this code will raise an error:

from ynab_sdk import YNAB

try:
    api_key = "12312123.....123123"
    apicheck = YNAB(api_key)
    data = apicheck.budgets.get_budget("12345678-f349-410e-8ae8-d35672845374")
    print(repr(data.data.budget))
except Exception:
    raise

The response:

Traceback (most recent call last):
  File "/Users/andreas/Downloads/apitest.py", line 21, in <module>
    data = apicheck.budgets.get_budget("12345678-f349-410e-8ae8-d35672845374")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/api/budgets.py", line 17, in get_budget
    return BudgetDetailResponse.from_dict(response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/api/models/responses/budget_detail.py", line 505, in from_dict
    data = Data.from_dict(obj.get("data"))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/api/models/responses/budget_detail.py", line 493, in from_dict
    budget = Budget.from_dict(obj.get("budget"))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/api/models/responses/budget_detail.py", line 455, in from_dict
    subtransactions = parsers.from_list(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/utils/parsers.py", line 47, in from_list
    return [f(y) for y in x]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/utils/parsers.py", line 47, in <listcomp>
    return [f(y) for y in x]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/api/models/responses/budget_detail.py", line 276, in from_dict
    category_id = parsers.from_str(obj.get("category_id"))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/utils/parsers.py", line 16, in from_str
    raise ex
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ynab_sdk/utils/parsers.py", line 11, in from_str
    assert isinstance(x, str)

I have a certain type of split transaction (mix of category and account) that is causing this issue (image attached):

Transaction: Payee TEST / Outflow: 100€
Split 1: Category RENT / Outflow: 50€
Split 2: Account CREDITCARD / Outflow: 50€

When I create this kind of transaction in a test budget, that is working otherwise, it fails. As soon as I delete it, the call is working fine.

Could you make your api handle this case? I am no programmer, so I cannot figure it myself and provide you with a pull request.

Thank you!
Andreas
Screenshot_2021-03-05 bla test YNAB

Esse projeto continua ativamente sendo mantido? 👀

Oi, tô considerando sair da minha implementação própria para me comunicar com a API do YNAB, mas para tomar essa decisão gostaria de saber se este projeto ainda está sendo mantido. @andreroggeri pode me responder essa? Se não eu poderia ajudar na manutenção também.

Scheduled Transactions

I really appreciate all the work that went into this. Thanks so much!

I think that Scheduled Transactions (future dates) aren't yet available based on what I read and what I can try to see digging into the code.

¿Is that right?

Looks like they're different from Transactions, though clearly similar.

get_transactions_from_account function does not accept the available optional filter params since_date or type

Your get_transactions_from_account implementation doesnt take the params since_date or type, which are really helpful for filtering and speeding up things when you only need transactions from the last couple of weeks. See the official docs here: https://api.ynab.com/v1#/Transactions/getTransactions.

I do not have the time to do a proper PR so I simply extended the class in my project like this

from ynab_sdk.api.transactions import TransactionsApi
from ynab_sdk.api.transactions import TransactionsApi
from ynab_sdk.api.transactions import TransactionsResponse

class ExtendedTransactionsApi(TransactionsApi):
    def get_transactions_from_account(self, budget_id, since_date=None, type=None):
        params = {}
        if since_date:
            params['since_date'] = since_date
        if type:
            params['type'] = type

        return self.service.get(f'/budgets/{budget_id}/transactions', params)

def construct_url_with_params(base_url, params):
    if params:
        param_str = "&".join(f"{k}={v}" for k, v in params.items())
        return f"{base_url}?{param_str}"
    else:
        return base_url
    
class ExtendedTransactionsApi(TransactionsApi):
    def get_transactions_from_account(self, budget_id, account_id, since_date=None, type=None):
        params = {}
        if since_date:
            params['since_date'] = since_date
        if type:
            params['type'] = type

        url = construct_url_with_params(f'/budgets/{budget_id}/accounts/{account_id}/transactions', params)
        response = self.client.get(url)
        return TransactionsResponse.from_dict(response)
    
class ExtendedYNAB(YNAB):
    def __init__(self, token):
        super().__init__(token)
        self.extended_transactions = ExtendedTransactionsApi(self.transactions.client)

#ynab = YNAB(YNAB_API_KEY)
ynab = ExtendedYNAB(YNAB_API_KEY)

Now I can do this:

transactions = ynab.extended_transactions.get_transactions_from_account(budget_id=budget, account_id=account_dict['Savings'], since_date="2024-02-26", type="uncategorized")

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Mismapped Parameters When Pulling Categories from Budget Detail

If a category is access directly it works fine, however when pulling the categories from the Budget Detail endpoint the category.goal_type is mapped to the 'Note' endpoint.

In file
Iynab_sdk/api/models/responses/budget_detail.py

Lines: 87 - 89

       goal_type = parsers.from_union(
            [parsers.from_str, parsers.from_none], obj.get("note")
        )

is there documentation?

where the module documentation? All I see is one "get_budgets()" example in the readme.md. Thanks.

Add CI

Stages:

  • Static Analysis (flake8, codeclimate)
  • Test
  • Coverage
  • Deploy to Pypi (Tags Only)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.