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

Allow list of Hyperlinks #11

Closed
royrusso opened this issue Jan 7, 2015 · 3 comments
Closed

Allow list of Hyperlinks #11

royrusso opened this issue Jan 7, 2015 · 3 comments

Comments

@royrusso
Copy link

royrusso commented Jan 7, 2015

Currently there is no way to create an array/list of hyperlinks, as it expects a dict. A common format for hateoas is as this: https://developer.paypal.com/docs/api/#hateoas-links

"links" : [{
"href" : "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XR800907F429382MKEBWOSA",
"rel" : "self",
"method" : "GET"
}, {
"href" : "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XR800907F429382MKEBWOSA/execute",
"rel" : "update",
"method" : "POST"

This format provides standard keys for every link, and is therefore easier to navigate.

@sloria
Copy link
Member

sloria commented Jan 12, 2015

I suppose we could support arrays of links with a many param to Hyperlinks. Pull requests welcome for this.

@Kareeeeem
Copy link

I figured I'd try and contribute and implement this, but it seems it already works as is. No work needed. I added this test code to start with and it passed.

def test_hyperlinks_field_many(ma, mockauthor):                                    
    field = ma.Hyperlinks([                                                        
        {                                                                          
            'rel': 'self',                                                         
            'method': 'GET',                                                       
            'href': url_for('author', id=mockauthor.id),                           
        },                                                                         
        {                                                                          
            'rel': 'collection',                                                   
            'method': 'GET',                                                       
            'href': url_for('authors'),                                            
        }                                                                          
    ])                                                                             

    result = field.serialize('_links', mockauthor)                                 
    assert result == [                                                             
        {                                                                                     
            'rel': 'self',                                                         
            'method': 'GET',                                                       
            'href': url_for('author', id=mockauthor.id),                           
        },                                                                         
        {                                                                          
            'rel': 'collection',                                                   
            'method': 'GET',                                                       
            'href': url_for('authors'),                                            
        }                                                                          
    ]

@sloria
Copy link
Member

sloria commented May 1, 2015

@Kareeeeem I believe the problem is that lists containing URLFor fields is not currently supported.

Here is a failing test:

def test_hyperlinks_field_recurses_into_list(ma, mockauthor):
    field = ma.Hyperlinks([
        {'rel': 'self', 'href': ma.URLFor('author', id='<id>')},
        {'rel': 'collection', 'href': ma.URLFor('authors')}
    ])
    result = field.serialize('_links', mockauthor)

    assert result == [
        {'rel': 'self', 'href': url_for('author', id=mockauthor.id)},
        {'rel': 'collection', 'href': url_for('authors')}
    ]

I believe the implementation will be simpler than I originally thought; will add this soon.

@sloria sloria removed the help wanted label May 1, 2015
@sloria sloria closed this as completed in f6730d8 May 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants