Angular2 support with HATEOAS

后端 未结 1 794
[愿得一人]
[愿得一人] 2021-02-10 07:20

I have a restful web service with the support of HATEOAS links. When I call \"http://localhost:8080/v1/bookings/1225380?lock=true\" link I got following resource URLs. I want t

相关标签:
1条回答
  • 2021-02-10 07:53

    You can create an Injectable for this and use this class instead of the angular http class. Here you filter the links and than call http with the right link.

    @Injectable() 
    export class Hypermedia {
    
       constructor(private http: Http) { }
    
       get(links: any[], rel: String, body?: any, options?: RequestOptionsArgs): Observable<Response> {
        var link = null;
        var request = null;
    
        // Find the right link
        links.forEach(function (_link) {
            if (_link.rel === rel) {
                link = _link
                return;
            }
        });
    
        return this.http.get(link.href);
    }
    

    }

    Provide this injector and add it to the constructor where you need it

    constructor(private hypermedia:Hypermedia)
    

    Then you can simply call it like you normally would call the http class

    this.hypermedia.get(myHypermediaLinksArray,myRel)
    

    Hope this helps:)

    0 讨论(0)
提交回复
热议问题