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

Accessing Nested Resources #67

Open
lucasob opened this issue Nov 22, 2021 · 2 comments
Open

Accessing Nested Resources #67

lucasob opened this issue Nov 22, 2021 · 2 comments

Comments

@lucasob
Copy link

lucasob commented Nov 22, 2021

Hey!

Not sure what people's opinions are here for implementing access to nested resources. One such example is the Spend Money Attachments: /{cf_uri}/Banking/SpendMoneyTxn/{Spend_Money_UID}/Attachment

The current implementation is fantastic and delightfully extensible for top-level resources, but doesn't make for easy implementation (at least to me) when querying nested resources.

Did anyone have any implementation ideas to get around this issue? I have one but it would involve a very large refactoring which isn't by any means ideal.

This idea would just centre around the below (using python-ish pseudocode)


MYOB_URL = "..."

def post<R>(url: str, body:R ): T
    return requests.post(url, body)
    
def get<T>(url: str) -> T:
    return requests.get(url)    


class SpendMoneyTxn:

    def get_attachments(self) -> SpendMoneyAttachment:
        return get(url=f"{MYOBURL}/Banking/SpendMoneyTxn/{self.id}/Atachment")
    

I am open to a way of trying to include the current managers but I'm not sure how that would look.

Thanks!

@jarekwg
Copy link
Member

jarekwg commented Nov 22, 2021

Hey @lucasob,

So I'd actually made things generic enough with the original implementation to allow for this.
https://github.com/uptick/pymyob/blame/7d932a42264fc625cfa3c0276f6d290f7f0fc7a6/myob/endpoints.py
One would manually set up kwargs, defined in [ ] brackets, and they'd be set up accordingly on the method.
This got kinda lost when I streamlined it later with METHOD_MAPPING to reduce repetition. Off the top of my head, the smallest tweak we need here to allow custom GETs is to allow that manual endpoint definition again. Something like

'Banking/': {
        'name': 'banking',
        'methods': [
            (ALL, '', 'banking type'),
            (CRUD, 'SpendMoneyTxn/', 'spend money transaction'),
            (RAW_GET, 'SpendMoneyTxn/[uid]/Attachment', 'Return attachments for selected selected spend money transaction.')
            (CRUD, 'ReceiveMoneyTxn/', 'receive money transaction'),
            (CRUD, 'TransferMoneyTxn/', 'transfer money transaction'),
        ],
    },

And plug it through so that RAW_GET folds down to GET later. Not very pretty though..
Could also look into nested definitions:

    'Banking/': {
        'name': 'banking',
        'methods': [
            (ALL, '', 'banking type'),
            (CRUD, 'SpendMoneyTxn/', 'spend money transaction', [
                ('Attachment', 'Return attachments for selected selected spend money transaction.'),
            ]),
            (CRUD, 'ReceiveMoneyTxn/', 'receive money transaction'),
            (CRUD, 'TransferMoneyTxn/', 'transfer money transaction'),
        ],
    },

Not a huge fan of this either, as it implies it's nesting off the entire set of CRUD operations, rather than just the GET..

Happy to look at a PR if you want to have a crack at implementing either of these above, or a more involved approach like what you've suggested. As long as the endpoints are easy to view at a glance and extend.

@lucasob
Copy link
Author

lucasob commented Nov 22, 2021

Hey -- Thanks so much for quick response. I'll give this a good whirl and get something back to you asap.
I'll leave this open and attach the PR for reference later too.

Thanks!

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

No branches or pull requests

2 participants